简体   繁体   English

Shopify 液体标签嵌套在液体标签中 - 任何解决方法?

[英]Shopify Liquid tag nested in liquid tag - any workaround?

I'm new to Shopify and Liquid.我是 Shopify 和 Liquid 的新手。 I know that you can't nest a liquid tag within another liquid tag ie:我知道您不能将液体标签嵌套在另一个液体标签中,即:

{% something {{ something_else }} %}

I was wondering if there is a workaround for this kind of scenario?我想知道这种情况是否有解决方法? Possibly involving 'capture' or clever use of 'raw'?可能涉及“捕获”或巧妙地使用“原始”?

I'm building a site that uses product tags to denote which chocolates go in which products (collection of chocolates).我正在建立一个网站,该网站使用产品标签来表示哪些巧克力进入哪些产品(巧克力系列)。 On the product page I can easily return the tags as a list:在产品页面上,我可以轻松地将标签作为列表返回:

<ul class="chocolates-menu">
  {% for tag in product.tags %}
    <li><a href="/collections/all/{{ tag | handleize }}">{{ tag }}</a></li>
  {% endfor %}
</ul>

However, I'd like to render snippets with file names to match the names of the tags (these will contain an image, chocolate name and chocolate description) ie:但是,我想渲染带有文件名的片段以匹配标签的名称(这些将包含图像、巧克力名称和巧克力描述),即:

<li><a href="/collections/all/{{ tag | handleize }}">{% render '{{ tag }}' %}</a></li>

The closest I've got is:我得到的最接近的是:

{% for tag in product.tags %}
  {% capture chocolate_tag %}
    {% raw %}{% render{% endraw %} {% raw %}'{% endraw %}{{ tag }}{% raw %}' %}{% endraw %}
  {% endcapture %}
  <li><a href="/collections/all/{{ tag | handleize }}">{{ chocolate_tag }}</a></li>
{% endfor %}

This will output the correct code but as text on the page (rather than parsing it).这将输出正确的代码,但作为页面上的文本(而不是解析它)。 ie: {% render 'Tag Name Here' %} simply as the text of the list item.即: {% render 'Tag Name Here' %} 只是作为列表项的文本。 Any help from brighter folk, is much appreciated.非常感谢聪明人的任何帮助。 Thanks.谢谢。

I would suggest creating a snippet for all your chocolates and using the tag as a variable to output what is needed.我建议为所有巧克力创建一个片段,并使用标签作为变量来输出所需的内容。

Here is a visual representation of what I mean and is kinda clearer from your discussion with @Fabio Filippi这是我的意思的直观表示,从您与@Fabio Filippi 的讨论中更清楚

snippets/chocolate.liquid

{% assign tag_image = tag | append: '.png' %}

{% case tag %}
  {% when 'diet' %}
    <img src="{{ tag_image | file_img_url: '100x' }}" class="responsive" />

  {% when 'dark' %}
    <div>My dark chocolate HTML</div>

  {% when 'white' %}
    <div>My white chocolate HTML</div>

{% endcase %}

and how to use:以及如何使用:

{% render 'chocolate', tag: tag %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM