Smarty Patch: Calling class methods in templates / Object handling
| From: | Andr� Rabold | Date: | Fri, 06 Dec 2002 12:32:52 +0000 |
| Subject: | Smarty Patch: Calling class methods in templates / Object handling | ||
| Groups: | php.smarty.dev | ||
| Request: | Send a blank email to php-smarty-dev+get-375@lists.php.net to get a copy of this message | ||
Hi again,
I have (nearly) finished my patch for Smarty to support class methods in
your templates too not only class variables.
Here are some examples what smarty is able do right now and whats not:
{$myObject->variableName} works as expected
{$myObject->getVar("variableName")} throws an "unknown tag" error :-(
{foreach from=$myObjectArray item="obj"}
{assign var="xyz" value=$obj->variableName}
{/foreach}
works as expected
{foreach from=$myObjectArray item="obj"}
{assign var="xyz" value=$obj->getVar("variableName")}
{/foreach}
throws an error that $obj is no object :-(
---------
These errors occures because smarty doesn't accept class methods but only
variables. After modifications on three
Smarty_Compiler.php lines these are supported too now.
Even modifiers on class methods are supported:
{$myObject->getVar("variableName")|lower}
I haven't found any side effects yet. All is working as expected. Please let
me know if you find any errors.
---------
MODIFICATIONS FOR SMARTY 2.3.1:
Line 238:
if
(preg_match('!^\$\w+(?>(\[(\d+|\$\w+|\w+(\.\w+)?)\])|((\.|->)\$?\w+))*(\(.*?
\))?(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command) || //
if a variable
Line 1080:
$var_exprs =
preg_grep('!^\$\w+(?>(\[(\d+|\$\w+|\w+(\.\w+)?)\])|((\.|->)\$?\w+(\(.*?\))?)
)*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens);
Line 1119:
preg_match_all('!\[(?:\$\w+|\w+(\.\w+)?)\]|(->|\.)\$?\w+(\(.*?\))?|^\w+(\(.*
?\))?!', $var_ref, $match);
---------
CVS
cvs diff Smarty_Compiler.class.php (in directory C:\CVS-Checkout\smarty\)
Index: Smarty_Compiler.class.php
===================================================================
RCS file: /repository/smarty/Smarty_Compiler.class.php,v
retrieving revision 1.129
diff -r1.129 Smarty_Compiler.class.php
238c238
< if
(preg_match('!^\$\w+(?>(\[(\d+|\$\w+|\w+(\.\w+)?)\])|((\.|->)\$?\w+))*(?>\|@
?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command) || // if a
variable
---
> if
(preg_match('!^\$\w+(?>(\[(\d+|\$\w+|\w+(\.\w+)?)\])|((\.|->)\$?\w+))*(\(.*?
\))?(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tag_command) || //
if a variable
1080c1080
< $var_exprs =
preg_grep('!^\$\w+(?>(\[(\d+|\$\w+|\w+(\.\w+)?)\])|((\.|->)\$?\w+))*(?>\|@?\
w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens);
---
> $var_exprs =
preg_grep('!^\$\w+(?>(\[(\d+|\$\w+|\w+(\.\w+)?)\])|((\.|->)\$?\w+(\(.*?\))?)
)*(?>\|@?\w+(:(?>' . $qstr_regexp . '|[^|]+))*)*$!', $tokens);
1119c1119
< preg_match_all('!\[(?:\$\w+|\w+(\.\w+)?)\]|(->|\.)\$?\w+|^\w+!', $var_ref,
$match);
---
>
preg_match_all('!\[(?:\$\w+|\w+(\.\w+)?)\]|(->|\.)\$?\w+(\(.*?\))?|^\w+(\(.*
?\))?!', $var_ref, $match);