Stash::embed PHP Method

embed() public method

Embed a Stash template file in the current template
public embed ( ) : string
return string
    public function embed()
    {
        /* Sample use
           ---------------------------------------------------------
           {stash:embed name="my_template" 
               context="my_template_folder" 
               process="start"
               stash:another_var1="value 1"
               stash:another_var2="value 2"
           }
           
           Alternative sytax:
           {stash:embed:name} or
           {stash:embed:context:name}
           --------------------------------------------------------- */
        // mandatory parameter values for template files
        $this->EE->TMPL->tagparams['file'] = 'yes';
        $this->EE->TMPL->tagparams['embed_vars'] = array();
        // parse="yes"?
        $this->set_parse_params();
        // default parameter values
        $this->EE->TMPL->tagparams['save'] = $this->EE->TMPL->fetch_param('save', 'yes');
        $this->EE->TMPL->tagparams['scope'] = $this->EE->TMPL->fetch_param('scope', 'site');
        $this->EE->TMPL->tagparams['parse_tags'] = $this->EE->TMPL->fetch_param('parse_tags', 'yes');
        $this->EE->TMPL->tagparams['parse_vars'] = $this->EE->TMPL->fetch_param('parse_vars', 'yes');
        $this->EE->TMPL->tagparams['parse_conditionals'] = $this->EE->TMPL->fetch_param('parse_conditionals', 'yes');
        // name and context passed in tagparts?
        if (isset($this->EE->TMPL->tagparts[3])) {
            $this->EE->TMPL->tagparams['context'] = $this->EE->TMPL->tagparts[2];
            $this->EE->TMPL->tagparams['name'] = $this->EE->TMPL->tagparts[3];
        } elseif (isset($this->EE->TMPL->tagparts[2])) {
            $this->EE->TMPL->tagparams['name'] = $this->EE->TMPL->tagparts[2];
        }
        // default to processing embeds at end
        $this->EE->TMPL->tagparams['process'] = $this->EE->TMPL->fetch_param('process', 'end');
        // is this a static template?
        if ($this->EE->TMPL->tagparams['process'] !== 'static') {
            // non-static templates are assigned to the template bundle by default
            $this->EE->TMPL->tagparams['bundle'] = $this->EE->TMPL->fetch_param('bundle', 'template');
            // by default, parse the template when it is retrieved from the database (like a standard EE embed)
            $this->EE->TMPL->tagparams['parse_stage'] = $this->EE->TMPL->fetch_param('parse_stage', 'get');
        } else {
            // mandatory params for static templates
            $this->EE->TMPL->tagparams['bundle'] = 'static';
            // must be assigned to the static bundle
            $this->EE->TMPL->tagparams['process'] = 'end';
            $this->EE->TMPL->tagparams['context'] = "@URI";
            // must be in the context of current URI
            $this->EE->TMPL->tagparams['parse_stage'] = "set";
            // static templates must be pre-parsed
            $this->EE->TMPL->tagparams['refresh'] = "0";
            // static templates can never expire
            // as this is the full rendered output of a template, check that we should really be saving it
            if (!$this->_is_cacheable()) {
                $this->EE->TMPL->tagparams['save'] = 'no';
                self::$_nocache = FALSE;
                // remove {stash:nocache} pairs
            }
        }
        // set default parameter values for template files
        // set a parse depth of 4
        $this->EE->TMPL->tagparams['parse_depth'] = $this->EE->TMPL->fetch_param('parse_depth', 4);
        // don't replace the variable by default (only read from file once)
        // note: file syncing can be forced by setting stash_file_sync = TRUE in config
        $this->EE->TMPL->tagparams['replace'] = $this->EE->TMPL->fetch_param('replace', 'no');
        // set priority to 0 by default, so that embeds come before post-processed variables
        $this->EE->TMPL->tagparams['priority'] = $this->EE->TMPL->fetch_param('priority', '0');
        // initialise?
        $init = (bool) preg_match('/1|on|yes|y/i', $this->EE->TMPL->fetch_param('init', 'yes'));
        // re-initialise parameters, unless disabled by init parameter
        if ($init) {
            $this->init();
        } else {
            $this->process = 'inline';
        }
        // save stash embed vars passed as parameters in the form stash:my_var which we'll
        // inject later into the stash array for replacement, so remove the stash: prefix
        $params = $this->EE->TMPL->tagparams;
        foreach ($params as $key => $val) {
            if (strncmp($key, 'stash:', 6) == 0) {
                $this->EE->TMPL->tagparams['embed_vars'][substr($key, 6)] = $val;
            }
        }
        // permitted parameters for embeds
        $reserved_vars = array('name', 'context', 'scope', 'file', 'file_name', 'parse_stage', 'save', 'refresh', 'replace', 'parse_tags', 'parse_depth', 'parse_vars', 'parse_conditionals', 'process', 'priority', 'output', 'embed_vars', 'bundle', 'prefix', 'trim', 'strip_tags', 'strip_curly_braces', 'strip_unparsed', 'compress', 'backspace', 'strip');
        return $this->_run_tag('get', $reserved_vars);
    }