简体   繁体   English

Locomotive CMS:使用 Liquid 变量动态定位 content_types

[英]Locomotive CMS: Dynamically Target content_types with Liquid Variable

I'm building a custom navigation snippet in Locomotive CMS and I want content_type entries to be listed in the navigation along with pages.我正在 Locomotive CMS 中构建自定义导航片段,并且我希望 content_type 条目与页面一起列在导航中。

Specifically (and obviously) I only want entries listed if the content_type has a content_type_template set up.具体(并且显然)我只希望在content_type设置了 content_type_template 时列出条目。

My plan was to set a handle (refereneced here but ultimately missing from the documentation) on the content_type_template page and then run the following logic when iterating through pages:我的计划是在 content_type_template 页面上设置一个句柄(在此处引用但最终从文档中丢失),然后在遍历页面时运行以下逻辑:

{% assign myContentType = loopPage.handle %}

{% for item in contents.myContentType %}

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

{% endfor %}

But I've run into a problem: It seems I can't use a variable in this way.但是我遇到了一个问题:似乎我不能以这种方式使用变量。 I assume there's some kind of issue with trying to plug a variable with a string value in place of a content_type slug.我认为尝试用字符串值代替 content_type slug 插入变量存在某种问题。 I wouldn't have thought it would be a problem, but it doesn't work.我不会认为这会是一个问题,但它不起作用。

I also tried:我也试过:

{% for item in contents[myContentType] %}

and just to be sure:只是为了确定:

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

No joy.没有喜悦。

How can I dynamically target content_types in Locomotive CMS using a Liquid variable?如何使用 Liquid 变量在 Locomotive CMS 中动态定位 content_types?

Apparently you can't use a Liquid Variable to dynamically target content_types in Locomotive CMS.显然,您不能使用 Liquid 变量在 Locomotive CMS 中动态定位 content_types。 Specifically, this won't work:具体来说,这行不通:

{% assign myVariable = some_string_that_is_the_same_as_a_content_type_slug %}

{% for item in contents.myVariable %}

I tried a number of methods and came to the conclusion that either:我尝试了多种方法,得出的结论是:

  • Liquid syntax simply doesn't accept a varibale in this context, or Liquid 语法在这种情况下根本不接受变量,或者
  • the Locomotive CMS implementation of Liquid doesn't facilitate it. Liquid 的 Locomotive CMS 实现并没有促进它。

(I'd welcome confirmation or clarification on this point as I'm drawing on anecdotal evidence, not documentation.) (我欢迎确认或澄清这一点,因为我正在利用轶事证据,而不是文件。)

In the end, however, I was able to access content_type entries dynamically using the Locomotive CMS RESTful API .然而,最后,我能够使用 Locomotive CMS RESTful API 动态访问 content_type 条目 This is largely undocumented and a completely separate topic - one which warranted its own Q & A, which I have created here .这在很大程度上没有记录,并且是一个完全独立的主题 - 一个值得我在这里创建的自己的问答。

Below is the code I used to dynmically access content_type entries.下面是我用来动态访问 content_type 条目的代码。 (For brevity I have included only this snippet which sits inside a number of Liquid loops to build the menu items and sub items.) (为简洁起见,我只包含了这个片段,它位于许多 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 %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM