Question

How do I get blocks inside a Liquid template snippet to render inside Rails? Currently, I am able to render plain text snippets, for example a layout might be:

<!DOCTYPE html>
<html>
  <head>
    {% include 'stylesheets' %}
  </head>
  <body>
  </body>
</html>

If _stylesheets.liquid is plain text, this works fine. However, if I do something slightly more complicated, such as:

<!-- _stylesheets.liquid -->
{% for stylesheet in stylesheets %}
  {{ stylesheet.file_url | stylesheet_tag }}
{% endfor %}

This will render nothing. When placing the exact code in the layout, it renders the expected results:

<!-- This Works -->
<!DOCTYPE html>
<html>
  <head>
    {% for stylesheet in stylesheets %}
      {{ stylesheet.file_url | stylesheet_tag }}
    {% endfor %}
  </head>
  <body>
  </body>
</html>

Here is how I am rendering the liquid, in my rails view:

<%= Liquid::Template.parse(@theme.layout)
    .render('stylesheets' => @theme.stylesheets)
    .html_safe 
%>
Was it helpful?

Solution

I was able to get the snippet partial working by changing the code to the following:

 <!-- Layout -->
 <head>
    {% include 'stylesheets' %}
 </head>

<!-- Partial: _stylesheets.liquid -->
{{ stylesheets.file_url | stylesheet_tag }}

This iterates through each stylesheet, but I cannot for the life of me find out why it does so without a liquid for loop.

Any ideas why this works?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top