Shopify template Shopify部分中的可变范围
我试图在分区文件中使用一个变量,但它似乎不是从父模板继承的 例如: 指数.液体Shopify template Shopify部分中的可变范围,shopify-template,Shopify Template,我试图在分区文件中使用一个变量,但它似乎不是从父模板继承的 例如: 指数.液体 {% assign foo = "bar" %} {% section 'example' %} <h1>{{ foo }}</h1> {% schema %} { "name": "Example", "settings": [ ... ] } {% endschema %} {% assign foo = "bar" %} {% include
{% assign foo = "bar" %}
{% section 'example' %}
<h1>{{ foo }}</h1>
{% schema %}
{
"name": "Example",
"settings": [
...
]
}
{% endschema %}
{% assign foo = "bar" %}
{% include 'example' %}
<h1>{{ foo }}</h1>
<h1><!-- foo --></h1>
{% assign foo = "bar" %}
{% capture section %}{% section 'example' %}{% endcapture %}
{{ section | replace: '<!-- foo -->', foo }}
<div>
<!-- REMOVE_TITLE? -->
<h1>{{ section.settings.title }}</h1>
<!-- REMOVE_TITLE? -->
<ul>
{% for block in section.blocks %}
<li>{{ block.settings.image | img_url: '300x' | img_tag }}</li>
{% endfor %}
</ul>
</div>
部分/示例。液体
{% assign foo = "bar" %}
{% section 'example' %}
<h1>{{ foo }}</h1>
{% schema %}
{
"name": "Example",
"settings": [
...
]
}
{% endschema %}
{% assign foo = "bar" %}
{% include 'example' %}
<h1>{{ foo }}</h1>
<h1><!-- foo --></h1>
{% assign foo = "bar" %}
{% capture section %}{% section 'example' %}{% endcapture %}
{{ section | replace: '<!-- foo -->', foo }}
<div>
<!-- REMOVE_TITLE? -->
<h1>{{ section.settings.title }}</h1>
<!-- REMOVE_TITLE? -->
<ul>
{% for block in section.blocks %}
<li>{{ block.settings.image | img_url: '300x' | img_tag }}</li>
{% endfor %}
</ul>
</div>
片段/example.liquid
{% assign foo = "bar" %}
{% section 'example' %}
<h1>{{ foo }}</h1>
{% schema %}
{
"name": "Example",
"settings": [
...
]
}
{% endschema %}
{% assign foo = "bar" %}
{% include 'example' %}
<h1>{{ foo }}</h1>
<h1><!-- foo --></h1>
{% assign foo = "bar" %}
{% capture section %}{% section 'example' %}{% endcapture %}
{{ section | replace: '<!-- foo -->', foo }}
<div>
<!-- REMOVE_TITLE? -->
<h1>{{ section.settings.title }}</h1>
<!-- REMOVE_TITLE? -->
<ul>
{% for block in section.blocks %}
<li>{{ block.settings.image | img_url: '300x' | img_tag }}</li>
{% endfor %}
</ul>
</div>
{{foo}
当渲染时,我将在其中获得条
- 这是一个bug,还是预期的行为
- 有没有一种方法可以包含一个节并使用某种形式的外部范围中的变量
谢谢 如果这是故意的行为,我设法找到了一个绕过它的方法,并认为我会发布我不完美但可行的解决方案: 部分/示例。液体
{% assign foo = "bar" %}
{% section 'example' %}
<h1>{{ foo }}</h1>
{% schema %}
{
"name": "Example",
"settings": [
...
]
}
{% endschema %}
{% assign foo = "bar" %}
{% include 'example' %}
<h1>{{ foo }}</h1>
<h1><!-- foo --></h1>
{% assign foo = "bar" %}
{% capture section %}{% section 'example' %}{% endcapture %}
{{ section | replace: '<!-- foo -->', foo }}
<div>
<!-- REMOVE_TITLE? -->
<h1>{{ section.settings.title }}</h1>
<!-- REMOVE_TITLE? -->
<ul>
{% for block in section.blocks %}
<li>{{ block.settings.image | img_url: '300x' | img_tag }}</li>
{% endfor %}
</ul>
</div>
您可以使用capture以字符串形式获取节的内容,并对捕获的标记使用字符串筛选器:
指数.液体
{% assign foo = "bar" %}
{% section 'example' %}
<h1>{{ foo }}</h1>
{% schema %}
{
"name": "Example",
"settings": [
...
]
}
{% endschema %}
{% assign foo = "bar" %}
{% include 'example' %}
<h1>{{ foo }}</h1>
<h1><!-- foo --></h1>
{% assign foo = "bar" %}
{% capture section %}{% section 'example' %}{% endcapture %}
{{ section | replace: '<!-- foo -->', foo }}
<div>
<!-- REMOVE_TITLE? -->
<h1>{{ section.settings.title }}</h1>
<!-- REMOVE_TITLE? -->
<ul>
{% for block in section.blocks %}
<li>{{ block.settings.image | img_url: '300x' | img_tag }}</li>
{% endfor %}
</ul>
</div>
{%assign foo=“bar”%}
{%capture-section%}{%section'示例'%}{%endcapture%}
{{节|替换:'',foo}
当然,您可以用变量替换任何字符串。但是我发现HTML注释工作得很好,因为如果您忘记运行replace,或者不需要运行replace,则不会呈现任何内容
如果您想做一些更复杂的事情,例如从节中删除一些标记,您可以:
部分/示例。液体
{% assign foo = "bar" %}
{% section 'example' %}
<h1>{{ foo }}</h1>
{% schema %}
{
"name": "Example",
"settings": [
...
]
}
{% endschema %}
{% assign foo = "bar" %}
{% include 'example' %}
<h1>{{ foo }}</h1>
<h1><!-- foo --></h1>
{% assign foo = "bar" %}
{% capture section %}{% section 'example' %}{% endcapture %}
{{ section | replace: '<!-- foo -->', foo }}
<div>
<!-- REMOVE_TITLE? -->
<h1>{{ section.settings.title }}</h1>
<!-- REMOVE_TITLE? -->
<ul>
{% for block in section.blocks %}
<li>{{ block.settings.image | img_url: '300x' | img_tag }}</li>
{% endfor %}
</ul>
</div>
{{section.settings.title}
{section.blocks%中的块的%s}
- {{block.settings.image | img_url:'300x'| img_tag}
{%endfor%}
然后你可以做一些类似的事情:
{% capture section %}{% section 'example' %}{% endcapture %}
{% assign parts = section | split: '<!-- REMOVE_TITLE? -->' %}
{% for part in parts %}
{% assign mod = forloop.index | modulo: 2 %}
{% if mod > 0 %}{{ part }}{% endif %}
{% endfor %}
{%capture-section%}{%section'示例'%}{%endcapture%}
{%assign parts=节|拆分:'%}
{零件%中的零件为%1}
{%assign mod=forloop.index |模:2%}
{%if mod>0%}{{part}{%endif%}
{%endfor%}
我会将您的所有变量分配到一个代码段中,并将相同的代码段包含在您需要在其中使用变量的任何范围内
这是一个相当枯燥的方法
此外,config/settings_schema.json中定义的任何内容都具有全局范围,但最终用户可以在主题设置中为其指定新值 这可能是我在Shopify周围见过的最脏的东西之一&液体,太棒了!我以为我看到了一切。。。这绝对是Shopify最肮脏的把戏之一!非常感谢你!使用js不是更容易吗?您也可以使用'param with'@FabricioG'发送它。在这个特定的示例中,是的,js会使代码更简洁。但是我的实际实现会产生很多多余的HTML,出于SEO原因我不想复制这些HTML。“with”参数在使用部分时对我不起作用,只对include起作用。