Box\Brainy\Templates\TemplateBase::display PHP Method

display() public method

This displays the contents of a template. To return the contents of a template into a variable, use the fetch() method instead. As an optional second and third parameter, you can pass a cache ID and compile ID. A fourth parameter can be passed which passes the parent scope that the template should use.
public display ( string | null | void $template = null, string | null | void $compile_id = null ) : void
$template string | null | void the resource handle of the template file or template object
$compile_id string | null | void compile id to be used with this template
return void
    public function display($template = null, $compile_id = null)
    {
        if ($template === null && $this instanceof Template) {
            $template = $this;
        }
        // create template object if necessary
        if (!$template instanceof Template) {
            $template = $this->smarty->createTemplate($template, $compile_id, $this);
        }
        // Add the smarty variable, if needed.
        // We check is_array() because tpl_vars might be an OverlayScope, which
        // will always have its parent's smarty variable.
        if (is_array($template->tpl_vars) && !isset($template->tpl_vars['smarty'])) {
            $template->tpl_vars['smarty'] = array('blocks' => array(), 'functions' => array(), 'foreach' => array(), 'ls_loadables' => array());
        }
        if (!empty(Brainy::$global_tpl_vars)) {
            foreach (Brainy::$global_tpl_vars as $key => $value) {
                $template->tpl_vars[$key] =& $value;
            }
        }
        // get rendered template
        // checks if template exists
        if (!$template->source->exists) {
            $parent_resource = '';
            if ($template->parent instanceof Template) {
                $parent_resource = " in '{$template->parent->template_resource}'";
            }
            throw new SmartyException("Unable to load template {$template->source->type} '{$template->source->name}'{$parent_resource}");
        }
        $_smarty_tpl = $template;
        // read from cache or render
        if ($template->source->recompiled) {
            // recompiled === 'eval'
            $code = $template->compileTemplateSource();
            eval('?>' . $code);
            // The closing PHP bit accounts for the opening PHP tag at the top of the compiled file
            unset($code);
        } else {
            if (!$template->compiled->exists || $template->smarty->force_compile && !$template->compiled->isCompiled) {
                $template->compileTemplateSource();
            }
            if (!$template->compiled->loaded) {
                $template->compiled->load($template);
                // If the template source's timestamp is greater than that of
                // the timestamp on its stored dependency, we will not be able
                // to decode the properties of the compiled template. Thus the
                // template needs to be recompiled here.
                if (empty($template->properties['unifunc']) || !is_callable($template->properties['unifunc'])) {
                    $template->recompileAndLoadCopy();
                }
            } else {
                $template->decodeProperties($template->compiled->properties, false);
            }
            if (empty($template->properties['unifunc']) || !is_callable($template->properties['unifunc'])) {
                throw new SmartyException("Invalid compiled template for '{$template->template_resource}': no unifunc found");
            }
            // render compiled template
            call_user_func($template->properties['unifunc'], $template);
        }
        if (!$template->source->recompiled && empty($template->properties['file_dependency'][$template->source->uid])) {
            $template->properties['file_dependency'][$template->source->uid] = array($template->source->filepath, $template->source->timestamp, $template->source->type);
        }
        if ($template->parent instanceof Template) {
            $template->parent->properties['file_dependency'] = array_merge($template->parent->properties['file_dependency'], $template->properties['file_dependency']);
            foreach ($template->required_plugins as $code => $tmp1) {
                foreach ($tmp1 as $name => $tmp) {
                    foreach ($tmp as $type => $data) {
                        $template->parent->required_plugins[$code][$name][$type] = $data;
                    }
                }
            }
        }
    }