mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-04 10:24:26 +02:00
update docs, fix smarty var compiling, allow any $smarty.*.$foo syntax,
add $`foobar` for embedded variables
This commit is contained in:
@@ -250,8 +250,12 @@ email: zaphod@slartibartfast.com<br></programlisting>
|
||||
<sect1 id="language.config.variables">
|
||||
<title>Variables loaded from config files</title>
|
||||
<para>
|
||||
Variables that are loaded from the config files are referenced by enclosing
|
||||
them within hash marks (#).
|
||||
Variables that are loaded from the config files are referenced by
|
||||
enclosing them within hash marks (#), or with the smarty variable
|
||||
<link
|
||||
linkend="language.variables.smarty.config">$smarty.config</link>.
|
||||
The second syntax is useful for embedding into quoted attribute
|
||||
values.
|
||||
</para>
|
||||
<example>
|
||||
|
||||
@@ -281,7 +285,24 @@ index.tpl:
|
||||
</body>
|
||||
</html>
|
||||
|
||||
OUTPUT:
|
||||
index.tpl: (alternate syntax)
|
||||
|
||||
{config_load file="foo.conf"}
|
||||
<html>
|
||||
<title>{$smarty.config.pageTitle}</title>
|
||||
<body bgcolor="{$smarty.config.bodyBgColor}">
|
||||
<table border="{$smarty.config.tableBorderSize}" bgcolor="{$smarty.config.tableBgColor}">
|
||||
<tr bgcolor="{$smarty.config.rowBgColor}">
|
||||
<td>First</td>
|
||||
<td>Last</td>
|
||||
<td>Address</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
OUTPUT: (same for both examples)
|
||||
|
||||
<html>
|
||||
<title>This is mine</title>
|
||||
@@ -383,6 +404,15 @@ OUTPUT:
|
||||
</para>
|
||||
</sect2>
|
||||
|
||||
<sect2 id="language.variables.smarty.config">
|
||||
<title>{$smarty.config}</title>
|
||||
<para>
|
||||
{$smarty} variable can be used to refer to loaded config variables.
|
||||
{$smarty.config.foo} is a synonyn for {#foo#}. See the section on
|
||||
<link linkend="language.function.config.load">config_load</link> for an example.
|
||||
</para>
|
||||
</sect2>
|
||||
|
||||
<sect2 id="language.variables.smarty.loops">
|
||||
<title>{$smarty.section}, {$smarty.foreach}</title>
|
||||
<para>
|
||||
|
@@ -511,13 +511,30 @@ $smarty->autoload_filters = array('pre' => array('trim', 'stamp'),
|
||||
<paramdef>string <parameter>varname</parameter></paramdef>
|
||||
<paramdef>mixed <parameter>var</parameter></paramdef>
|
||||
</funcprototype>
|
||||
<funcprototype>
|
||||
<funcdef>void <function>append</function></funcdef>
|
||||
<paramdef>string <parameter>varname</parameter></paramdef>
|
||||
<paramdef>mixed <parameter>var</parameter></paramdef>
|
||||
<paramdef>boolean <parameter>merge</parameter></paramdef>
|
||||
</funcprototype>
|
||||
</funcsynopsis>
|
||||
<para>
|
||||
This is used to append an element to an assigned array. If you append
|
||||
to a string value, it is converted to an array value and then
|
||||
appended to. You can explicitly pass name/value pairs, or associative
|
||||
arrays containing the name/value pairs.
|
||||
arrays containing the name/value pairs. If you pass the optional third
|
||||
parameter of true, the value will be merged with the current array
|
||||
instead of appended.
|
||||
</para>
|
||||
<note>
|
||||
<title>Technical Note</title>
|
||||
<para>
|
||||
The merge parameter respects array keys, so if you merge two
|
||||
numerically indexed arrays, they may overwrite each other or result in
|
||||
non-sequential keys. This is unlike the array_merge() function of PHP
|
||||
which wipes out numerical keys and renumbers them.
|
||||
</para>
|
||||
</note>
|
||||
<example>
|
||||
<title>append</title>
|
||||
<programlisting>
|
||||
@@ -537,10 +554,18 @@ $smarty->append(array("city" => "Lincoln","state" => "Nebraska"));</programlisti
|
||||
<paramdef>string <parameter>varname</parameter></paramdef>
|
||||
<paramdef>mixed <parameter>var</parameter></paramdef>
|
||||
</funcprototype>
|
||||
<funcprototype>
|
||||
<funcdef>void <function>append_by_ref</function></funcdef>
|
||||
<paramdef>string <parameter>varname</parameter></paramdef>
|
||||
<paramdef>mixed <parameter>var</parameter></paramdef>
|
||||
<paramdef>boolean <parameter>merge</parameter></paramdef>
|
||||
</funcprototype>
|
||||
</funcsynopsis>
|
||||
<para>
|
||||
This is used to append values to the templates by reference instead of
|
||||
making a copy. See the PHP manual on variable referencing for an explanation.
|
||||
making a copy. See the PHP manual on variable referencing for an
|
||||
explanation. If you pass the optional third parameter of true, the value
|
||||
will be merged with the current array instead of appended.
|
||||
</para>
|
||||
<note>
|
||||
<title>Technical Note</title>
|
||||
@@ -553,6 +578,15 @@ $smarty->append(array("city" => "Lincoln","state" => "Nebraska"));</programlisti
|
||||
workaround.
|
||||
</para>
|
||||
</note>
|
||||
<note>
|
||||
<title>Technical Note</title>
|
||||
<para>
|
||||
The merge parameter respects array keys, so if you merge two
|
||||
numerically indexed arrays, they may overwrite each other or result in
|
||||
non-sequential keys. This is unlike the array_merge() function of PHP
|
||||
which wipes out numerical keys and renumbers them.
|
||||
</para>
|
||||
</note>
|
||||
<example>
|
||||
<title>append_by_ref</title>
|
||||
<programlisting>
|
||||
@@ -936,6 +970,56 @@ $output = $smarty->fetch("index.tpl");
|
||||
// do something with $output here
|
||||
|
||||
echo $output;</programlisting>
|
||||
</example>
|
||||
</sect1>
|
||||
<sect1 id="api.get.config.vars">
|
||||
<title>get_config_vars</title>
|
||||
<funcsynopsis>
|
||||
<funcprototype>
|
||||
<funcdef>array <function>get_config_vars</function></funcdef>
|
||||
<paramdef>string <parameter><optional>varname</optional></parameter></paramdef>
|
||||
</funcprototype>
|
||||
</funcsynopsis>
|
||||
<para>
|
||||
This returns the given loaded config variable value. If no parameter
|
||||
is given, an array of all loaded config variables is returned.
|
||||
</para>
|
||||
<example>
|
||||
<title>get_config_vars</title>
|
||||
<programlisting>
|
||||
// get loaded config template var 'foo'
|
||||
$foo = $smarty->get_config_vars('foo');
|
||||
|
||||
// get all loaded config template vars
|
||||
$config_vars = $smarty->get_config_vars();
|
||||
|
||||
// take a look at them
|
||||
print_r($config_vars);</programlisting>
|
||||
</example>
|
||||
</sect1>
|
||||
<sect1 id="api.get.registered.object">
|
||||
<title>get_registered_object</title>
|
||||
<funcsynopsis>
|
||||
<funcprototype>
|
||||
<funcdef>array <function>get_registered_object</function></funcdef>
|
||||
<paramdef>string <parameter>object_name</parameter></paramdef>
|
||||
</funcprototype>
|
||||
</funcsynopsis>
|
||||
<para>
|
||||
This returns a reference to a registered object. This is useful
|
||||
from within a custom function when you need direct access to a
|
||||
registered object.
|
||||
</para>
|
||||
<example>
|
||||
<title>get_registered_object</title>
|
||||
<programlisting>
|
||||
function smarty_block_foo($params, &$smarty) {
|
||||
if (isset[$params['object']]) {
|
||||
// get reference to registered object
|
||||
$obj_ref =& $smarty->&get_registered_object($params['object']);
|
||||
// use $obj_ref is now a reference to the object
|
||||
}
|
||||
}</programlisting>
|
||||
</example>
|
||||
</sect1>
|
||||
<sect1 id="api.get.template.vars">
|
||||
@@ -943,20 +1027,24 @@ echo $output;</programlisting>
|
||||
<funcsynopsis>
|
||||
<funcprototype>
|
||||
<funcdef>array <function>get_template_vars</function></funcdef>
|
||||
<paramdef><parameter></parameter></paramdef>
|
||||
<paramdef>string <parameter><optional>varname</optional></parameter></paramdef>
|
||||
</funcprototype>
|
||||
</funcsynopsis>
|
||||
<para>
|
||||
This gets an array of the currently assigned template vars.
|
||||
This returns the given assigned variable value. If no parameter
|
||||
is given, an array of all assigned variables is returned.
|
||||
</para>
|
||||
<example>
|
||||
<title>get_template_vars</title>
|
||||
<programlisting>
|
||||
// get assigned template var 'foo'
|
||||
$foo = $smarty->get_template_vars('foo');
|
||||
|
||||
// get all assigned template vars
|
||||
$tpl_vars = $smarty->get_template_vars();
|
||||
|
||||
// take a look at them
|
||||
var_dump($tpl_vars);</programlisting>
|
||||
print_r($tpl_vars);</programlisting>
|
||||
</example>
|
||||
</sect1>
|
||||
<sect1 id="api.is.cached">
|
||||
|
@@ -1449,11 +1449,11 @@ class Smarty_Compiler extends Smarty {
|
||||
function _expand_quoted_text($var_expr)
|
||||
{
|
||||
// if contains unescaped $, expand it
|
||||
if(preg_match_all('%(?<!\\\\)\$(?:smarty(?:\.\w+){1,2}|\w+(?:' . $this->_var_bracket_regexp . ')*)%', $var_expr, $match)) {
|
||||
if(preg_match_all('%(?<!\\\\)\$\`?(?:smarty(?:\.\w+)*|\w+(?:' . $this->_var_bracket_regexp . ')*)\`?%', $var_expr, $match)) {
|
||||
rsort($match[0]);
|
||||
reset($match[0]);
|
||||
foreach($match[0] as $var) {
|
||||
$var_expr = str_replace ($var, '".' . $this->_parse_var($var) . '."', $var_expr);
|
||||
$var_expr = str_replace ($var, '".' . $this->_parse_var(str_replace('`','',$var)) . '."', $var_expr);
|
||||
}
|
||||
return preg_replace('!\.""|""\.!', '', $var_expr);
|
||||
} else {
|
||||
@@ -1677,73 +1677,52 @@ class Smarty_Compiler extends Smarty {
|
||||
function _compile_smarty_ref(&$indexes)
|
||||
{
|
||||
/* Extract the reference name. */
|
||||
$ref = substr($indexes[0], 1);
|
||||
$_ref = substr($indexes[0], 1);
|
||||
|
||||
switch ($ref) {
|
||||
foreach($indexes as $_index) {
|
||||
if ($_index{0} != '.') {
|
||||
$this->_syntax_error('$smarty' . implode('', array_slice($indexes, 0, 2)) . ' is an invalid reference', E_USER_ERROR, __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
|
||||
switch ($_ref) {
|
||||
case 'now':
|
||||
$compiled_ref = 'time()';
|
||||
if (count($indexes) > 1) {
|
||||
$this->_syntax_error('$smarty' . implode('', $indexes) .' is an invalid reference', E_USER_ERROR, __FILE__, __LINE__);
|
||||
}
|
||||
$_max_index = 1;
|
||||
break;
|
||||
|
||||
case 'foreach':
|
||||
case 'section':
|
||||
if ($indexes[1]{0} != '.') {
|
||||
$this->_syntax_error('$smarty' . implode('', array_slice($indexes, 0, 2)) . ' is an invalid reference', E_USER_ERROR, __FILE__, __LINE__);
|
||||
}
|
||||
$name = substr($indexes[1], 1);
|
||||
array_shift($indexes);
|
||||
if ($ref == 'foreach')
|
||||
$compiled_ref = "\$this->_foreach['$name']";
|
||||
$_var = $this->_parse_var_props(substr($indexes[0], 1));
|
||||
if ($_ref == 'foreach')
|
||||
$compiled_ref = "\$this->_foreach[$_var]";
|
||||
else
|
||||
$compiled_ref = "\$this->_sections['$name']";
|
||||
$compiled_ref = "\$this->_sections[$_var]";
|
||||
break;
|
||||
|
||||
case 'get':
|
||||
array_shift($indexes);
|
||||
$compiled_ref = "\$GLOBALS['HTTP_GET_VARS']";
|
||||
if ($name = substr($indexes[0], 1))
|
||||
$compiled_ref .= "['$name']";
|
||||
break;
|
||||
|
||||
case 'post':
|
||||
array_shift($indexes);
|
||||
$name = substr($indexes[0], 1);
|
||||
$compiled_ref = "\$GLOBALS['HTTP_POST_VARS']";
|
||||
if ($name = substr($indexes[0], 1))
|
||||
$compiled_ref .= "['$name']";
|
||||
break;
|
||||
|
||||
case 'cookies':
|
||||
array_shift($indexes);
|
||||
$name = substr($indexes[0], 1);
|
||||
$compiled_ref = "\$GLOBALS['HTTP_COOKIE_VARS']";
|
||||
if ($name = substr($indexes[0], 1))
|
||||
$compiled_ref .= "['$name']";
|
||||
break;
|
||||
|
||||
case 'env':
|
||||
array_shift($indexes);
|
||||
$compiled_ref = "\$GLOBALS['HTTP_ENV_VARS']";
|
||||
if ($name = substr($indexes[0], 1))
|
||||
$compiled_ref .= "['$name']";
|
||||
break;
|
||||
|
||||
case 'server':
|
||||
array_shift($indexes);
|
||||
$name = substr($indexes[0], 1);
|
||||
$compiled_ref = "\$GLOBALS['HTTP_SERVER_VARS']";
|
||||
if ($name = substr($indexes[0], 1))
|
||||
$compiled_ref .= "['$name']";
|
||||
break;
|
||||
|
||||
case 'session':
|
||||
array_shift($indexes);
|
||||
$name = substr($indexes[0], 1);
|
||||
$compiled_ref = "\$GLOBALS['HTTP_SESSION_VARS']";
|
||||
if ($name = substr($indexes[0], 1))
|
||||
$compiled_ref .= "['$name']";
|
||||
break;
|
||||
|
||||
/*
|
||||
@@ -1759,26 +1738,24 @@ class Smarty_Compiler extends Smarty {
|
||||
|
||||
case 'template':
|
||||
$compiled_ref = "'$this->_current_file'";
|
||||
if (count($indexes) > 1) {
|
||||
$this->_syntax_error('$smarty' . implode('', $indexes) .' is an invalid reference', E_USER_ERROR, __FILE__, __LINE__);
|
||||
}
|
||||
$_max_index = 1;
|
||||
break;
|
||||
|
||||
case 'version':
|
||||
$compiled_ref = "'$this->_version'";
|
||||
$_max_index = 1;
|
||||
break;
|
||||
|
||||
case 'const':
|
||||
array_shift($indexes);
|
||||
$compiled_ref = '(defined("' . substr($indexes[0],1) . '") ? ' . substr($indexes[0],1) . ' : null)';
|
||||
$_val = $this->_parse_var_props(substr($indexes[0],1));
|
||||
$compiled_ref = "(defined($_val) ? $_val : null)";
|
||||
$_max_index = 1;
|
||||
break;
|
||||
|
||||
case 'config':
|
||||
array_shift($indexes);
|
||||
$name = substr($indexes[0], 1);
|
||||
$compiled_ref = "\$this->_config[0]['vars']";
|
||||
if ($name = substr($indexes[0], 1))
|
||||
$compiled_ref .= "['$name']";
|
||||
$_max_index = 2;
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -1786,6 +1763,10 @@ class Smarty_Compiler extends Smarty {
|
||||
break;
|
||||
}
|
||||
|
||||
if (isset($_max_index) && count($indexes) > $_max_index) {
|
||||
$this->_syntax_error('$smarty' . implode('', $indexes) .' is an invalid reference', E_USER_ERROR, __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
array_shift($indexes);
|
||||
return $compiled_ref;
|
||||
}
|
||||
|
Reference in New Issue
Block a user