3

I'm using Stash to capture a list of articles. I'm trying to keep my viewModel separate from my view markup.

For each article, I'm capturing a nested list of categories.

In the view, I want to output comma-separated categories. I was under the impression that I could use backspace="x" on a get_list, but it doesn't seem to work. I am I going about this incorrectly?

I suppose I could use an if/else with 'count' and 'total_results' to remove the final comma and space, but would also like to know if backspace can work.

Set article list

{!-- Articles --}
{exp:stash:set_list name="articles" parse_tags="yes" parse_depth="2"}
    {exp:channel:entries
        channel="news"
        dynamic="no"
        orderby="sticky|entry_date"
        sort="desc|desc"
        sort_type="numeric|numeric"
        disable="member_data|category_fields"
        {triggers:entries}
    }
        {stash:id}{entry_id}{/stash:id}
        {stash:sticky}{sticky}{/stash:sticky}
        {stash:title}{title}{/stash:title}
        {stash:summary}{article_summary}{/stash:summary}
        {stash:date}{entry_date}{/stash:date}
        {stash:url}{page_url}{/stash:url}
        {stash:image}{article_featured_image}{/stash:image}

        {exp:stash:set_list:nested name="this_article_categories" parse_tags="yes" context="{entry_id}"}
            {categories}
                {stash:category_name}{category_name}{/stash:category_name}
                {stash:category_url_title}{category_url_title}{/stash:category_url_title}
            {/categories}
        {/exp:stash:set_list:nested}
    {/exp:channel:entries}
{/exp:stash:set_list}

Get Articles (See the get_list:nested name="this_article_categories" in the code below.)

{!-- Get Articles --}
{exp:stash:get_list name="articles" prefix="news" paginate="bottom" limit="15"}
<article>
    <header>
        <div class="meta">
            {news:date format="%M %j, %Y"} in {exp:stash:get_list:nested name="this_article_categories" backspace="2" context="{news:id}"}
                <a href="/news-events/news/category/{category_url_title}">{category_name}</a>, 
            {/exp:stash:get_list:nested}
        </div>
        <h1 class="gamma"><a href="{news:url}">{news:title}</a></h1>
    </header>
    <div class="article-body">
        {news:summary}
    </div>
    <p class="read-more"><a href="{news:url}">Continue reading <i class="ss-gizmo ss-right"></i></a></p>
</article>

<hr>

{/exp:stash:get_list}
3
  • I also posted the question on devot-ee where @mark-croxton says that backspace should be available on get_list, but maybe I'm not reading that right. devot-ee.com/add-ons/support/stash/viewthread/10103
    – jthomas
    Jul 16, 2014 at 18:24
  • Just a question: How are you using context="{entry_id}" in the set_list and context="{news:id}" in the get_list? Jul 16, 2014 at 21:53
  • I believe it's because I name it {stash:id} instead of {stash:entry_id} in the setter. And I have a news prefix on my getter. But in the nested list in the setter, I use the default {entry_id} for the context.
    – jthomas
    Jul 16, 2014 at 22:53

1 Answer 1

3

The backspace parameter can be used with {exp:stash:get_list}, however be aware that any trailing whitespace and line breaks will count as 'spaces'.

{!-- Desired output - no trailing comma --}
Product 1, Products 2, Product 3

{!-- this won't work because of the line break after the comma --}
{exp:stash:get_list name="products" backspace="1"}
   {title},
{/exp:stash:get_list}

{!-- Either close the space up manually... --}
{exp:stash:get_list name="products" backspace="1"}{title},{/exp:stash:get_list}

{!-- ...or use the trim parameter --}
{exp:stash:get_list name="products" trim="y"}
   {title},
{/exp:stash:get_list}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.