aboutsummaryrefslogtreecommitdiffstats
path: root/include/smarty/libs/sysplugins/smarty_internal_runtime_subtemplate.php
blob: 2c7623f93d77886132247bd0297d8c5c019140e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php

/**
 * Sub Template Runtime Methods render, setupSubTemplate
 *
 * @package    Smarty
 * @subpackage PluginsInternal
 * @author     Uwe Tews
 *
 **/
class Smarty_Internal_Runtime_SubTemplate
{

    /**
     * Subtemplate template object cache
     *
     * @var Smarty_Internal_Template[]
     */
    public $tplObjects = array();

    /**
     * Subtemplate call count
     *
     * @var int[]
     */
    public $subTplInfo = array();

    /**
     * Runtime function to render subtemplate
     *
     * @param \Smarty_Internal_Template $parent
     * @param string                    $template       template name
     * @param mixed                     $cache_id       cache id
     * @param mixed                     $compile_id     compile id
     * @param integer                   $caching        cache mode
     * @param integer                   $cache_lifetime life time of cache data
     * @param array                     $data           passed parameter template variables
     * @param int                       $scope          scope in which {include} should execute
     * @param bool                      $forceTplCache  cache template object
     * @param string                    $uid            file dependency uid
     * @param string                    $content_func   function name
     *
     */
    public function render(Smarty_Internal_Template $parent, $template, $cache_id, $compile_id, $caching,
                           $cache_lifetime, $data, $scope, $forceTplCache, $uid = null, $content_func = null)
    {
        // if there are cached template objects calculate $templateID
        $_templateId =
            !empty($this->tplObjects) ? $parent->smarty->_getTemplateId($template, $cache_id, $compile_id, $caching) :
                null;
        // already in template cache?
        /* @var Smarty_Internal_Template $tpl */
        if (isset($_templateId) && isset($this->tplObjects[$_templateId])) {
            // clone cached template object because of possible recursive call
            $tpl = clone $this->tplObjects[$_templateId];
            $tpl->parent = $parent;
            // if $caching mode changed the compiled resource is invalid
            if ((bool) $tpl->caching !== (bool) $caching) {
                unset($tpl->compiled);
            }
            // get variables from calling scope
            $tpl->tpl_vars = $parent->tpl_vars;
            $tpl->config_vars = $parent->config_vars;
            // get template functions
            $tpl->tpl_function = $parent->tpl_function;
            // copy inheritance object?
            if (isset($parent->ext->_inheritance)) {
                $tpl->ext->_inheritance = $parent->ext->_inheritance;
            } else {
                unset($tpl->ext->_inheritance);
            }
        } else {
            $tpl = clone $parent;
            $tpl->parent = $parent;
            if (!isset($tpl->templateId) || $tpl->templateId !== $_templateId) {
                $tpl->templateId = $_templateId;
                $tpl->template_resource = $template;
                $tpl->cache_id = $cache_id;
                $tpl->compile_id = $compile_id;
                if (isset($uid)) {
                    // for inline templates we can get all resource information from file dependency
                    if (isset($tpl->compiled->file_dependency[$uid])) {
                        list($filepath, $timestamp, $resource) = $tpl->compiled->file_dependency[$uid];
                        $tpl->source =
                            new Smarty_Template_Source(isset($tpl->smarty->_cache['resource_handlers'][$resource]) ?
                                                           $tpl->smarty->_cache['resource_handlers'][$resource] :
                                                           Smarty_Resource::load($tpl->smarty, $resource), $tpl->smarty,
                                                       $filepath, $resource, $filepath);
                        $tpl->source->filepath = $filepath;
                        $tpl->source->timestamp = $timestamp;
                        $tpl->source->exists = true;
                        $tpl->source->uid = $uid;
                    } else {
                        $tpl->source = null;
                    }
                } else {
                    $tpl->source = null;
                }
                if (!isset($tpl->source)) {
                    $tpl->source = Smarty_Template_Source::load($tpl);
                    unset($tpl->compiled);
                }
                unset($tpl->cached);
            }
        }
        $tpl->caching = $caching;
        $tpl->cache_lifetime = $cache_lifetime;
        if ($caching == 9999) {
            $tpl->cached = $parent->cached;
        }
        // set template scope
        $tpl->scope = $scope;
        $scopePtr = false;
        if ($scope & ~Smarty::SCOPE_BUBBLE_UP) {
            if ($scope == Smarty::SCOPE_GLOBAL) {
                $tpl->tpl_vars = Smarty::$global_tpl_vars;
                $tpl->config_vars = $tpl->smarty->config_vars;
                $scopePtr = true;
            } else {
                if ($scope == Smarty::SCOPE_PARENT) {
                    $scopePtr = $parent;
                } elseif ($scope == Smarty::SCOPE_SMARTY) {
                    $scopePtr = $tpl->smarty;
                } else {
                    $scopePtr = $tpl;
                    while (isset($scopePtr->parent)) {
                        if ($scopePtr->parent->_objType != 2 && $scope & Smarty::SCOPE_TPL_ROOT) {
                            break;
                        }
                        $scopePtr = $scopePtr->parent;
                    }
                }
                $tpl->tpl_vars = $scopePtr->tpl_vars;
                $tpl->config_vars = $scopePtr->config_vars;
            }
        }

        if (!isset($this->tplObjects[$tpl->_getTemplateId()]) && !$tpl->source->handler->recompiled) {
            // if template is called multiple times set flag to to cache template objects
            $forceTplCache = $forceTplCache ||
                (isset($this->subTplInfo[$tpl->template_resource]) && $this->subTplInfo[$tpl->template_resource] > 1);
            // check if template object should be cached
            if ($tpl->parent->_objType == 2 && isset($this->tplObjects[$tpl->parent->templateId]) ||
                ($forceTplCache && $tpl->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_AUTOMATIC) ||
                ($tpl->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON)
            ) {
                $this->tplObjects[$tpl->_getTemplateId()] = $tpl;
            }
        }

        if (!empty($data)) {
            // set up variable values
            foreach ($data as $_key => $_val) {
                $tpl->tpl_vars[$_key] = new Smarty_Variable($_val);
            }
        }
        if (isset($uid)) {
            if ($parent->smarty->debugging) {
                $parent->smarty->_debug->start_template($tpl);
                $parent->smarty->_debug->start_render($tpl);
            }
            $tpl->compiled->getRenderedTemplateCode($tpl, $content_func);
            if ($parent->smarty->debugging) {
                $parent->smarty->_debug->end_template($tpl);
                $parent->smarty->_debug->end_render($tpl);
            }
            if ($tpl->caching == 9999 && $tpl->compiled->has_nocache_code) {
                $parent->cached->hashes[$tpl->compiled->nocache_hash] = true;
            }
        } else {
            if (isset($tpl->compiled)) {
                $tpl->compiled->render($tpl);
            } else {
                $tpl->render();
            }
        }
        if ($scopePtr) {
            if ($scope == Smarty::SCOPE_GLOBAL) {
                Smarty::$global_tpl_vars = $tpl->tpl_vars;
                $tpl->smarty->config_vars = $tpl->config_vars;
            } else {
                $scopePtr->tpl_vars = $tpl->tpl_vars;
                $scopePtr->config_vars = $tpl->config_vars;
            }
        }
    }

    /**
     * Get called subtemplates from compiled template and save call count
     *
     * @param \Smarty_Internal_Template $tpl
     */
    public function registerSubTemplates(Smarty_Internal_Template $tpl)
    {
        foreach ($tpl->compiled->includes as $name => $count) {
            if (isset($this->subTplInfo[$name])) {
                $this->subTplInfo[$name] += $count;
            } else {
                $this->subTplInfo[$name] = $count;
            }
        }
    }
}