博客标签的业务催化剂液体标记

时间:2015-06-24 12:04:17

标签: liquid business-catalyst

我正在使用blogposts资源输出我的博客帖子,并添加了分页,一次渲染三个帖子。这是正常的。但是使用blogposts资源我还添加了fields="tag" subresource="tags所以我可以渲染与每个博客帖子相关联的标签。

但我无法输出这部分内容。这是我用过的代码:

{% if globals.get.limit %}
    {% assign limit = globals.get.limit | convert: "number" %}
{% else %}
    {% assign limit = 3 %}
{% endif %}

{% if globals.get.skip %}
    {% assign skip = globals.get.skip | convert: "number" %}
{% else %}
    {% assign skip = 0 %}
{% endif %}

{% assign resource = "blogposts" fields="tag" subresource="tags"%}

{module_data resource="{{resource}}" version="v3" template="" limit="{{limit}}" skip="{{skip}}" collection="bpost"}

{% assign moduloCheck =  bpost.totalItemsCount | modulo :limit %}

{% if moduloCheck == 0 %}
    {% assign totalPages = bpost.totalItemsCount | divided_by: limit | minus: 1 %}
{% else %}
    {% assign totalPages = bpost.totalItemsCount | divided_by: limit %}
{% endif %}

{% assign currentPage =  globals.get.skip|divided_by:limit %}
<hr />
{% for item in bpost.items %}
    <h2>{{item.postTitle}}</h2>
    <p>{{item.postBody}}</p>
    <p>Skapad&nbsp;<span>{{item.releaseDate | date: "yyyy-MM-dd \ hh:mm:ss tt"}}</span></p>
    <p><a href="{{item.postUrl}}">Kommentera</a></p>
    <br />
    <ul>
        {% for item in bpost.items -%}
            <li><a href="/retreivepostsbytag.html?tagid={{item.tag.id}}"># {{item.tag.tagName}}</a></li>
        {% endfor -%}
    </ul>
    <hr />
{% endfor %}

<span>Sida:
    {% for pageNum in (0..totalPages) %}
        {% if pageNum == currentPage %}
            {{ pageNum | plus: 1 }}
        {% else %}
            <a href="{{ globals.get.id }}?skip={{ pageNum|times:globals.get.limit }}&amp;limit={{ limit }}">{{ pageNum | plus: 1 }}</a>
        {% endif %}
    {% endfor %}
</span>

下面是我试图在testpage

上实现此代码的测试页面

谢谢, -Sohail

1 个答案:

答案 0 :(得分:0)

对于每个帖子,标签数据的子资源请求需要发生一次,如下所示:

{% for item in bpost.items %}
    ...

    {module_data resource="blogposts" version="v3" fields="objectId,tagId,objectType,siteId,tag,post" subresource="tags" resourceId="{{ item.id }}" skip="0" limit="10" order="objectType" collection="postTagData"}
    <ul>
        {% for tagMeta in postTagData.items -%}
            <li>
                <a href="/retreivepostsbytag.html?tagid={{ tagMeta.tag.id }}"
                  >{{ tagMeta.tag.tagName }}</a>
            </li>
        {% endfor -%}
    </ul>

    ...
{% endfor %}

此外,您的resource变量只被设置为blogposts,另一部分(fields="tag" subresource="tags")被丢弃。为清楚起见,我将其更改为:

{% assign resource = "blogposts" %}

编辑:这是您的代码,已修改为包含更改:

{% if globals.get.limit %}
    {% assign limit = globals.get.limit | convert: "number" %}
{% else %}
    {% assign limit = 3 %}
{% endif %}

{% if globals.get.skip %}
    {% assign skip = globals.get.skip | convert: "number" %}
{% else %}
    {% assign skip = 0 %}
{% endif %}

{% assign resource = "blogposts" %} <!-- `fields="tag" subresource="tags"` was being discarded -->

{module_data resource="{{resource}}" version="v3" template="" limit="{{limit}}" skip="{{skip}}" collection="bpost"}

{% assign moduloCheck =  bpost.totalItemsCount | modulo :limit %}

{% if moduloCheck == 0 %}
    {% assign totalPages = bpost.totalItemsCount | divided_by: limit | minus: 1 %}
{% else %}
    {% assign totalPages = bpost.totalItemsCount | divided_by: limit %}
{% endif %}

{% assign currentPage =  globals.get.skip|divided_by:limit %}
<hr />

{% for item in bpost.items %}
    <h2>{{item.postTitle}}</h2>
    <p>{{item.postBody}}</p>
    <p>Skapad&nbsp;<span>{{item.releaseDate | date: "yyyy-MM-dd \ hh:mm:ss tt"}}</span></p>
    <p><a href="{{item.postUrl}}">Kommentera</a></p>
    <br />

    {module_data resource="blogposts" version="v3" fields="objectId,tagId,objectType,siteId,tag,post" subresource="tags" resourceId="{{ item.id }}" skip="0" limit="10" order="objectType" collection="postTagData"}
    <ul>
        {% for tagMeta in postTagData.items -%}
            <li>
                <a href="/retreivepostsbytag.html?tagid={{ tagMeta.tag.id }}"
                    >{{ tagMeta.tag.tagName }}</a>
            </li>
        {% endfor -%}
    </ul>

    <hr />
{% endfor %}

<span>Sida:
    {% for pageNum in (0..totalPages) %}
        {% if pageNum == currentPage %}
            {{ pageNum | plus: 1 }}
        {% else %}
            <a href="{{ globals.get.id }}?skip={{ pageNum|times:globals.get.limit }}&amp;limit={{ limit }}">{{ pageNum | plus: 1 }}</a>
        {% endif %}
    {% endfor %}
</span>