Custom Parsing Liquid Tags

134 views
Skip to first unread message

chris.parrish

unread,
Sep 22, 2009, 11:42:55 PM9/22/09
to Liquid Templates
I am new to liquid. Is it possible to create a custom parser for a
tag?

I'd like to handle a tag with a format like:
{{ my_tag param1 key1="value1" key2="value2" ... keyN="valueN" }}

I would then read my key/value pairs into a hash. But since this
means handling an indeterminant number of key/value pairs so perhaps a
better syntax (easier to parse) might be something like:
{{ my_tag param1 (key1="value1" key2="value2" ...
keyN="valueN") }}

Does liquid offer the flexibility to parse my own tag like this so
that I can evaluate the params into my hash? Or is there already
something within liquid that does something like this?

Thanks,
Chris Parrish

did

unread,
Sep 23, 2009, 7:36:37 PM9/23/09
to Liquid Templates
Hey Chris,

Of course, you can do what you wrote. Actually, creating you own tag
is pretty simple. The key is to write the right regexp to parse your
options (in your case, key1="value1", ...). I suggest you to use
rubular.com to write it.
Oh, you misspelled the call to your tag: the right syntax is {% ... %}
and not {{ }} (used instead for displaying variables or drop
attributes).

In your case, you would have something like that:

class MyTag < Tag
Syntax = /(#{VariableSignature}+)\s*(.*)/

def initialize(tag_name, markup, tokens)
if markup =~ Syntax
@first_param = $1
@keys = keys_from_string($2)
...
end
...
end

private

def keys_from_string(str)
str.split(' ').inject({}) do |map, couple| # your keys are space
separated
# here, key contains <name of your key>="<value>". Write the
right regexp to get both name and value
# and add it to map. As a result, you will have a nice
hashmap.
...
map.merge!(key.to_sym => value)
end
end
end

My code is not tested but it gives you enough information to begin.
Hope it helps anyway.

Did

On 23 sep, 05:42, "chris.parrish" <chris.parrish-

chris.parrish

unread,
Sep 24, 2009, 11:46:37 AM9/24/09
to Liquid Templates

On Sep 23, 5:36 pm, did <didier.laffor...@gmail.com> wrote:
> Hey Chris,
>
> Of course, you can do what you wrote. Actually, creating you own tag
> is pretty simple. The key is to write the right regexp to parse your
> options (in your case, key1="value1", ...). I suggest you to use
> rubular.com to write it.


Thanks for the tip on Rubular.com. I've never seen that before --
looks very cool.


> Oh, you misspelled the call to your tag: the right syntax is {% ... %}
> and not {{ }} (used instead for displaying variables or drop
> attributes).


I understood the {{ }} syntax to be used for anything that creates an
output (or "resolves to text" as mentioned here:
http://wiki.github.com/tobi/liquid/liquid-for-designers). But then I
see examples of tags using {% %} notation that appear to produce
output (like here: http://wiki.github.com/tobi/liquid/liquid-for-programmers).
So I'm a bit confused (and very new to liquid).
Thank you very much. That helps tremendously. I'll test it and get
back to you.

-Chris
Reply all
Reply to author
Forward
0 new messages