From 1f127b0a549cd3e6a9723ac8883a57f545d3b4fc Mon Sep 17 00:00:00 2001 From: andrey Date: Thu, 26 Apr 2001 16:06:21 +0000 Subject: [PATCH] Added ability to reference object properties. --- NEWS | 2 ++ Smarty_Compiler.class.php | 19 ++++++++++--------- demo/templates/index.tpl | 5 +++++ docs.sgml | 33 +++++++++++++++++++++++++++------ libs/Smarty_Compiler.class.php | 19 ++++++++++--------- templates/index.tpl | 5 +++++ 6 files changed, 59 insertions(+), 24 deletions(-) diff --git a/NEWS b/NEWS index 64d9040e..8590629f 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,7 @@ 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 parameter; this can be used to dynamically change template variables, for example. (Andrei) diff --git a/Smarty_Compiler.class.php b/Smarty_Compiler.class.php index 7ecb441d..0f3bd7c2 100644 --- a/Smarty_Compiler.class.php +++ b/Smarty_Compiler.class.php @@ -170,7 +170,7 @@ class Smarty_Compiler extends Smarty { /* If the tag name matches a variable or section property definition, 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+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command)) { // or a section property settype($tag_command, 'array'); @@ -679,7 +679,7 @@ class Smarty_Compiler extends Smarty { { $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); $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); - $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); $output = "\$this->_tpl_vars['$var_name']"; foreach ($indexes as $index) { - if ($index{0} == '.') { - foreach (array_slice(explode('.', $index), 1) as $prop) { - $output .= "['$prop']"; - } - } else { - list($section, $section_prop) = explode('.', $index); + if ($index{0} == '[') { + list($section, $section_prop) = explode('.', substr($index, 1, -1)); if (!isset($section_prop)) $section_prop = 'index'; $output .= "[\$_smarty_sections['$section']['properties']['$section_prop']]"; + } else if ($index{0} == '.') { + $output .= "['" . substr($index, 1) . "']"; + } else { + $output .= $index; } } diff --git a/demo/templates/index.tpl b/demo/templates/index.tpl index ad66a50c..ab0d854f 100644 --- a/demo/templates/index.tpl +++ b/demo/templates/index.tpl @@ -69,3 +69,8 @@ This is an example of the html_options function: {include file="footer.tpl"} + +{section name=test loop=$a->foo} + foo: {$a->foo[test]} + bar: {$a->bar[test]} +{/section} diff --git a/docs.sgml b/docs.sgml index 625ac8dd..e99f45d7 100644 --- a/docs.sgml +++ b/docs.sgml @@ -1244,9 +1244,9 @@ $smarty->display("index.tpl"); Variables assigned from PHP - 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 - like so: {$varname} + like so: $varname @@ -1269,8 +1269,9 @@ Your last login was on January 11th, 2001. Associative arrays - You can also print variables that are assigned as associative - arrays from PHP by supplying the key value with the array name. + You can also reference associative array variables that are + assigned from PHP by specifying the key after the '.' (period) + symbol. displaying assigned associative array variables @@ -1295,6 +1296,27 @@ zaphod@slartibartfast.com<br> + + Objects + + Properties of objects assigned from PHP can be referenced + by specifying the property name after the '->' symbol. + + +displaying object properties + + +name: {$person->name}<br> +email: {$person->email}<br> + +OUTPUT: + +name: Zaphod Beeblebrox<br> +email: zaphod@slartibartfast.com<br> + + + + Variables passed from config files @@ -1945,8 +1967,7 @@ OUTPUT: (usually an array of values) determines the number of times the section will loop. When printing a variable within a section, the section name must be given next to variable name within brackets - []. If you are using an associative array, separate the key and - value with a period (.). sectionelse is + []. sectionelse is executed when there are no values in the loop variable. diff --git a/libs/Smarty_Compiler.class.php b/libs/Smarty_Compiler.class.php index 7ecb441d..0f3bd7c2 100644 --- a/libs/Smarty_Compiler.class.php +++ b/libs/Smarty_Compiler.class.php @@ -170,7 +170,7 @@ class Smarty_Compiler extends Smarty { /* If the tag name matches a variable or section property definition, 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+%(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command)) { // or a section property settype($tag_command, 'array'); @@ -679,7 +679,7 @@ class Smarty_Compiler extends Smarty { { $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); $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); - $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); $output = "\$this->_tpl_vars['$var_name']"; foreach ($indexes as $index) { - if ($index{0} == '.') { - foreach (array_slice(explode('.', $index), 1) as $prop) { - $output .= "['$prop']"; - } - } else { - list($section, $section_prop) = explode('.', $index); + if ($index{0} == '[') { + list($section, $section_prop) = explode('.', substr($index, 1, -1)); if (!isset($section_prop)) $section_prop = 'index'; $output .= "[\$_smarty_sections['$section']['properties']['$section_prop']]"; + } else if ($index{0} == '.') { + $output .= "['" . substr($index, 1) . "']"; + } else { + $output .= $index; } } diff --git a/templates/index.tpl b/templates/index.tpl index ad66a50c..ab0d854f 100644 --- a/templates/index.tpl +++ b/templates/index.tpl @@ -69,3 +69,8 @@ This is an example of the html_options function: {include file="footer.tpl"} + +{section name=test loop=$a->foo} + foo: {$a->foo[test]} + bar: {$a->bar[test]} +{/section}