Skip to content

{exp:stash:set_list}

croxton edited this page Sep 24, 2014 · 9 revisions

Set an array of key/value pairs, defined by stash variable pairs {stash:my_key}my_value{/stash:my_key}. Automatically detects and captures multiple rows of variables pairs, so can be used to capture data from tags and tag pairs that loop.

{exp:stash:set_list name="my_list"}
      {stash:item_title}My title{/stash:item_title}
      {stash:item_summary}Summary text{/stash:item_summary}
      {stash:item_copy}Bodycopy goes here{/stash:item_copy}
{/exp:stash:set_list}

Parameters

Accepts the same parameters as {exp:stash:set}, with the following changes / additions:

match = [ #regex# ]

Match a column in the list against a regular expression. Only rows in the list that match will be appended to the list.

against = [ list column ]

Column to match against. If against is not specified or is not a valid list column, match="#regex#" will be applied to the whole block of tagdata passed to set_list.

prefix = [ string ]

Prefix for {if no_results}, e.g. {if my_prefix:no_results}

Examples

Capturing channel entry data

{exp:stash:set_list name="my_products" parse_tags="yes"}
        {exp:channel:entries channel="products"}
                {stash:item_title}{title}{/stash:item_title}
                {stash:item_summary}{summary}{/stash:item_summary}
                {stash:item_copy}{copy}{/stash:item_copy}
        {/exp:channel:entries}
{/exp:stash:set_list}

Capturing and caching Playa / Matrix tag pairs

{exp:channel:entries channel="blog" entry_id="123"}
	{exp:stash:set_list name="blog_related_entries" parse_tags="yes" save="yes" scope="site"}
		{blog_related}
			{stash:item_title}{title}{/stash:item_title}
		{/blog_related}
	{/exp:stash:set_list}	
{/exp:channel:entries}	

Match against - set items where the topic title begins with 'A'

{exp:stash:set_list name="recent_discussion_topics" parse_tags="yes" match="#^A#" against="topic_title"}
	{exp:forum:topic_titles 
		orderby="post_date" 
		sort="desc" 
		limit="5" 
		forums="1"
	}	
		{stash:topic_url}{thread_path='forum/viewthread'}{/stash:topic_url}
		{stash:topic_title}{title}{/stash:topic_title}
		{stash:last_author_url}{last_author_profile_path='member'}{/stash:last_author_url}
		{stash:last_author_name}{last_author}{/stash:last_author_name}
		{stash:last_post_date}{last_post_date}{/stash:last_post_date}
	{/exp:forum:topic_titles}
{/exp:stash:set_list}	

Nesting

{exp:stash:set_list name="my_entries" parse_tags="yes" parse_depth="2"}
	{exp:channel:entries channel="clients" limit="5"}

		{stash:entry_title}{title}{/stash:entry_title}
		{stash:entry_id}{entry_id}{/stash:entry_id}
	
		{exp:stash:set_list:nested name="related_entries_{entry_id}" parse_tags="yes"}
	
			{!-- this is a matrix tag pair --}
			{contact_docs}
				{stash:related_title}{mx_doc_title}{/stash:related_title}
			{/contact_docs}
		{/exp:stash:set_list:nested}	
	
	{/exp:channel:entries}	
{/exp:stash:set_list}

{exp:stash:get_list name="my_entries"}

	Entry title: {entry_title}

	Related:
	{exp:stash:get_list:nested name="related_entries_{entry_id}"}
		{related_title}
	{/exp:stash:get_list:nested}

{/exp:stash:get_list}

Handling no_results

no_results should ideally be handled by get_list, however it is possible to set a variable within the no_results block

{exp:stash:set_list parse_tags="yes" name="my_entries"}
	{exp:channel:entries 
		channel="clients" 
		limit="20"
	}
	    {stash:entry_title}{title}{/stash:entry_title}
		{stash:entry_id}{entry_id}{/stash:entry_id}
	
		 {if no_results}
      		{exp:stash:set name="my_message"}No results{/exp:stash:set}
    	{/if}

	{/exp:channel:entries}
{/exp:stash:set_list}

{exp:stash:get name="message" default="Showing results"}

{exp:stash:get_list name="my_entries" limit="5"}
    {entry_title} ({entry_id})
{/exp:stash:get_list}
Clone this wiki locally