Using variables in the new {% render %} tag

Highlighted
Shopify Partner
8 0 3

This question has stemmed from the Shopify Partners Slack group, where myself and Will Christenson are hunting for an answer.

As Will wrote:

I recently read up on include being deprecated in favor of render (https://shopify.dev/docs/themes/liquid/reference/tags/deprecated-tags) - but have discovered a MAJOR issue with render, which is that it doesn’t allow you to include a snippet by using a variable for the snippet name (eg. {% render sizeSvg %} ). This breaks a number of development patterns we’ve used in the past, and is very problematic. Can anyone speak to whether or not Shopify is going to address that bug? Or alternatives?

Here is a link to a screenshot of the full thread:

https://monosnap.com/file/rZa49XgVGyNnJUG8hmeKEq3cB5z0em

Any takes on how to use a variable name inside the render tag?

0 Likes
Highlighted
Astronaut
1084 178 213

Hello,

There are three ways of doing it: using assigncapture or simply passing the variable value as an argument.

Here is a simple code for you test it out:

1. Create this snippet called custom-test.liquid.

<p>name: {{name}}</p>
<p>test: {{test}}</p>
<p>age: {{age}}</p>

2. At theme.liquid add this right below the <body>:

  {% assign test = 'llama' %}
  {% capture name %}
      kangaroo
  {% endcapture %}
  {% render 'custom-test', name: name, age: 10, test: test %}


As you can see, you can simply pass the variables you want by separating them with a comma.

Let me know if this solves your issue!

Regards,
Diego 

◦ Problem Solved? ✔️ Please click on Accept as Solution and like.
◦ Hire me! Recommended by 25+ customers on Facebook. Click here to read their reviews.
◦ Contact me at: diego.boarutto.fortes@gmail.com
1 Like
Highlighted
Shopify Partner
8 0 3

Thank you very much @diego_ezfy, I think that's the answer we were looking for.

I've posted your reply on the original Slack thread from which this question originated from, and once Will has had the chance to confirm this is the solution I'll go ahead and mark your answer as such.

Much appreciated!

0 Likes
Highlighted
Shopify Partner
8 0 3

Unfortunately it seems what we're trying to accomplish isn't possible after all; here's the response over on the Slack thread:

https://monosnap.com/file/rPHmaV2TQw0zUHJH7NPY74it8eYDmU

If you're a member of the Shopify Partners Slack Community, you can follow this link to view the thread directly:

https://shopifypartners.slack.com/archives/CD2QPG0LS/p1595026118181300

0 Likes
Highlighted
Shopify Partner
2294 116 353

Dynamic variable includes while useful should be avoided and considered for refactoring in legacy code.

I'd consider it an anti-pattern for any theme intended for merchants, since it's confusing to merchants(and developers) as to which snippet is actually being called.

If you need this level of dynamism consider an intermediary snippet that has described parameters used in switch statement to render final snippets.

We'll probably start seeing more of that for media objects , and yes irony of ironies avoiding such switch statements is the exact reason we want to use dynamic variable includes.

 

For a series of snippets ,intended for enumerable object's , use {% render %}'s for parameter in the switch

Problem Solved? ✔️Accept and ? Like the solution so you can help others.
Buy me a coffee ☕ paypal.me/paulnewton or donate to eff.org
Confused? Busy? Buy a custom solution paull.newton+shopifyforum@gmail.com
0 Likes