从本节外部访问部分块字段

  • 本文关键字:字段 访问部 外部 shopify
  • 更新时间 :
  • 英文 :


我正在尝试访问从"部分文件模板"外部上载在部分字段中的图像的URL。有什么方法可以访问该部分,获取该部分的块,然后获得单个块的块值?我的希望是我可以访问调用该部分的页面模板上的促销图像,但是在页面模板的另一部分中,我想显示从块上传的图像。

<div id="carouselExampleControls" class="promotions  carousel slide" data-ride="carousel">
    <div class="carousel-inner">
        {% for block in section.blocks %}  
            {% if forloop.index == 1 %}
                <div class="carousel-item active">
                    <div class="row m-0  py-3 justify-content-center">
                        <h6 class="mb-0 row m-0 align-items-center  text-uppercase">{{block.settings.promotion-message}} </h6>
                        <a class="ml-3 btn btn-primary btn-sm" href="{{collections[block.settings.promotion-collection].url}}">{{block.settings.promotion-link-text}}</a>
                    </div>  
                </div>
            {% else %}
                <div class="carousel-item">
                    <div class="row m-0  py-3 justify-content-center">
                        <h6 class="mb-0 row m-0 align-items-center  text-uppercase">{{block.settings.promotion-message}} </h6>
                        <a class="ml-3 btn btn-primary btn-sm" href="{{collections[block.settings.promotion-collection].url}}">{{block.settings.promotion-link-text}}</a>
                    </div>  
                </div>
            {% endif %}
        {% endfor %}
    </div>
    <a class="carousel-control-prev text-dark" href="#carouselExampleControls" role="button" data-slide="prev">
        <i class="fas fa-angle-left text-dark"></i>
        <span class="sr-only">Previous</span>
    </a>
    <a class="carousel-control-next text-dark" href="#carouselExampleControls" role="button" data-slide="next">
        <i class="fas fa-angle-right text-dark"></i>
        <span class="sr-only">Next</span>
    </a>
</div>
{% schema %}
{
    "name": "Promotions",
    "max_blocks": 4,
    "settings":[],
    "presets": [
        {
            "name": "Promotions",
            "category": "Promotions",
            "blocks": [
                    { 
                    "type": "select"
                    },
                    {
                    "type": "select"
                    }
            ]
        }
    ],
    "blocks": [
        {
            "type": "select",
            "name": "Promotion",
            "settings": [
                {
                    "type": "richtext",
                    "id": "promotion-message",
                    "label": "Promotion Message",
                    "default": "<p>Your promotion message</p>"
                },
                {
                    "id":"promotion-link-text",
                    "type": "text",
                    "label": "Promotion Link Text",
                    "default":"Learn More"
                },
                {
                    "type": "collection",
                    "id": "promotion-collection",
                    "label": "Promotion Collection"
                },
                {
                    "id":"promotion-image",
                    "type": "image_picker",
                    "label": "Promotion Image"
                }
            ]
        }
    ]
}
{% endschema %}
{% stylesheet %}
{% endstylesheet %}
{% javascript %}
{% endjavascript %}

您无法传递或访问本节以外的任何内容。

这意味着本节之外创建的变量在其中不可用,并且本节内没有任何变量。

因此,section对象只能从"部分文件本身"访问。


现在说这有解决方法。

如果捕获整个部分:

{% capture section_item %}{% section 'your-section' %}{% endcapture %}

您将可以访问HTML节。如果将其作为数据属性输出或其他可以从HTML分开的内容,则可以获取所需的内容。

例如:

{{ section_item | split: 'data-image="' | last | split: '"" | first }}

您将从称为data-image的数据属性获得图像。

最新更新