mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-06 11:24:27 +02:00
Added ability to reference object properties.
This commit is contained in:
2
NEWS
2
NEWS
@@ -1,5 +1,7 @@
|
|||||||
Version 1.4.0
|
Version 1.4.0
|
||||||
-------------
|
-------------
|
||||||
|
- implemented '->' syntax for accessing properties of objects passed to the
|
||||||
|
template. (Andrei)
|
||||||
- allowed custom functions to receive Smarty object as the second
|
- allowed custom functions to receive Smarty object as the second
|
||||||
parameter; this can be used to dynamically change template variables, for
|
parameter; this can be used to dynamically change template variables, for
|
||||||
example. (Andrei)
|
example. (Andrei)
|
||||||
|
@@ -170,7 +170,7 @@ class Smarty_Compiler extends Smarty {
|
|||||||
|
|
||||||
/* If the tag name matches a variable or section property definition,
|
/* If the tag name matches a variable or section property definition,
|
||||||
we simply process it. */
|
we simply process it. */
|
||||||
if (preg_match('!^\$\w+(?>(\[\w+(\.\w+)?\])|(\.\w+))*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command) || // if a variable
|
if (preg_match('!^\$\w+(?>(\[\w+(\.\w+)?\])|((\.|->)\w+))*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command) || // if a variable
|
||||||
preg_match('!^#(\w+)#(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command) || // or a configuration variable
|
preg_match('!^#(\w+)#(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command) || // or a configuration variable
|
||||||
preg_match('!^%\w+\.\w+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command)) { // or a section property
|
preg_match('!^%\w+\.\w+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command)) { // or a section property
|
||||||
settype($tag_command, 'array');
|
settype($tag_command, 'array');
|
||||||
@@ -679,7 +679,7 @@ class Smarty_Compiler extends Smarty {
|
|||||||
{
|
{
|
||||||
$qstr_regexp = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'';
|
$qstr_regexp = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'';
|
||||||
|
|
||||||
$var_exprs = preg_grep('!^\$\w+(?>(\[\w+(\.\w+)?\])|(\.\w+))*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens);
|
$var_exprs = preg_grep('!^\$\w+(?>(\[\w+(\.\w+)?\])|((\.|->)\w+))*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens);
|
||||||
$conf_var_exprs = preg_grep('!^#(\w+)#(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens);
|
$conf_var_exprs = preg_grep('!^#(\w+)#(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens);
|
||||||
$sect_prop_exprs = preg_grep('!^%\w+\.\w+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens);
|
$sect_prop_exprs = preg_grep('!^%\w+\.\w+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens);
|
||||||
|
|
||||||
@@ -710,21 +710,22 @@ class Smarty_Compiler extends Smarty {
|
|||||||
{
|
{
|
||||||
list($var_ref, $modifiers) = explode('|', substr($var_expr, 1), 2);
|
list($var_ref, $modifiers) = explode('|', substr($var_expr, 1), 2);
|
||||||
|
|
||||||
$indexes = preg_split('![\[\]]!', $var_ref, -1, PREG_SPLIT_NO_EMPTY);
|
preg_match_all('!\[\w+\]|(->|\.)\w+|^\w+!', $var_ref, $match);
|
||||||
|
$indexes = $match[0];
|
||||||
$var_name = array_shift($indexes);
|
$var_name = array_shift($indexes);
|
||||||
|
|
||||||
$output = "\$this->_tpl_vars['$var_name']";
|
$output = "\$this->_tpl_vars['$var_name']";
|
||||||
|
|
||||||
foreach ($indexes as $index) {
|
foreach ($indexes as $index) {
|
||||||
if ($index{0} == '.') {
|
if ($index{0} == '[') {
|
||||||
foreach (array_slice(explode('.', $index), 1) as $prop) {
|
list($section, $section_prop) = explode('.', substr($index, 1, -1));
|
||||||
$output .= "['$prop']";
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
list($section, $section_prop) = explode('.', $index);
|
|
||||||
if (!isset($section_prop))
|
if (!isset($section_prop))
|
||||||
$section_prop = 'index';
|
$section_prop = 'index';
|
||||||
$output .= "[\$_smarty_sections['$section']['properties']['$section_prop']]";
|
$output .= "[\$_smarty_sections['$section']['properties']['$section_prop']]";
|
||||||
|
} else if ($index{0} == '.') {
|
||||||
|
$output .= "['" . substr($index, 1) . "']";
|
||||||
|
} else {
|
||||||
|
$output .= $index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -69,3 +69,8 @@ This is an example of the html_options function:
|
|||||||
</form>
|
</form>
|
||||||
|
|
||||||
{include file="footer.tpl"}
|
{include file="footer.tpl"}
|
||||||
|
|
||||||
|
{section name=test loop=$a->foo}
|
||||||
|
foo: {$a->foo[test]}
|
||||||
|
bar: {$a->bar[test]}
|
||||||
|
{/section}
|
||||||
|
33
docs.sgml
33
docs.sgml
@@ -1244,9 +1244,9 @@ $smarty->display("index.tpl");
|
|||||||
<sect3>
|
<sect3>
|
||||||
<title>Variables assigned from PHP</title>
|
<title>Variables assigned from PHP</title>
|
||||||
<para>
|
<para>
|
||||||
Variables that are assigned from PHP are displayed by preceeding
|
Variables that are assigned from PHP are referenced by preceding
|
||||||
them with a dollar sign ($) and enclosing the variable in delimiters
|
them with a dollar sign ($) and enclosing the variable in delimiters
|
||||||
like so: {$varname}
|
like so: $varname
|
||||||
</para>
|
</para>
|
||||||
<example>
|
<example>
|
||||||
|
|
||||||
@@ -1269,8 +1269,9 @@ Your last login was on January 11th, 2001.
|
|||||||
<sect3>
|
<sect3>
|
||||||
<title>Associative arrays</title>
|
<title>Associative arrays</title>
|
||||||
<para>
|
<para>
|
||||||
You can also print variables that are assigned as associative
|
You can also reference associative array variables that are
|
||||||
arrays from PHP by supplying the key value with the array name.
|
assigned from PHP by specifying the key after the '.' (period)
|
||||||
|
symbol.
|
||||||
</para>
|
</para>
|
||||||
<example>
|
<example>
|
||||||
<title>displaying assigned associative array variables</title>
|
<title>displaying assigned associative array variables</title>
|
||||||
@@ -1295,6 +1296,27 @@ zaphod@slartibartfast.com<br>
|
|||||||
</example>
|
</example>
|
||||||
</sect3>
|
</sect3>
|
||||||
|
|
||||||
|
<sect3>
|
||||||
|
<title>Objects</title>
|
||||||
|
<para>
|
||||||
|
Properties of objects assigned from PHP can be referenced
|
||||||
|
by specifying the property name after the '->' symbol.
|
||||||
|
</para>
|
||||||
|
<example>
|
||||||
|
<title>displaying object properties</title>
|
||||||
|
<programlisting>
|
||||||
|
|
||||||
|
name: {$person->name}<br>
|
||||||
|
email: {$person->email}<br>
|
||||||
|
|
||||||
|
OUTPUT:
|
||||||
|
|
||||||
|
name: Zaphod Beeblebrox<br>
|
||||||
|
email: zaphod@slartibartfast.com<br>
|
||||||
|
|
||||||
|
</programlisting>
|
||||||
|
</example>
|
||||||
|
</sect3>
|
||||||
|
|
||||||
<sect3>
|
<sect3>
|
||||||
<title>Variables passed from config files</title>
|
<title>Variables passed from config files</title>
|
||||||
@@ -1945,8 +1967,7 @@ OUTPUT:
|
|||||||
(usually an array of values) determines the number of times the
|
(usually an array of values) determines the number of times the
|
||||||
section will loop. When printing a variable within a section, the
|
section will loop. When printing a variable within a section, the
|
||||||
section name must be given next to variable name within brackets
|
section name must be given next to variable name within brackets
|
||||||
[]. If you are using an associative array, separate the key and
|
[]. <emphasis>sectionelse</emphasis> is
|
||||||
value with a period (.). <emphasis>sectionelse</emphasis> is
|
|
||||||
executed when there are no values in the loop variable.
|
executed when there are no values in the loop variable.
|
||||||
</para>
|
</para>
|
||||||
<example>
|
<example>
|
||||||
|
@@ -170,7 +170,7 @@ class Smarty_Compiler extends Smarty {
|
|||||||
|
|
||||||
/* If the tag name matches a variable or section property definition,
|
/* If the tag name matches a variable or section property definition,
|
||||||
we simply process it. */
|
we simply process it. */
|
||||||
if (preg_match('!^\$\w+(?>(\[\w+(\.\w+)?\])|(\.\w+))*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command) || // if a variable
|
if (preg_match('!^\$\w+(?>(\[\w+(\.\w+)?\])|((\.|->)\w+))*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command) || // if a variable
|
||||||
preg_match('!^#(\w+)#(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command) || // or a configuration variable
|
preg_match('!^#(\w+)#(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command) || // or a configuration variable
|
||||||
preg_match('!^%\w+\.\w+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command)) { // or a section property
|
preg_match('!^%\w+\.\w+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command)) { // or a section property
|
||||||
settype($tag_command, 'array');
|
settype($tag_command, 'array');
|
||||||
@@ -679,7 +679,7 @@ class Smarty_Compiler extends Smarty {
|
|||||||
{
|
{
|
||||||
$qstr_regexp = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'';
|
$qstr_regexp = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'';
|
||||||
|
|
||||||
$var_exprs = preg_grep('!^\$\w+(?>(\[\w+(\.\w+)?\])|(\.\w+))*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens);
|
$var_exprs = preg_grep('!^\$\w+(?>(\[\w+(\.\w+)?\])|((\.|->)\w+))*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens);
|
||||||
$conf_var_exprs = preg_grep('!^#(\w+)#(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens);
|
$conf_var_exprs = preg_grep('!^#(\w+)#(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens);
|
||||||
$sect_prop_exprs = preg_grep('!^%\w+\.\w+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens);
|
$sect_prop_exprs = preg_grep('!^%\w+\.\w+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens);
|
||||||
|
|
||||||
@@ -710,21 +710,22 @@ class Smarty_Compiler extends Smarty {
|
|||||||
{
|
{
|
||||||
list($var_ref, $modifiers) = explode('|', substr($var_expr, 1), 2);
|
list($var_ref, $modifiers) = explode('|', substr($var_expr, 1), 2);
|
||||||
|
|
||||||
$indexes = preg_split('![\[\]]!', $var_ref, -1, PREG_SPLIT_NO_EMPTY);
|
preg_match_all('!\[\w+\]|(->|\.)\w+|^\w+!', $var_ref, $match);
|
||||||
|
$indexes = $match[0];
|
||||||
$var_name = array_shift($indexes);
|
$var_name = array_shift($indexes);
|
||||||
|
|
||||||
$output = "\$this->_tpl_vars['$var_name']";
|
$output = "\$this->_tpl_vars['$var_name']";
|
||||||
|
|
||||||
foreach ($indexes as $index) {
|
foreach ($indexes as $index) {
|
||||||
if ($index{0} == '.') {
|
if ($index{0} == '[') {
|
||||||
foreach (array_slice(explode('.', $index), 1) as $prop) {
|
list($section, $section_prop) = explode('.', substr($index, 1, -1));
|
||||||
$output .= "['$prop']";
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
list($section, $section_prop) = explode('.', $index);
|
|
||||||
if (!isset($section_prop))
|
if (!isset($section_prop))
|
||||||
$section_prop = 'index';
|
$section_prop = 'index';
|
||||||
$output .= "[\$_smarty_sections['$section']['properties']['$section_prop']]";
|
$output .= "[\$_smarty_sections['$section']['properties']['$section_prop']]";
|
||||||
|
} else if ($index{0} == '.') {
|
||||||
|
$output .= "['" . substr($index, 1) . "']";
|
||||||
|
} else {
|
||||||
|
$output .= $index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -69,3 +69,8 @@ This is an example of the html_options function:
|
|||||||
</form>
|
</form>
|
||||||
|
|
||||||
{include file="footer.tpl"}
|
{include file="footer.tpl"}
|
||||||
|
|
||||||
|
{section name=test loop=$a->foo}
|
||||||
|
foo: {$a->foo[test]}
|
||||||
|
bar: {$a->bar[test]}
|
||||||
|
{/section}
|
||||||
|
Reference in New Issue
Block a user