update docs, fix smarty var compiling, allow any $smarty.*.$foo syntax,

add $`foobar` for embedded variables
This commit is contained in:
mohrt
2003-02-27 17:32:08 +00:00
parent 45c536ad7c
commit e03824dd55
3 changed files with 151 additions and 52 deletions

View File

@@ -250,8 +250,12 @@ email: zaphod@slartibartfast.com&lt;br&gt;</programlisting>
<sect1 id="language.config.variables"> <sect1 id="language.config.variables">
<title>Variables loaded from config files</title> <title>Variables loaded from config files</title>
<para> <para>
Variables that are loaded from the config files are referenced by enclosing Variables that are loaded from the config files are referenced by
them within hash marks (#). 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> </para>
<example> <example>
@@ -281,7 +285,24 @@ index.tpl:
&lt;/body&gt; &lt;/body&gt;
&lt;/html&gt; &lt;/html&gt;
OUTPUT: index.tpl: (alternate syntax)
{config_load file="foo.conf"}
&lt;html&gt;
&lt;title&gt;{$smarty.config.pageTitle}&lt;/title&gt;
&lt;body bgcolor="{$smarty.config.bodyBgColor}"&gt;
&lt;table border="{$smarty.config.tableBorderSize}" bgcolor="{$smarty.config.tableBgColor}"&gt;
&lt;tr bgcolor="{$smarty.config.rowBgColor}"&gt;
&lt;td&gt;First&lt;/td&gt;
&lt;td&gt;Last&lt;/td&gt;
&lt;td&gt;Address&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
OUTPUT: (same for both examples)
&lt;html&gt; &lt;html&gt;
&lt;title&gt;This is mine&lt;/title&gt; &lt;title&gt;This is mine&lt;/title&gt;
@@ -383,6 +404,15 @@ OUTPUT:
</para> </para>
</sect2> </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"> <sect2 id="language.variables.smarty.loops">
<title>{$smarty.section}, {$smarty.foreach}</title> <title>{$smarty.section}, {$smarty.foreach}</title>
<para> <para>

View File

@@ -511,13 +511,30 @@ $smarty-&gt;autoload_filters = array('pre' =&gt; array('trim', 'stamp'),
<paramdef>string <parameter>varname</parameter></paramdef> <paramdef>string <parameter>varname</parameter></paramdef>
<paramdef>mixed <parameter>var</parameter></paramdef> <paramdef>mixed <parameter>var</parameter></paramdef>
</funcprototype> </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> </funcsynopsis>
<para> <para>
This is used to append an element to an assigned array. If you append 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 to a string value, it is converted to an array value and then
appended to. You can explicitly pass name/value pairs, or associative 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> </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> <example>
<title>append</title> <title>append</title>
<programlisting> <programlisting>
@@ -537,10 +554,18 @@ $smarty->append(array("city" => "Lincoln","state" => "Nebraska"));</programlisti
<paramdef>string <parameter>varname</parameter></paramdef> <paramdef>string <parameter>varname</parameter></paramdef>
<paramdef>mixed <parameter>var</parameter></paramdef> <paramdef>mixed <parameter>var</parameter></paramdef>
</funcprototype> </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> </funcsynopsis>
<para> <para>
This is used to append values to the templates by reference instead of 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> </para>
<note> <note>
<title>Technical Note</title> <title>Technical Note</title>
@@ -553,6 +578,15 @@ $smarty->append(array("city" => "Lincoln","state" => "Nebraska"));</programlisti
workaround. workaround.
</para> </para>
</note> </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> <example>
<title>append_by_ref</title> <title>append_by_ref</title>
<programlisting> <programlisting>
@@ -936,6 +970,56 @@ $output = $smarty->fetch("index.tpl");
// do something with $output here // do something with $output here
echo $output;</programlisting> 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, &amp;$smarty) {
if (isset[$params['object']]) {
// get reference to registered object
$obj_ref =&amp; $smarty->&amp;get_registered_object($params['object']);
// use $obj_ref is now a reference to the object
}
}</programlisting>
</example> </example>
</sect1> </sect1>
<sect1 id="api.get.template.vars"> <sect1 id="api.get.template.vars">
@@ -943,20 +1027,24 @@ echo $output;</programlisting>
<funcsynopsis> <funcsynopsis>
<funcprototype> <funcprototype>
<funcdef>array <function>get_template_vars</function></funcdef> <funcdef>array <function>get_template_vars</function></funcdef>
<paramdef><parameter></parameter></paramdef> <paramdef>string <parameter><optional>varname</optional></parameter></paramdef>
</funcprototype> </funcprototype>
</funcsynopsis> </funcsynopsis>
<para> <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> </para>
<example> <example>
<title>get_template_vars</title> <title>get_template_vars</title>
<programlisting> <programlisting>
// get assigned template var 'foo'
$foo = $smarty->get_template_vars('foo');
// get all assigned template vars // get all assigned template vars
$tpl_vars = $smarty->get_template_vars(); $tpl_vars = $smarty->get_template_vars();
// take a look at them // take a look at them
var_dump($tpl_vars);</programlisting> print_r($tpl_vars);</programlisting>
</example> </example>
</sect1> </sect1>
<sect1 id="api.is.cached"> <sect1 id="api.is.cached">

View File

@@ -1449,11 +1449,11 @@ class Smarty_Compiler extends Smarty {
function _expand_quoted_text($var_expr) function _expand_quoted_text($var_expr)
{ {
// if contains unescaped $, expand it // 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]); rsort($match[0]);
reset($match[0]); reset($match[0]);
foreach($match[0] as $var) { 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); return preg_replace('!\.""|""\.!', '', $var_expr);
} else { } else {
@@ -1677,73 +1677,52 @@ class Smarty_Compiler extends Smarty {
function _compile_smarty_ref(&$indexes) function _compile_smarty_ref(&$indexes)
{ {
/* Extract the reference name. */ /* 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': case 'now':
$compiled_ref = 'time()'; $compiled_ref = 'time()';
if (count($indexes) > 1) { $_max_index = 1;
$this->_syntax_error('$smarty' . implode('', $indexes) .' is an invalid reference', E_USER_ERROR, __FILE__, __LINE__);
}
break; break;
case 'foreach': case 'foreach':
case 'section': 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); array_shift($indexes);
if ($ref == 'foreach') $_var = $this->_parse_var_props(substr($indexes[0], 1));
$compiled_ref = "\$this->_foreach['$name']"; if ($_ref == 'foreach')
$compiled_ref = "\$this->_foreach[$_var]";
else else
$compiled_ref = "\$this->_sections['$name']"; $compiled_ref = "\$this->_sections[$_var]";
break; break;
case 'get': case 'get':
array_shift($indexes);
$compiled_ref = "\$GLOBALS['HTTP_GET_VARS']"; $compiled_ref = "\$GLOBALS['HTTP_GET_VARS']";
if ($name = substr($indexes[0], 1))
$compiled_ref .= "['$name']";
break; break;
case 'post': case 'post':
array_shift($indexes);
$name = substr($indexes[0], 1);
$compiled_ref = "\$GLOBALS['HTTP_POST_VARS']"; $compiled_ref = "\$GLOBALS['HTTP_POST_VARS']";
if ($name = substr($indexes[0], 1))
$compiled_ref .= "['$name']";
break; break;
case 'cookies': case 'cookies':
array_shift($indexes);
$name = substr($indexes[0], 1);
$compiled_ref = "\$GLOBALS['HTTP_COOKIE_VARS']"; $compiled_ref = "\$GLOBALS['HTTP_COOKIE_VARS']";
if ($name = substr($indexes[0], 1))
$compiled_ref .= "['$name']";
break; break;
case 'env': case 'env':
array_shift($indexes);
$compiled_ref = "\$GLOBALS['HTTP_ENV_VARS']"; $compiled_ref = "\$GLOBALS['HTTP_ENV_VARS']";
if ($name = substr($indexes[0], 1))
$compiled_ref .= "['$name']";
break; break;
case 'server': case 'server':
array_shift($indexes);
$name = substr($indexes[0], 1);
$compiled_ref = "\$GLOBALS['HTTP_SERVER_VARS']"; $compiled_ref = "\$GLOBALS['HTTP_SERVER_VARS']";
if ($name = substr($indexes[0], 1))
$compiled_ref .= "['$name']";
break; break;
case 'session': case 'session':
array_shift($indexes);
$name = substr($indexes[0], 1);
$compiled_ref = "\$GLOBALS['HTTP_SESSION_VARS']"; $compiled_ref = "\$GLOBALS['HTTP_SESSION_VARS']";
if ($name = substr($indexes[0], 1))
$compiled_ref .= "['$name']";
break; break;
/* /*
@@ -1759,26 +1738,24 @@ class Smarty_Compiler extends Smarty {
case 'template': case 'template':
$compiled_ref = "'$this->_current_file'"; $compiled_ref = "'$this->_current_file'";
if (count($indexes) > 1) { $_max_index = 1;
$this->_syntax_error('$smarty' . implode('', $indexes) .' is an invalid reference', E_USER_ERROR, __FILE__, __LINE__);
}
break; break;
case 'version': case 'version':
$compiled_ref = "'$this->_version'"; $compiled_ref = "'$this->_version'";
$_max_index = 1;
break; break;
case 'const': case 'const':
array_shift($indexes); 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; break;
case 'config': case 'config':
array_shift($indexes);
$name = substr($indexes[0], 1);
$compiled_ref = "\$this->_config[0]['vars']"; $compiled_ref = "\$this->_config[0]['vars']";
if ($name = substr($indexes[0], 1)) $_max_index = 2;
$compiled_ref .= "['$name']";
break; break;
default: default:
@@ -1786,6 +1763,10 @@ class Smarty_Compiler extends Smarty {
break; 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); array_shift($indexes);
return $compiled_ref; return $compiled_ref;
} }