I made a custom link tag in Liquid and I am trying to be able to pass liquid variables into the call for that tag like so
{{ assign id = 'something' }} // this value is actual dynamic while looping through data
{% link_to article: id, text: 'Click Me!' %} // my custom tag
However this results in the article parameter being passed in as 'id' instead of 'something' as per the assign statement above it.
Does anyone know how to pass variables into tag calls?
Doesn't look like this is possible, my solution was to just pass the variable name in to the tag and grab it out of the context the tag is being rendered in. Like so:
in my tag code (condensed):
This solved the case for me
context[@markup.strip]
.My problem was that i wanted to be able to pass a variable to my custom Liquid tag like this:
{% get_menu main_menu navigation.html settings.theme.id %}
In order to do this i first split the variable string into different varaibles on every space character.
*This is just a more rich example that the answer above by Jonathan Julian
It would be great to have a tag that can be called with literals and variables like
or
or
and also of course
In order to so I propose a helper function
And in the render just call the helper function to get the value of the literal or variable.
FYI, the initialiser would look like this:
I've recently solved this very simply with Jekyll 0.11.2 and Liquid 2.3.0 by passing the name of the variable as the tag parameter.
You can also pass the name of the control var while in a loop, like
article
above.In
Liquid::Tag.initialize
,@markup
is the second parameter, the string following the tag name. The assigned variables are available in the top level of thecontext
.This obviously only allows one param to be passed. A more complex solution would parse params like
x: 2, y: 3
.