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

时间:2019-07-31 18:50:06

标签: 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 %}

1 个答案:

答案 0 :(得分:1)

您不能通过或访问本节以外的任何内容。

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

因此section对象只能从节文件本身访问。


现在说有解决方法。

如果您捕获了整个部分:

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

您将有权访问html部分。如果将其输出为数据属性或其他易于从html拆分的内容,则可以获取所需的内容。

例如:

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

,您将从名为data-image的数据属性中获取图像。