Stash::init PHP Method

init() public method

Initialise tag parameters
public init ( boolean $calling_from_hook = FALSE ) : void
$calling_from_hook boolean Is method being called by an extension hook?
return void
    public function init($calling_from_hook = FALSE)
    {
        // make sure we have a Template object to work with, in case Stash is being invoked outside of a template
        if (!class_exists('EE_Template')) {
            $this->_load_EE_TMPL();
        }
        // initialise internal flags
        $this->parse_complete = FALSE;
        $this->_update = FALSE;
        $this->_append = TRUE;
        $this->_embed_nested = FALSE;
        $this->process = 'inline';
        // postpone the parsing of the called stash tag?
        if (FALSE === $calling_from_hook) {
            /* process stage:
                   start = called prior to template parsing in the current template
                   inline = process as a normal tag within the natural parse order of the template
                   end = called after all tag parsing has completed
               */
            $this->process = $this->EE->TMPL->fetch_param('process', 'inline');
            // start | inline | end
            $this->priority = $this->EE->TMPL->fetch_param('priority', '1');
            // ensure a priority is set
        }
        // legacy: make 'final' the same as 'end'
        if ($this->process == "final") {
            $this->process = "end";
        }
        // tags can't be processed on start, only stash embeds
        if ($this->process == "start") {
            $this->process = "inline";
        }
        // allow the site_id to be overridden, for e.g. shared variables across mutliple sites
        $this->site_id = (int) $this->EE->TMPL->fetch_param('site_id', $this->site_id);
        // selected bundle
        $bundle = $this->EE->TMPL->fetch_param('bundle', 'default');
        // lookup the id of an existing bundle, or map to one of the preset bundles
        if (!($this->bundle_id = $this->EE->stash_model->get_bundle_by_name($bundle))) {
            // not found, fallback to the default
            $this->bundle_id = 1;
        }
        // xss scripting protection
        $this->xss_clean = (bool) preg_match('/1|on|yes|y/i', $this->EE->TMPL->fetch_param('xss_clean'));
        // if the variable is already set, do we want to replace it's value? Default = yes
        $this->replace = (bool) preg_match('/1|on|yes|y/i', $this->EE->TMPL->fetch_param('replace', 'yes'));
        // parse="yes"?
        $this->set_parse_params();
        // do we want to parse any tags and variables inside tagdata? Default = no
        $this->parse_tags = (bool) preg_match('/1|on|yes|y/i', $this->EE->TMPL->fetch_param('parse_tags'));
        $this->parse_vars = $this->EE->TMPL->fetch_param('parse_vars', NULL);
        // legacy behaviour: if parse_vars is null but parse tags is true, we should make sure vars are parsed too
        if ($this->parse_tags && $this->parse_vars == NULL) {
            $this->parse_vars = TRUE;
        } else {
            $this->parse_vars = (bool) preg_match('/1|on|yes|y/i', $this->parse_vars);
        }
        // parsing: how many passes of the template should we make? (more passes = more overhead). Default = 1
        $this->parse_depth = preg_replace('/[^0-9]/', '', $this->EE->TMPL->fetch_param('parse_depth', 1));
        // parsing: parse advanced conditionals. Default = no
        $this->parse_conditionals = (bool) preg_match('/1|on|yes|y/i', $this->EE->TMPL->fetch_param('parse_conditionals'));
        // stash type, default to 'variable'
        $this->type = strtolower($this->EE->TMPL->fetch_param('type', 'variable'));
        // create a stash array in the session if we don't have one
        if (!array_key_exists('stash', $this->EE->session->cache)) {
            $this->EE->session->cache['stash'] = array();
        }
        // determine the memory storage location
        if ($this->type === 'variable') {
            // we're setting/getting a 'native' stash variable
            $this->_stash =& $this->EE->session->cache['stash'];
        } elseif ($this->type === 'snippet' || $this->type === 'global') {
            // we're setting/getting a global variable {snippet}
            $this->_stash =& $this->EE->config->_global_vars;
        } else {
            $this->EE->output->show_user_error('general', $this->EE->lang->line('unknown_stash_type') . $this->type);
        }
    }

Usage Example

コード例 #1
0
 /**
  * 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;
 }
All Usage Examples Of Stash::init