Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Liquid variables inside of a liquid tag call

Tags:

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?

like image 246
Jimmy Avatar asked Oct 27 '11 17:10

Jimmy


People also ask

What does capture mean in liquid?

Capture allows you to write complex logic and strings and then capture the output of a block of liquid to a variable. This means you can write functions for your liquid in one place and then use the variable in much simplified liquid. ``` {%- capture title -%}

How do I add a variable in Shopify?

You can add variables to any text field that contains the Add variable link. Click the Add variable link beneath the relevant field, and then choose a variable from the list.

What is render in Shopify liquid?

The render tag isolates the variables that are used in the snippet that is being rendered. Using the render tag increases the performance of the storefront on themes and makes theme code easier to understand and maintain.


1 Answers

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.

{% assign v = 'art' %}
{% link_to_article v %}

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 the context.

def render(context)
  "/#{context[@markup.strip]}/"
end

This obviously only allows one param to be passed. A more complex solution would parse params like x: 2, y: 3.

like image 116
Jonathan Julian Avatar answered Nov 15 '22 13:11

Jonathan Julian