1: <?php
 2: /**
 3:  * includeq Smarty compiler plug-in
 4:  *
 5:  * See the enclosed file LICENSE for licensing information.
 6:  * If you did not receive this file, get it at http://www.gnu.org/licenses/gpl-2.0.html
 7:  *
 8:  * @copyright    (c) 2000-2016 XOOPS Project (www.xoops.org)
 9:  * @license          GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
10:  * @author           Skalpa Keo <skalpa@xoops.org>
11:  * @package          xos_opal
12:  * @subpackage       xos_opal_Smarty
13:  * @since            2.0.14
14:  */
15: 
16: /**
17:  * Quick include template plug-in
18:  *
19:  * Like {@link smarty_compiler_foreachq() foreachq}, this plug-in has been written to provide
20:  * a faster version of an already existing Smarty function. <var>includeq</var> can be used
21:  * as a replacement for the Smarty
22:  * {@link http://smarty.php.net/manual/en/language.function.include.php include} function as long
23:  * as you are aware of the differences between them.
24:  *
25:  * Normally, when you include a template, Smarty does the following:
26:  * - Backup all your template variables in an array
27:  * - Include the template you specified
28:  * - Restore the template variables from the previously created backup array
29:  *
30:  * The advantage of this method is that it makes the main template variables <i>safe</i>: if your
31:  * main template uses a variable called <var>$stuff</var> and the included template modifies it
32:  * value, the main template will recover the original value automatically.
33:  *
34:  * While this can be useful in some cases (for example, when you include templates you have absolutely
35:  * no control over), some may consider this a limitation and it has the disadvantage of slowing down
36:  * the inclusion mechanism a lot.
37:  *
38:  * <var>includeq</var> fixes that: the code it generates doesn't contain the variables backup/recovery
39:  * mechanism and thus makes templates inclusion faster. Note that however, this new behavior may
40:  * create problems in some cases (but you can prevent them most of the times, for example by always
41:  * using a <var>tmp_</var> prefix for the variables you create in included templates looping sections).
42:  * @param $tag_args
43:  * @param $comp
44:  * @return string
45:  */
46: function smarty_compiler_includeq($tag_args, &$comp)
47: {
48:     $attrs    = $comp->_parse_attrs($tag_args);
49:     $arg_list = array();
50: 
51:     if (empty($attrs['file'])) {
52:         $comp->_syntax_error("missing 'file' attribute in includeq tag", E_USER_ERROR, __FILE__, __LINE__);
53:     }
54: 
55:     foreach ($attrs as $arg_name => $arg_value) {
56:         if ($arg_name === 'file') {
57:             $include_file = $arg_value;
58:             continue;
59:         } elseif ($arg_name === 'assign') {
60:             $assign_var = $arg_value;
61:             continue;
62:         }
63:         if (is_bool($arg_value)) {
64:             $arg_value = $arg_value ? 'true' : 'false';
65:         }
66:         $arg_list[] = "'$arg_name' => $arg_value";
67:     }
68: 
69:     $output = '';
70: 
71:     if (isset($assign_var)) {
72:         $output .= "ob_start();\n";
73:     }
74: 
75:     //$output .= "\$_smarty_tpl_vars = \$this->_tpl_vars;\n";
76:     $_params = "array('smarty_include_tpl_file' => " . $include_file . ", 'smarty_include_vars' => array(" . implode(',', (array)$arg_list) . '))';
77:     $output .= "\$this->_smarty_include($_params);\n";
78:     //"\$this->_tpl_vars = \$_smarty_tpl_vars;\n" .
79:     //"unset(\$_smarty_tpl_vars);\n";
80: 
81:     if (isset($assign_var)) {
82:         $output .= "\$this->assign(" . $assign_var . ", ob_get_contents()); ob_end_clean();\n";
83:     }
84: 
85:     //$output .= '';
86:     return $output;
87: }
88: