Skip to content

Instantly share code, notes, and snippets.

@croxton
Created March 20, 2012 13:21
Show Gist options
  • Save croxton/2135481 to your computer and use it in GitHub Desktop.
Save croxton/2135481 to your computer and use it in GitHub Desktop.
Stash embeds
I have a new tag {exp:stash:embed} which allows you to include Stash template files and store
in the database. Files can be fully or partially parsed when read before saving (set), OR
saved and then parsed on retrieval (get). Stash variables can be accessed in the Stash
template files as {stash:variable} and of course all the usual {global_variables} work as
expected.
An example:
{exp:stash:embed
name="news_listing"
refresh="10"
replace="no"
parse_stage="set"
}
It loads a file news_listing.html from the Stash template folder, parses the tags therein and
then saves the rendered code for 10 minutes before it gets regenerated. Subsequent views of
the same page within the 10 minutes load the saved version from the database rather than reading
from the file (reading from the filesystem is expensive, whereas a key/value retrieval from the
innodb table is very fast). Setting replace="yes" causes the cache to be regenerated on each
page view, which is handy for development.
Stash embeds are parsed at the end of current template parsing, like a standard EE embed. But
using the new 'process=' parameter you can change this to process the tag 'inline' (like an
ordinary tag) or 'final' - at the very end of all template parsing just before final output.
Stash embeds are process="end" by default. You can also use the new 'priority=' parameter to
determine the order in which postponed tags are processed - lower numbers are processed earlier.
As a bonus, Stash get and get_list tags also get the new 'process' and 'priority' parameters,
so now you can do stuff like this in the same template and not worry about parse order:
{exp:get name="my_var" process="end" priority="10"}
{exp:channel:entries}
{exp:stash:set name="my_var"}{title}{/exp:stash:set}
{/exp:channel:entries}
All this subversion of the EE parse order means that we should finally be able to do without
conventional embeds and the overhead they entail. Well that's the theory anyway :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment