0

我正在 Locomotive CMS 中构建一个自定义导航片段,我希望 content_type 条目与页面一起列在导航中。

具体来说(显然)我只希望在content_type设置了 content_type_template 时列出条目。

我的计划是在 content_type_template 页面上设置一个句柄(在此处引用但最终从文档中丢失),然后在遍历页面时运行以下逻辑:

{% assign myContentType = loopPage.handle %}

{% for item in contents.myContentType %}

    <li>a href="{{ item._slug }}">{{ item.name }}</a></li>

{% endfor %}

但是我遇到了一个问题:似乎我不能以这种方式使用变量。我认为尝试用字符串值代替 content_type slug 插入变量存在某种问题。我不会认为这会是一个问题,但它不起作用。

我也试过:

{% for item in contents[myContentType] %}

并且可以肯定的是:

{% for item in contents[myContentType].items %}

没有喜悦。

如何使用 Liquid 变量在 Locomotive CMS 中动态定位 content_types?

4

1 回答 1

0

显然,您不能使用 Liquid 变量在 Locomotive CMS 中动态定位 content_types。具体来说,这是行不通的:

{% assign myVariable = some_string_that_is_the_same_as_a_content_type_slug %}

{% for item in contents.myVariable %}

我尝试了多种方法,得出的结论是:

  • Liquid 语法在这种情况下根本不接受变量,或者
  • Liquid 的 Locomotive CMS 实施并不能促进它。

(我欢迎确认或澄清这一点,因为我使用的是轶事证据,而不是文件。)

然而,最后,我能够使用 Locomotive CMS RESTful API 动态访问 content_type 条目这在很大程度上是未记录的,并且是一个完全独立的主题 - 我在这里创建了它自己的问答。

下面是我用来动态访问 content_type 条目的代码。(为简洁起见,我只包含了这个片段,它位于许多 Liquid 循环中以构建菜单项和子项。)

{% assign myContentType = loopPage.handle %}

{% action "get content_type entries" %}
    var contentData = false;
    contentData = callAPI('GET', 'https://station.locomotive.works/locomotive/api/v3/content_types/{{ myContentType }}/entries.json', {
        data: {
        },
        headers: {
            'X-Locomotive-Account-Email': '[my-account-email-adddress]',
            'X-Locomotive-Site-Handle': '[my-site-handle]',
            'X-Locomotive-Account-Token': '[my-account-token]'
        }
    });
    setProp('contentData', contentData);
{% endaction %}

{% for item in contentData.data %}

    <li><a href="{{ item._slug}}">{{ item.name }}</a></li>

{% endfor %}
于 2020-11-08T21:10:38.837 回答