Skip to content

Instantly share code, notes, and snippets.

@jeromecoupe
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeromecoupe/11380666 to your computer and use it in GitHub Desktop.
Save jeromecoupe/11380666 to your computer and use it in GitHub Desktop.
Cache an entire page but escape an embed used for relationships (in case one of the related entry changes or some are removed / added)
{!-- Stash page caching https://github.com/croxton/Stash/wiki/Caching --}
{exp:stash:cache bundle="news"}
{!-- layout used --}
{stash:embed name="layouts:page"}
{exp:channel:entries
channel="news"
status="open"
disable="member_data|pagination"
limit="1"
require_entry="yes"
}
{!-- 404 --}
{if no_results}
{redirect="404"}
{/if}
{!-- meta data --}
{exp:stash:set name="st_htmltitle"}{title}{/exp:stash:set}
{exp:stash:set name="st_htmldescription" strip_tags="yes"}{exp:low_replace find="QUOTE" replace="""}{news_summary}{/exp:low_replace}{/exp:stash:set}
{exp:stash:set name="st_currentnav"}newspress{/exp:stash:set}
{!-- build entry variables --}
{exp:stash:set name="st_item_id"}{entry_id}{/exp:stash:set}
{exp:stash:set name="st_item_title"}{title}{/exp:stash:set}
{exp:stash:set name="st_item_url"}{url_title_path="news_press/news_detail"}{/exp:stash:set}
{exp:stash:set name="st_item_date"}{entry_date format="%F %j, %Y"}{/exp:stash:set}
{exp:stash:set name="st_item_machinedate"}{entry_date format="%Y-%m-%d"}{/exp:stash:set}
{exp:stash:set_list:categories name="st_item_categories" parse_tags="yes"}
{categories show_group="1"}
{stash:st_item_caturl}{path="news_press/news"}{/stash:st_item_caturl}
{stash:st_item_catname}{category_name}{/stash:st_item_catname}
{/categories}
{/exp:stash:set_list:categories}
{exp:stash:set name="st_item_img"}{news_img}{/exp:stash:set}
{exp:stash:set name="st_item_img_caption"}{news_img_caption}{/exp:stash:set}
{exp:stash:set name="st_item_img_copyright"}{news_img_copyright}{/exp:stash:set}
{exp:stash:set name="st_item_summary"}{news_summary}{/exp:stash:set}
{exp:stash:set name="st_item_body"}{news_body}{/exp:stash:set}
{if news_author != ""}
{exp:stash:set name="st_item_author"}{news_author}{/exp:stash:set}
{if:else}
{exp:stash:set name="st_item_author"}IPF{/exp:stash:set}
{/if}
{/exp:channel:entries}
{!-- build page content --}
{exp:stash:set name="content"}
<main class="pagecontent">
<p class="imgholder">{exp:ce_img:pair src="{exp:stash:st_item_img}" width="920" height="517" crop="yes|center,center" allow_scale_larger="yes" parse="inward"}<img src="{made}" alt="{exp:stash:st_item_img_caption} - &copy; {exp:stash:st_item_img_copyright}" class="fluidimg" />{/exp:ce_img:pair}</p>
<p class="item-meta"><time datetime="{exp:stash:st_item_machinedate}">{exp:stash:st_item_date}</time> in <a href="{path='news_press/news'}">News</a></p>
<h1 class="page-title page-title--notop">{exp:stash:st_item_title}</h1>
<div class="intro">
{exp:stash:st_item_summary}
</div>
<div class="grid">
<div class="pagecontent__primary grid__unit large-unit-8of12 wysiwyg">
{exp:stash:st_item_body}
<div class="iteminfo">
<p class="iteminfo__author">Author: {exp:stash:st_item_author}</p>
<p class="iteminfo__media">Picture: {exp:stash:st_item_img_caption} - &copy;&nbsp;{exp:stash:st_item_img_copyright}</p>
</div>
</div><!--
--><div class="pagecontent__secondary grid__unit large-unit-4of12">
{!-- include related items module: same for all item types, just passing the father entry_id and using playa children to get related items --}
{!-- THIS IS THE ONE I'D LIKE TO NOT CACHE. Surrounding it with {stash:nocache} did not work for me --}
{exp:stash:embed name="embeds:mod_relationships" stash:item_id="{exp:stash:st_item_id}"}
</div>
</div>
</main>
{/exp:stash:set}
{/exp:stash:cache}
@croxton
Copy link

croxton commented Apr 29, 2014

Assuming you wrap the embed with {stash:nocache} you will find that on subsequent views of your template the value of {exp:stash:st_item_id} does not get set. You need to un-escape this variable so that it's value is cached the first time the template is viewed:

{stash:nocache}
    {exp:stash:embed name="embeds:mod_relationships" stash:item_id="{/stash:nocache}{exp:stash:st_item_id}{stash:nocache}" parse="inward"}
{/stash:nocache}

@jeromecoupe
Copy link
Author

::lightbulb::

Now it makes sense. You want that variable to BE cached for this to work, of course. Otherwise that variable is not cached and thus not available on subsequent page loads. Never would have though about doing it this way, though.

Thank you for taking the time to explain this thoroughly.

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