Re: object methods in templates?
| From: | Frank Adler | Date: | Fri, 19 Jul 2002 15:59:09 +0000 |
| Subject: | Re: object methods in templates? | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-5369@lists.php.net to get a copy of this message | ||
We have developed some workaround for Smarty 2.1.1. This is not fully
tested yet, so use it on your own risk.
You will use it like this:
PHP:
$test = new Test;
$smarty->assign('test',$test);
TPL:
{$test->showSomething()}
Index: Smarty_Compiler.class.php
===================================================================
/classes/Smarty_Compiler.class.php,v
diff -w -r1.1 -r1.2
240c240,241
< 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
>
> preg_match('!^\$\w+(?>(\[(\d+|\w+(\.\w+)?)\])|((\.|->)\$?\w+))*(?>\|@?\w+(:(?>'
> . $qstr_regexp . '|[^|]+))*)*\(.*\)$!', $tag_command)) { // if a Object Method)
244d244
< }
245a246
> }
246a248
>
329a332,338
> case 'function':
>
> list($obj,$method) = explode("->",$tag_args);
> $obj = substr($obj,1,strlen($obj));
>
> return '<?php echo
> $this->_tpl_vars['.$obj.']->'.$method.' ?>';
>
333a343
>
335a346
>
1086a1098
> $obj_methods_exprs =
> preg_grep('!^\$\w+(?>(\[(\d+|\w+(\.\w+)?)\])|((\.|->)\$?\w+))*(?>\|@?\w+(:(?>'
> . $qstr_regexp . '|[^|]+))*)*\(\)$!', $tokens);
1093a1106,1112
> if (count($obj_methods_exprs)) {
> foreach ($obj_methods_exprs as $expr_index => $var_expr) {
>
> $tokens[$expr_index] = $this->_parse_var($var_expr);
> }
> }
>
1118c1137
< preg_match_all('!\[\w+(\.\w+)?\]|(->|\.)\$?\w+|^\w+!', $var_ref, $match);
---
> preg_match_all('!\[\w+(\.\w+)?\]|(->|\.)\$?\w+(\(\))?|^\w+!', $var_ref,
> $match);
1121a1141
>
1141a1162
>
1150a1172
>
Regards,
Frank Adler
K. Ono wrote on Donnerstag, 18. Juli 2002, 11:20:26:
> Does Smarty allow you to access object methods?
> For example
name: {$person->>getName()}<br>
email: {$person->>getEmail()}<br>
> thanks in advance