-
Notifications
You must be signed in to change notification settings - Fork 20
Filtering lists with match against
Mark Croxton edited this page Mar 24, 2014
·
4 revisions
If you want to filter a list by multiple columns, create an extra index column to match against. In this example our list needs to be filtered by channel, by colour, and by one or more categories. Our index takes the form channel/colour/@category_1@category_2@
{exp:stash:set_list name="groceries"}
{stash:id}1{/stash:id}
{stash:title}Cox Apple{/stash:title}
{stash:date}5{/stash:date}
{stash:index}fruits/green/@apples@traditional@{/stash:index}
{stash:id}2{/stash:id}
{stash:title}Jazz Apple{/stash:title}
{stash:date}4{/stash:date}
{stash:index}fruits/yellow/@apples@modern@crisp@{/stash:index}
{stash:id}3{/stash:id}
{stash:title}Banana{/stash:title}
{stash:date}3{/stash:date}
{stash:index}fruits/yellow/@bananas@soft@{/stash:index}
{stash:id}4{/stash:id}
{stash:title}Brocoli{/stash:title}
{stash:date}2{/stash:date}
{stash:index}veggies/green/@plants@{/stash:index}
{stash:id}5{/stash:id}
{stash:title}Pepper{/stash:title}
{stash:date}1{/stash:date}
{stash:index}veggies/red/@plants@spicy@{/stash:index}
{/exp:stash:set_list}
<h3>5 most recent entries from channel "fruits"</h3>
{exp:stash:get_list name="groceries" match="#^fruits/#" against="index" orderby="date" sort="desc" limit="5"}
{title}<br>
{/exp:stash:get_list}
<h3>5 most recent entries from channel "veggies"</h3>
{exp:stash:get_list name="groceries" match="#^veggies#" against="index" orderby="date" sort="desc" limit="5"}
{title}<br>
{/exp:stash:get_list}
<h3>5 most recent entries from channel "fruits" from category "apples"</h3>
{exp:stash:get_list name="groceries" match="#^fruits/.*@apples@#" against="index" orderby="date" sort="desc" limit="5"}
{title}<br>
{/exp:stash:get_list}
<h3>5 most recent entries from channel "veggies" with color "green"</h3>
{exp:stash:get_list name="groceries" match="#^veggies/green/#" against="index" orderby="date" sort="desc" limit="5"}
{title}<br>
{/exp:stash:get_list}
<h3>5 most recent entries from channel "fruits" and category "apples" and color "green"</h3>
{exp:stash:get_list name="groceries" match="#^fruits/green/.*@apples@#" against="index" orderby="date" sort="desc" limit="5"}
{title}<br>
{/exp:stash:get_list}
When accessing a list with {exp:stash:get_list}
you can also choose to track which items have already been used in your template, and exclude or include those items in other lists on the same page. Tracking is based on the column name, and works across any lists that share the same column names.
<h2>Bonus: tracking (requires Stash 2.5.1+)</h2>
<h3>5 most recent entries from channel "fruits" and category "apples" and color "green": tracking "id" column:</h3>
{exp:stash:get_list name="groceries" match="#^fruits/green/.*@apples@#" against="index" orderby="date" sort="desc" limit="5" track="id"}
{title}<br>
{/exp:stash:get_list}
<h3>5 most recent entries excluding items already in the tracked "id" column</h3>
{exp:stash:get_list name="groceries" orderby="date" sort="desc" limit="5" not_in="id"}
{title}<br>
{/exp:stash:get_list}
<h3>Tracking even works with entirely separate lists, providing the same column name is used</h3>
{exp:stash:get_list name="shopping" orderby="date" sort="desc" limit="5" in="id"}
{title}<br>
{/exp:stash:get_list}
Getting started
Using Stash
Using Mustash
- Mustash
- Installing Mustash
- Managing variables
- Managing bundles
- Cache-breaking rules
- Mustash plugins
- Mustash Varnish plugin
- Mustash plugin development
- Mustash API
Template design patterns
Tag reference
- {exp:stash:set}
- {exp:stash:get}
- {exp:stash:block}
- {exp:stash:set_value}
- {exp:stash:append}
- {exp:stash:append_value}
- {exp:stash:prepend}
- {exp:stash:prepend_value}
- {exp:stash:copy}
- {exp:stash:context}
- {exp:stash:is_empty}
- {exp:stash:not_empty}
- {exp:stash:set_list}
- {exp:stash:get_list}
- {exp:stash:append_list}
- {exp:stash:prepend_list}
- {exp:stash:split_list}
- {exp:stash:join_lists}
- {exp:stash:list_count}
- {exp:stash:unset}
- {exp:stash:flush_cache}
- {exp:stash:bundle}
- {stash:embed}
- {exp:stash:extend}
- {exp:stash:parse}
- {exp:stash:cache}
- {exp:stash:static}
- {exp:stash:finish}
- {exp:stash:not_found}
- Short tag syntax
- Using Stash methods in your own add-ons