【问题标题】:Display JSON data array from settings_data.json in page template liquid (Shopify)在页面模板液体(Shopify)中显示来自 settings_data.json 的 JSON 数据数组
【发布时间】:2023-04-10 10:32:01
【问题描述】:

我的 setting_data.json(Deput Shopify 主题)文件:

{
  "current": {
    "checkout_error_color": "#ff6d6d",
    "sections": {
      "header": {
        "type": "header",
        "settings": {
          "align_logo": "left",
          "logo": "shopify:\/\/shop_images\/logo_9bb43dd5-21d6-442c-8a19-0f4adf03e13a.png",
          "logo_max_width": 100,
          "main_linklist": "main-menu",
          "message": true,
          "home_page_only": true,
          "message_text": "Paw Paper – Edible Gift Wrap for Pets",
          "message_link": "",
          "color_bg": "#162950",
          "color_text": "#ffffff"
        }
      },

如何在 Shopify 液体模板页面中获取部分标题变量?

我可以得到一个变量:

{% assign text_color = settings.color_text %}
{{ text_color }}

我需要在页面上显示自定义块并从 settings_data.json 获取数据

"1543393771012": {
        "type": "custom-content",
        "blocks": {
          "1543393771012-1": {
            "type": "image",
            "settings": {
              "image": "shopify:\/\/shop_images\/info.png",
              "width": "50%",
              "alignment": "center"
            }
          },
          "1543393802354": {
            "type": "html",
            "settings": {
              "code": "<p>Paw Paper is the world's first edible wrapping paper designed specifically for pets. Our edible paper is 100% all-natural, made from potato starch with Omega-3 enhanced beef flavoring. It's water-soluble so no tape required!<\/p> <p>Just lick it like a stamp or dab water on the edges to bind the seams.<\/p>",
              "width": "50%"
            }
          }
        },

但是,我无法获取并显示带有变量的数组。

请帮忙。

【问题讨论】:

  • 你的 json 中的数组在哪里?
  • 如何从 settings_data.json 获取 message_text 变量?
  • 从第一个还是第二个?
  • 我们先试试吧。

标签: arrays json shopify liquid


【解决方案1】:

假设您的 JSONOBJ 是正确的。

var JSONOBJ={
  "current": {
    "checkout_error_color": "#ff6d6d",
    "sections": {
      "header": {
        "type": "header",
        "settings": {
          "align_logo": "left",
          "logo": "shopify:\/\/shop_images\/logo_9bb43dd5-21d6-442c-8a19-0f4adf03e13a.png",
          "logo_max_width": 100,
          "main_linklist": "main-menu",
          "message": true,
          "home_page_only": true,
          "message_text": "Paw Paper – Edible Gift Wrap for Pets",
          "message_link": "",
          "color_bg": "#162950",
          "color_text": "#ffffff"
        }
      }}}}

console.log(JSONOBJ.current.sections.header.settings.message_text)

【讨论】:

  • 我不想将 settings_data.json 中的所有内容复制到我的液体模板中。我需要直接从 .json 文件中获取变量。
  • 你要直接表示>?
  • 我需要直接从.json设置文件中获取数据,并在模板页面上展示出来。
【解决方案2】:

settings_data.json 文件不是您可以直接在 Liquid 中访问的文件,但您可以使用相应的 Liquid 命令访问其中存储的值。

Liquid 中的全局settings 对象使您可以访问在settings_schema.json 文件中定义的所有变量,仅此而已。

但是,您的 color_text 设置不是主题设置,而是名为“标题”的部分的部分设置。这个变量可以作为section.settings.color_text 访问,只要你在你的标题部分中调用它。

以类似的方式访问块设置。假设您有某种for block in section.blocks 循环,这些块级设置可以通过block.settings.whatever_key_you_made 访问

请记住 - 您创建的所有设置都有相应的范围,并且需要适当地访问! settings_schema.json 为您提供全局 settings 对象;每个部分都有其私有settings 对象;每个区块都有自己的settings 对象。

希望这会有所帮助!

【讨论】:

  • 注意:作为此范围界定的必然结果,无法访问资产文件内部的部分设置(或扩展的块设置)。这与全局设置形成对比,全局设置创建的资产中可用.liquid 附加到文件名。取决于节/块设置的 CSS 样式和 Javascript 变量声明必须在节或块中包含的内联样式/脚本标记内完成,视情况而定。
猜你喜欢
  • 1970-01-01
  • 2016-02-25
  • 1970-01-01
  • 1970-01-01
  • 2018-12-04
  • 1970-01-01
  • 1970-01-01
  • 2017-09-16
  • 2018-10-05
相关资源
最近更新 更多