Skip to content

Instantly share code, notes, and snippets.

@thisisjamessmith
Last active April 15, 2019 19:01
Show Gist options
  • Save thisisjamessmith/6892683 to your computer and use it in GitHub Desktop.
Save thisisjamessmith/6892683 to your computer and use it in GitHub Desktop.
Intermediate Stash templating with ExpressionEngine to reuse the same markup for different types of listing. See related article for discussion of this template: http://www.jamessmith.co.uk/articles/expressionengine-stash-tutorial-level-2
{exp:switchee variable="{segment_2}" parse="inward"}
{!-- ====================================
FETCH DATA for various GAME LISTINGS
(categories, publishers, designers, all, search, alphalist).
The markup is assembled at the end of this template.
The crazy-looking regex: #^P(\d+)$# simply matches pagination segments ("P" plus any digits).
Then this one #^(\d+)$# looks for date archives (any digits)
========================================= --}
{case value="''|cat|#^P(\d+)$#|#^(\d+)$#"}
{exp:stash:set parse_tags="yes"}
{if segment_2 == 'cat'}
{stash:title}{segment_3_category_name} Games{/stash:title}
{stash:description}{if segment_3_category_description}{segment_3_category_description}{/if}{/stash:description}
{/if}
{if segment_2 != 'cat'}
{stash:title}Our Games{/stash:title}
{stash:description}{exp:low_variables:single var="lv_games_intro"}{/stash:description}
{/if}
{/exp:stash:set}
{exp:stash:set_list
name="games-{segment_2}{segment_3}{segment_4}"
parse_depth="2"
parse_conditionals="yes"
parse_tags="yes"
save="yes"
replace="no"
scope="site"
trim="yes"
}
{exp:low_alphabet:entries
channel="games"
orderby="title"
sort="asc"
alpha_ignore="The"
disable="categories|member_data"
limit="100"
dynamic="yes"
}
{stash:game-image}{game_image}{/stash:game-image}
{stash:game-title}{title}{/stash:game-title}
{stash:game-link}{url_title_path='games'}{/stash:game-link}
{if {game_options:selected option="in-store"}}{stash:play-in-store}true{/stash:play-in-store}{/if}
{if {game_options:selected option="online"}}{stash:buy-online}true{/stash:buy-online}{/if}
{stash:on-sale}{game_details:on_sale}{/stash:on-sale}
{stash:regular-price}{game_details:regular_price}{/stash:regular-price}
{stash:price}{game_details:price}{/stash:price}
{stash:you-save}{game_details:you_save}{/stash:you-save}
{stash:you-save-percent}{game_details:you_save_percent}{/stash:you-save-percent}
{paginate}
{stash:has-pagination}true{/stash:has-pagination}
{stash:current-page}{current_page}{/stash:current-page}
{stash:total-pages}{total_pages}{/stash:total-pages}
{stash:pagination-links}
{pagination_links}
{first_page}<li><a href="{pagination_url}">First</a></li>{/first_page}
{previous_page}<li><a href="{pagination_url}">«</a></li>{/previous_page}
{page}<li{if current_page} class="active"{/if}><a href="{pagination_url}">{pagination_page_number}</a></li>{/page}
{next_page}<li><a href="{pagination_url}">»</a></li>{/next_page}
{last_page}<li><a href="{pagination_url}">Last</a></li>{/last_page}
{/pagination_links}
{/stash:pagination-links}
{/paginate}
{/exp:low_alphabet:entries}
{/exp:stash:set_list}
{/case}
{!-- ________________________________ --}
{case value="alphalist"}
{exp:stash:set_list
name="games-{segment_2}{segment_3}{segment_4}"
parse_depth="2"
parse_conditionals="yes"
parse_tags="yes"
save="yes"
replace="no"
scope="site"
trim="yes"
}
{exp:low_alphabet:entries
alpha_filter="{segment_3}"
channel="games"
orderby="title"
sort="asc"
alpha_ignore="A|The"
auto_limit="yes"
disable="categories|member_data|pagination"
dynamic="yes"
}
{stash:game-image}{game_image}{/stash:game-image}
{stash:game-title}{title}{/stash:game-title}
{stash:game-link}{url_title_path='games'}{/stash:game-link}
{if {game_options:selected option="in-store"}}{stash:play-in-store}true{/stash:play-in-store}{/if}
{if {game_options:selected option="online"}}{stash:buy-online}true{/stash:buy-online}{/if}
{stash:on-sale}{game_details:on_sale}{/stash:on-sale}
{stash:regular-price}{game_details:regular_price}{/stash:regular-price}
{stash:price}{game_details:price}{/stash:price}
{stash:you-save}{game_details:you_save}{/stash:you-save}
{stash:you-save-percent}{game_details:you_save_percent}{/stash:you-save-percent}
{/exp:low_alphabet:entries}
{/exp:stash:set_list}
{/case}
{!-- ________________________________ --}
{case value="publishers|designers"}
{exp:stash:set_list
name="games-{segment_2}{segment_3}{segment_4}"
parse_depth="3"
parse_conditionals="yes"
parse_tags="yes"
save="yes"
replace="no"
scope="site"
trim="yes"
}
{exp:channel:entries
channel="{segment_2}"
dynamic="no" limit="1"
url_title="{segment_3}"
disable="categories|member_data|pagination"
}
{exp:stash:set}
{stash:title}{title}{/stash:title}
{stash:description}{publishers_description}{designers_description}{/stash:description}
{/exp:stash:set}
{exp:playa:parents entry_id="{entry_id}" sort="asc" limit="400"}
{stash:game-image}{game_image}{/stash:game-image}
{stash:game-title}{title}{/stash:game-title}
{stash:game-link}{url_title_path='games'}{/stash:game-link}
{if {game_options:selected option="in-store"}}{stash:play-in-store}true{/stash:play-in-store}{/if}
{if {game_options:selected option="online"}}{stash:buy-online}true{/stash:buy-online}{/if}
{stash:on-sale}{game_details:on_sale}{/stash:on-sale}
{stash:regular-price}{game_details:regular_price}{/stash:regular-price}
{stash:price}{game_details:price}{/stash:price}
{stash:you-save}{game_details:you_save}{/stash:you-save}
{stash:you-save-percent}{game_details:you_save_percent}{/stash:you-save-percent}
{/exp:playa:parents}
{/exp:channel:entries}
{/exp:stash:set_list}
{/case}
{!-- ________________________________ --}
{case value="search"}
{exp:stash:set_list
name="games-{segment_2}{segment_3}{segment_4}"
parse_depth="2"
parse_conditionals="yes"
parse_tags="yes"
trim="yes"
}
{exp:stash:set}
{stash:title}Search Results{/stash:title}
{/exp:stash:set}
{exp:low_search:results query="{segment_3}" limit="20"}
{stash:result-channel}{channel_short_name}{/stash:result-channel}
{stash:game-image}{game_image}{/stash:game-image}
{stash:game-title}{title}{/stash:game-title}
{if channel_short_name == 'games'}{stash:game-link}{url_title_path='games'}{/stash:game-link}{/if}
{if channel_short_name == 'publishers'}{stash:game-link}{url_title_path='games/publishers'}{/stash:game-link}{/if}
{if channel_short_name == 'designers'}{stash:game-link}{url_title_path='games/designers'}{/stash:game-link}{/if}
{if {game_options:selected option="in-store"}}{stash:play-in-store}true{/stash:play-in-store}{/if}
{if {game_options:selected option="online"}}{stash:buy-online}true{/stash:buy-online}{/if}
{stash:on-sale}{game_details:on_sale}{/stash:on-sale}
{stash:regular-price}{game_details:regular_price}{/stash:regular-price}
{stash:price}{game_details:price}{/stash:price}
{stash:you-save}{game_details:you_save}{/stash:you-save}
{stash:you-save-percent}{game_details:you_save_percent}{/stash:you-save-percent}
{/exp:low_search:results}
{/exp:stash:set_list}
{/case}
{/exp:switchee}
{!-- ======================================================================== --}
{!-- ======================================================================== --}
{!-- ASSEMBLE THE MARKUP for LISTINGS VIEWS ================================= --}
{!-- ======================================================================== --}
{!-- ======================================================================== --}
{exp:switchee variable="{segment_2}" parse="inward"}
{case value="''|cat|publishers|designers|search|#^P(\d+)$#|#^(\d+)$#|alphalist"}
{exp:stash:set parse_tags="yes"}
{stash:page-title}{exp:stash:get name="title"}{/stash:page-title}
{!-- build a meta description from the first 20 words of the regular description--}
{stash:meta-desc}{exp:eehive_hacksaw words="20"}{exp:stash:get name="description"}{/exp:eehive_hacksaw}{/stash:meta-desc}
{stash:meta-keys}Board Games, {exp:stash:get name="title"}{/stash:meta-keys}
{stash:content}
<h1>
{if segment_2 == 'publishers'}All games published by {/if}
{if segment_2 == 'designers'}All games designed by {/if}
{if segment_2 == 'cat'}All games filed under {/if}
{if segment_2 == 'alphalist'}All games starting with '{segment_3}'{/if}
{if {exp:stash:not_empty name="title"}}{exp:stash:get name="title"}{/if}
</h1>
{if {exp:stash:not_empty name="description"}}
<p>{exp:stash:get name="description"}</p>
{/if}
{if segment_2 == 'search'}
<p>You searched for: <strong>{exp:low_search:keywords query="{segment_3}"}</strong></p>
{/if}
{if segment_2 != 'search'}
<div class="pagination pagination-small">
<ul>
{exp:low_alphabet:azlist
channel="games"
alpha_field="title"
alpha_ignore="the|a"
numbers="after"
group_numbers="yes"
numbers_label="#"
}
<li{if segment_3 == low_alphabet_url} class="active"{/if}><a href="{path="games/alphalist/{low_alphabet_url}"}">{low_alphabet_label}</a></li>
{/exp:low_alphabet:azlist}
</ul>
</div>
{/if}
<div class="filters">
<div class="btn-group">
<a class="btn btn-inverse" style="cursor:default">Filters:</a>
<a class="btn active" href="#filter=*">All Games</a>
<a class="btn" href="#filter=.buyOnline">Available to buy online</a>
<a class="btn" href="#filter=.inStore">Available to play in the café</a>
</div>
</div>
{if {exp:stash:not_empty name="games-{segment_2}{segment_3}{segment_4}"}}
{exp:stash:get_list name="games-{segment_2}{segment_3}{segment_4}" scope="site"}
{if count == 1}<ul>{/if}
<li class="{if play-in-store}inStore{/if} {if buy-online}buyOnline{/if}">
<a href="{game-link}">
{exp:ce_img:single alt="{game-title}" title="{game-title}" src="{game-image}" width="150" crop="yes"}
{if result-channel == 'publishers' || result-channel == 'designers'}
<h3>
{if result-channel == 'publishers'}Publisher: {/if}
{if result-channel == 'designers'}Designer: {/if}
{game-title}
</h3>
{if:else}
<p>
{game-title}
{if buy-online}
<br />
{if on-sale}
<del>{regular-price}</del>
<strong>{price}</strong>
<span class="label label-success">Save {you-save} ({you-save-percent}%)</span>
{if:else}
{price}
{/if}
{/if}
</p>
{if play-in-store}<p class="play">Play this in the café!</p>{/if}
{/if}
</a>
</li>
{if count == total_results}
</ul>
{if has-pagination}
<div class="pagination">
<p>Page {current-page} of {total-pages} pages:</p>
<ul>{pagination-links}</ul>
</div>
{/if}
{/if}
{/exp:stash:get_list}
{if:else}
<p>Sorry, we didn't find anything matching your search. Please try searching again. If you still can't find what you're looking for, please contact us: <a href="mailto:{lv_email}">{lv_email}</a>.
</p>
{/if}
{/stash:content}
{/exp:stash:set}
{/case}
{!-- ======================================================================== --}
{/exp:switchee}
{embed=site/view}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment