Skip to content

Instantly share code, notes, and snippets.

@croxton
Created October 29, 2012 14:23
Show Gist options
  • Save croxton/3973821 to your computer and use it in GitHub Desktop.
Save croxton/3973821 to your computer and use it in GitHub Desktop.
Stash & template partials - nested embed

Required:

Stash 2.3.4 (beta) or later

Stash template directory

	/layouts/
		standard.html

	/partials/
		listing.html
		article.html

viewModel template

An EE template, e.g. blog/index

	{stash:embed:layouts:standard} 

	{!--  ============================================
	CACHE BREAKING
	================================================== --}
	{exp:switchee variable="{segment_3}" parse="inward"}
		{!-- clear the cached comments variable when a new comment is submitted --}
		{case value="comment_posted"}
			{exp:stash:unset name="comments:{segment_2}" scope="site"}
		{/case}
	{/exp:switchee}

	{exp:switchee variable="{segment_2}" parse="inward"}

		{!--  ============================================
		LISTINGS
		================================================== --}
		{case value="''|category"}
	
			{exp:stash:set_value name="content" value="{exp:stash:embed:partials:listing}"}
			{exp:stash:set_value name="title" value="Articles"}
	
			{!-- cache the list of articles in a given category for 60 minutes --}
		   	{exp:stash:set_list 
				name="articles:{segment_3}" 
				parse_tags="yes" 
				save="yes" 
				scope="site" 
				refresh="60" 
				replace="no"
			}
				{exp:channel:entries channel="articles" limit="15" disable="member_data|pagination"}   
					{stash:article_url}{url_title_path=articles}{/stash:article_url}
					{stash:article_title}{title}{/stash:article_title}
				{/exp:channel:entries} 
			{exp:stash:set_list}
	
		{/case}

		{!--  ============================================
		SINGLE ENTRY
		================================================== --}
	 	{case default="Yes"}

			{exp:stash:set_value name="content" value="{exp:stash:embed:partials:article}"}
			{exp:stash:set_value name="url_title" value="{segment_2}"}
			
			{!--set the overall page title --}
			{exp:stash:set name="title" trim="yes"}
				{exp:stash:get_list name="article:{segment_2}" scope="site"}{title}{/exp:stash:get_list}
			{/exp:stash:set}
	
			{!-- cache the article content for a day, identify by url_title (segment_2) --}
			{exp:stash:set_list 
				name="article:{segment_2}" 
				parse_tags="yes" 
				save="yes" 
				scope="site" 
				refresh="1440" 
				replace="no"
			}
				{exp:channel:entries limit="1" channel="articles" disable="member_data|pagination" require_entry="yes"} 
			    	{stash:title}{title}{/stash:title}
			    	{stash:entry_id}{entry_id}{/stash:entry_id}
			    	{stash:body}{articles_body}{/stash:body}
				{stash:allow_comments}{allow_comments}{/stash:allow_comments}   
				{/exp:channel:entries}

				{if switchee_no_results}{redirect="404"}{/if}
			{/exp:stash:set_list}
		
			{!-- cache the list of comments for a 60 minutes, identify by url_title (segment_2) --}
			{exp:stash:set_list 
				name="comments:{segment_2}" 
				parse_tags="yes" 
				save="yes" 
				scope="site" 
				refresh="60" 
				replace="no"
			}
				{exp:comment:entries}
			     	{stash:author}{name}{/stash:author}
					{stash:time_machine}{comment_date format='{dt_machine}'}{/stash:time_machine}
					{stash:time_human}{comment_date format="{dt_human}"}{/stash:time_human}
					{stash:comment}{comment}{/stash:comment}
			   {/exp:comment:entries} 
			{/exp:stash:set_list}

	 	{/case}

	{/exp:switchee}

partials/listing.html

	{exp:stash:get_list name="articles:{segment_3}"}   
	   {if count == 1}<h2>Recent Articles {if segment_3}filed under {segment_3_category_name}{/if}</h2>{/if}
	   <article class="listing summary"><a href="{article_url}">{article_title}</a></article>
	   {if no_results}No articles in this category{/if}
	{/exp:stash:get_list} 

partials/article.html

	{exp:stash:get_list name="article:{stash:url_title}" scope="site"}

		{body}

		{if "{allow_comments}" == "y"}
	
			<section class="comments">        
			<h2>Comments</h2>

			{exp:stash:get_list:nested name="comments:{stash:url_title}" scope="site" prefix="comments"}
			 {if comments:no_results}<p>There are no comments yet</p>{/if}
			 <article class="comment">     
			  <h3>{author}</h3>
			  <time datetime="{time_machine}">{time_human}</time>     
			  {comment}
			 </article>
			{/exp:stash:get_list:nested} 
			</section>

			{!-- comment form needs to remain un-cached, if you want to populate with logged-in users' details --}
			{exp:comment:form 
				channel="articles"
				entry_id="{entry_id}"
				return="/{segment_1}/{stash:url_title}/comment_posted"
			}
			 <label for="name">Name:</label>
			 <input type="text" value="" name="name"><br>

			 <label for="email">Email:</label>
			 <input type="text" value="" name="email"><br>

			 <label for="url">Website:</label>
			 <input type="text" value="http://" name="url"><br>

			 <label for="comment">Comments:</label>
			 <textarea rows="10" cols="50" name="comment"></textarea>

			 <input type="submit" value="Post Comment" name="submit">
			{/exp:comment:form}
		
		{/if}
	
	{/exp:stash:get_list}

layouts/standard.html

	<!DOCTYPE html>
	<html>
		<head>
			<title>{stash:title}</title>
		</head>
		<body>		
			<h1>{stash:title}</h1>
			{stash:content}
		</body>
	</html>

snippets

dt_machine

	%Y-%m-%d

dt_human

	%j<sup>%S</sup> %F
@since1976
Copy link

Just a note for anybody copying this code, {stash_list name="articles:{segment_3}" is missing a closing / on it's closing tag pair.

@nifl
Copy link

nifl commented Feb 6, 2015

Thanks for that @since1976. Maybe the syntax has changed since the previous comment but the closing / is still missing on the first instance of {exp:stash:set_list ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment