Liquid templating : Parsing String value to objects and using as a variable

3657 views liquid
0

I'd like to give content editors a way to specify a gallery they'd like to use. Now, the input is retrieved as string, which, when stored into a variable intended to use as an object doesn't work. Please see the code below that works:

{% assign pagehandle = site.galleries.projects %}

<div class="section thumbs-grid" aria-hidden="true">
  {% for image in pagehandle.images %}
     <img src="{{ image | gallery_asset_url }}"/>
  {% endfor %}
</div>

The value I retrieve from the user input is stored as string, hence as below:

{{ page.user-input }} // Outputs site.galleries.projects

{% assign pagehandle = page.user-input %} // Probably stored as string

The above variable does not work. Any ideas would be very much appreciated. Thanks!

answered question

1 Answer

12

This can work :

{{ page.user-input }} // Outputs string "**projects**"

{% assign pagehandle = site.galleries[page.user-input] %}

posted this

Have an answer?

JD

Please login first before posting an answer.