Smarty - Nested Function calls when using Objects

Smarty - Nested/Chained function calls when using Objects

When using Smarty, I've been trying to embed something like the following to chain method calls together :

{$theObject->getUser()->getName()}

That, at least with Smarty 2.4.14, doesn't work.

Thankfully, #smarty on freenode, provided the following fix which I'll include here, just incase the link breaks :

Index: Smarty_Compiler.class.php
===================================================================
RCS file: /repository/smarty/libs/Smarty_Compiler.class.php,v
retrieving revision 1.382
diff -u -r1.382 Smarty_Compiler.class.php
--- Smarty_Compiler.class.php	28 May 2006 17:35:05 -0000	1.382
+++ Smarty_Compiler.class.php	11 Jul 2006 14:57:13 -0000
@@ -161,7 +161,7 @@
         $this->_obj_params_regexp = '\((?:' . $this->_obj_single_param_regexp
                 . '(?:\s*,\s*' . $this->_obj_single_param_regexp . ')*)?\)';
         $this->_obj_start_regexp = '(?:' . $this->_dvar_regexp . '(?:' . $this->_obj_ext_regexp . ')+)';
-        $this->_obj_call_regexp = '(?:' . $this->_obj_start_regexp . '(?:' . $this->_obj_params_regexp . ')?(?:' . $this->_dvar_math_regexp . '(?:' . $this->_num_const_regexp . '|' . $this->_dvar_math_var_regexp . ')*)?)';
+        $this->_obj_call_regexp = '(?:' . $this->_obj_start_regexp . '(?:' . $this->_obj_params_regexp . '' . '(?:' . $this->_obj_ext_regexp . $this->_obj_params_regexp . ')*' . ')?(?:' . $this->_dvar_math_regexp . '(?:' . $this->_num_const_regexp . '|' . $this->_dvar_math_var_regexp . ')*)?)';
         
         // matches valid modifier syntax:
         // |foo

So that now works properly, and I don't have to do something nasty like :

{php} echo $this->_tpl_vars['theObject']->getUser()->getName(); {/php} 

Technorati Tags:

Comments

Small world, having the same

Small world, having the same problem and found your patch. All was good until we updated a (test!) server to Etch today, and now we get:
Warning: preg_match() [function.preg-match]: Compilation failed: repeated subpattern is too long at offset 18444 in /mnt/websites/godiva.te/lib/smarty/Smarty_Compiler.class.php on line 438

any suggestions? :)

Greg

yes... been there, fixed that ...

You'll need to compile pcre yourself; see this post and it will work perfectly.

(Sorry for not including more details on the error message in the post, but such is life)

David.

pcre fixed in php 5.2.2rc2

it seems (from a quick test) that it's not necessary to compile php against an external installation of pcre with php 5.2.2RC2.

But property calls doesn't work

{$object->getUser()->username}

Smarty error: [in html:home.tpl line 48]: syntax error: unrecognized tag: $object->getUser()->username

{$object->getUser()->getUsername()} works

Greetings from Germany,
Martin

odd

I've not come across that before; to be honest the Smarty patch is like black magic to me, and I hope I never have to try and fix/modify/alter it

Property calls

Property calls work with this patch:
### Eclipse Workspace Patch 1.0
#P smarty
Index: Smarty_Compiler.class.php
===================================================================
--- Smarty_Compiler.class.php (revision 34623)
+++ Smarty_Compiler.class.php (working copy)
@@ -152,6 +152,8 @@
// $foo->bar($foo->bar)
// $foo->bar($foo->bar())
// $foo->bar($foo->bar($blah,$foo,44,"foo",$foo[0].bar))
+ // $foo->getBar()->getFoo()
+ // $foo->getBar()->foo
$this->_obj_ext_regexp = '\->(?:\$?' . $this->_dvar_guts_regexp . ')';
$this->_obj_restricted_param_regexp = '(?:'
. '(?:' . $this->_var_regexp . '|' . $this->_num_const_regexp . ')(?:' . $this->_obj_ext_regexp . '(?:\((?:(?:' . $this->_var_regexp . '|' . $this->_num_const_regexp . ')'
@@ -161,7 +163,7 @@
$this->_obj_params_regexp = '\((?:' . $this->_obj_single_param_regexp
. '(?:\s*,\s*' . $this->_obj_single_param_regexp . ')*)?\)';
$this->_obj_start_regexp = '(?:' . $this->_dvar_regexp . '(?:' . $this->_obj_ext_regexp . ')+)';
- $this->_obj_call_regexp = '(?:' . $this->_obj_start_regexp . '(?:' . $this->_obj_params_regexp . '(?:' . $this->_obj_ext_regexp . $this->_obj_params_regexp . ')*' . ')?(?:' . $this->_dvar_math_regexp . '(?:' . $this->_num_const_regexp . '|' . $this->_dvar_math_var_regexp . ')*)?)';
+ $this->_obj_call_regexp = '(?:' . $this->_obj_start_regexp . '(?:' . $this->_obj_params_regexp . '(?:' . $this->_obj_ext_regexp . '(?:'.$this->_obj_params_regexp . ')?)*' . ')?(?:' . $this->_dvar_math_regexp . '(?:' . $this->_num_const_regexp . '|' . $this->_dvar_math_var_regexp . ')*)?)';

// matches valid modifier syntax:
// |foo

Thanks!

Oddly I/we don't tend to access properties (probably because we use propel, and everything's wrapped in a 'get' method).

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
We don't take kindly to automated nonsensible adverts around here.