business catalyst liquid markup for blog tags

378 views Asked by At

I am using the blogposts resource to output my blogposts and added pagination to render three posts at a time. This is working as it is suppose to. But with the blogposts resource I have also added fields="tag" subresource="tags So I can render the tags associated with each blogpost.

But I am not being able to output this part. This is code I have used:

{% 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>

Heres a test page I am trying to implement this code upon: testpage

Thanks, -Sohail

1

There are 1 answers

4
Robert K. Bell On BEST ANSWER

The subresource request for the tag data needs to happen once for each post, like so:

{% 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 %}

Also, your resource variable was only ever being set to blogposts, the other part (fields="tag" subresource="tags") was getting discarded. I'd change it to this, for clarity:

{% assign resource = "blogposts" %}

EDIT: Here's your code, modified to include the changes:

{% 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>