机车CMS:使用液体变量动态定位content_type [英] Locomotive CMS: Dynamically Target content_types with Liquid Variable
问题描述
我正在Locomotive CMS中构建自定义导航片段,并且希望content_type条目与页面一起在导航中列出.
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.
特别(显然),我只希望列出content_type具有content_type_template 设置.
Specifically (and obviously) I only want entries listed if the content_type has a content_type_template set up.
我的计划是设置一个句柄(请在此处,但最终从文档)在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:
{% assign myContentType = loopPage.handle %}
{% for item in contents.myContentType %}
<li>a href="{{ item._slug }}">{{ item.name }}</a></li>
{% endfor %}
但是我遇到了一个问题:似乎我不能以这种方式使用变量.我认为尝试插入带有字符串值的变量来代替content_type slug存在某种问题.我不会以为这会是一个问题,但这是行不通的.
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. I wouldn't have thought it would be a problem, but it doesn't work.
我也尝试过:
{% for item in contents[myContentType] %}
并请确保:
{% for item in contents[myContentType].items %}
不高兴.
如何使用Liquid变量动态定位机车CMS中的content_type?
推荐答案
显然,您不能使用液体变量在机车CMS中动态定位content_type.具体来说,这是行不通的:
Apparently you can't use a Liquid Variable to dynamically target content_types in Locomotive CMS. 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的机车CMS实施不方便.
(在这一点上,我欢迎您进行确认或澄清,因为我使用的是轶事证据,而不是文档文件.)
(I'd welcome confirmation or clarification on this point as I'm drawing on anecdotal evidence, not documentation.)
最后,我能够使用Locomotive CMS RESTful API动态访问content_type条目.这在很大程度上没有文献记载,是一个完全独立的主题-保证了自己的Q&我在此处创建的A.
In the end, however, I was able to access content_type entries dynamically using the Locomotive CMS RESTful API. This is largely undocumented and a completely separate topic - one which warranted its own Q & A, which I have created here.
下面是我用来动态访问 content_type 条目的代码.(为简洁起见,我只包含了位于多个Liquid循环内的该代码段,以构建菜单项和子项.)
Below is the code I used to dynmically access content_type entries. (For brevity I have included only this snippet which sits inside a number of Liquid loops to build the menu items and sub items.)
{% 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 %}
这篇关于机车CMS:使用液体变量动态定位content_type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!