I'm just getting my feet wet with Liquid (pardon the pun), and am
planning to integrate into a project I'm working on.
So far, I'm impressed! However, I am having some difficulty figuring
out how to send parameters to various methods.
As far as I can tell calls to objects included in the assigns
parameter to the Liquid::Template.render method cannot accept
parameters at all. Is that correct? It looks to be the case, but I
haven't seen that spelled out anywhere in black & white.
Say someone wanted to embed some specific content from another section
of the site, what is the best way to go about doing that?
I want to do something like this:
{% for item in site.sections('gardening').items %}
{{ item.name }}
{% endfor %}
In this case 'site' is a Drop that provides a section(section_id)
method -- but I can't get that parameter to the method.
Ways I've thought about doing this...
Have site.section return a drop, where the before_method
implementation looks for a matching id, and if so returns the
appropriately wrapped object. It would looks something like:
{% for item in site.sections.gardening.items %}
{{ item.name }}
{% endfor %}
I'm fairly certain I can get this approach working. But it only seems
like it would work for valid method names. I couldn't, for example,
use this approach if the parameter I needed to pass were a URL or a
float, or if I needed to pass multiple params.
So the other potential solution I see is implementing a customer
Liquid::Tag or Liquid::Block -- but that is getting deep quick and
it's getting late here -- so I thought I'd send the question out the
group to see if I'm heading down the right path.
Thanks in advance for your advice.
Mason
Seems I found my own answer in this thread:
http://groups.google.com/group/liquid-templates/browse_thread/thread/90ae684e754b6de5/a0624d6e136e2217?lnk=gst&q=assign&rnum=5#a0624d6e136e2217
I've been able to use filters to implement it like so:
{{ 'gardening' | section | assign_to: 'my_section' }}
{{ my_section.name }} #=> gardening
It works, the syntax still feels awkward, but I'll live with it.
Mason
There are some nice tricks in Liquid and in Ruby that make these
things easier though. I for example have an OptionDrop which allows to
retrieve the settings used in the app using the code:
{{ option.setting_name }}
All this can be done using the following Ruby code:
module Liquid
class OptionDrop < Drop
def before_method(name)
::Option.get_option(name)
end
end
end
::Option refers to the native Ruby object for the settings, and
Option#get_option is a function that returns the setting for a given
name. Additionally, you have to give an "option" assign to
Template.render that is an instance of OptionDrop.
This works because before_method is a catch-all, much like Ruby's
method_missing, although not completely. Take a look at drop.rb in
Liquid to get a sense of how it works. I reccommend reading other
parts of the Liquid code too, the code is good and clear, and readin
it allows you to get a better knowledge of how Liquid works easily.
JW