/**
* Method for template_post_parse hook
*
* @param string Parsed template string
* @param bool Whether an embed or not
* @param integer Site ID
* @return string Template string
*/
public function template_post_parse($template, $sub, $site_id)
{
// play nice with other extensions on this hook
if (isset($this->EE->extensions->last_call) && $this->EE->extensions->last_call) {
$template = $this->EE->extensions->last_call;
}
// is this the final template?
if ($sub == FALSE) {
// check the cache for postponed tags
if (!isset($this->EE->session->cache['stash']['__template_post_parse__'])) {
$this->EE->session->cache['stash']['__template_post_parse__'] = array();
}
// an array of tags needing to be post-parsed
$cache = $this->EE->session->cache['stash']['__template_post_parse__'];
// are we capturing the final output of the rendered EE host template?
$save_output = FALSE;
// run any postponed stash tags
if (!empty($cache)) {
$context = '';
if (!class_exists('Stash')) {
include_once PATH_THIRD . 'stash/mod.stash.php';
} else {
// get static context if it has been set
$context = Stash::$context;
}
// save TMPL values for later
$tagparams = isset($this->EE->TMPL->tagparams) ? $this->EE->TMPL->tagparams : array();
$tagdata = isset($this->EE->TMPL->tagdata) ? $this->EE->TMPL->tagdata : '';
// reset tagparams so Stash is instantiated with default values
$this->EE->TMPL->tagparams = array();
// instantiate but don't initialise
$s = new Stash(TRUE);
// sort by priority
$cache = $s->sort_by_key($cache, 'priority', 'sort_by_integer');
// loop through, prep the Stash instance, call the postponed tag and replace output into the placeholder
foreach ($cache as $placeholder => $tag) {
// make sure there is a placeholder in the template
// it may have been removed by advanced conditional processing
if (strpos($template, $placeholder) !== FALSE) {
$this->EE->TMPL->log_item("Stash: post-processing tag: " . $tag['tagproper'] . " will be replaced into " . LD . $placeholder . RD);
$this->EE->TMPL->tagparams = $tag['tagparams'];
$this->EE->TMPL->tagdata = $tag['tagdata'];
// restore context @ pointer in context parameter
if (isset($this->EE->TMPL->tagparams['context']) && $this->EE->TMPL->tagparams['context'] == '@') {
$this->EE->TMPL->tagparams['context'] = $context;
}
// restore context @ pointer if hardcoded in name parameter
if (isset($this->EE->TMPL->tagparams['name']) && strncmp($this->EE->TMPL->tagparams['name'], '@:', 2) == 0) {
$this->EE->TMPL->tagparams['name'] = str_replace('@', $context, $this->EE->TMPL->tagparams['name']);
}
// restore context @ pointer if hardcoded in file_name parameter
if (isset($this->EE->TMPL->tagparams['file_name']) && strncmp($this->EE->TMPL->tagparams['file_name'], '@:', 2) == 0) {
$this->EE->TMPL->tagparams['file_name'] = str_replace('@', $context, $this->EE->TMPL->tagparams['file_name']);
}
// has the save_output tag been called?
if ($tag['method'] === 'save_output') {
$save_output = $tag;
$save_output['placeholder'] = $placeholder;
} else {
// initialise Stash with our custom tagparams
$s->init(TRUE);
$out = $s->{$tag['method']}();
$template = str_replace(LD . $placeholder . RD, $out, $template);
// remove the placeholder from the cache so we don't iterate over it in future calls of this hook
unset($this->EE->session->cache['stash']['__template_post_parse__'][$placeholder]);
}
}
}
// restore original TMPL values
$this->EE->TMPL->tagparams = $tagparams;
$this->EE->TMPL->tagdata = $tagdata;
}
// cache output to a static file
if ($save_output) {
$this->EE->TMPL->tagparams = $save_output['tagparams'];
$s->init(TRUE);
$template = str_replace(LD . $save_output['placeholder'] . RD, '', $template);
$s->{$save_output['method']}($template);
// restore original TMPL values
$this->EE->TMPL->tagparams = $tagparams;
}
// cleanup
unset($cache);
}
return $template;
}