Re: [SMARTY-DEV] another way to handle objects

From: Date: Wed, 25 Dec 2002 12:43:21 +0000
Subject: Re: [SMARTY-DEV] another way to handle objects
References: 1  Groups: php.smarty.dev 
Request: Send a blank email to php-smarty-dev+get-490@lists.php.net to get a copy of this message
Hello, On Tue, Dec 24, 2002 at 09:41:00AM -0600, Monte Ohrt wrote: > This had to have been thought of already, so I'll throw it out there > (again?) and see what reaction we get... > > Currently, we are looking at supporting this kind of syntax for objects: > > {$myobj->get_headline($foo,$smarty.const._MY_CONST)} > > Instead of supporting a new syntax, why not use the exisiting template > syntax and access them through a template function? > > example: > > {object name="myobj" method="get_headline" p1=$foo > p2=$smarty.const._MY_CONST} > > This way we aren't polluting Smarty with new syntax support, we don't > have to make special parsing logic for the parameters, and we are only > adding a plugin. The only downside I see is that the syntax is a little > wordier to work with, but it works all the same. I'm not sure of the > best way to handle parameters since you have to name them. I just used > p1, p2, etc. There are probably other ways to simplify the syntax, maybe > something like: > > {object name="myobj->get_headline" parameters="\$foo, \$bar, > \$smarty.const._MY_CONST"} > > The CVS version of Smarty supports applying modifiers to function output > so you can do this: > > {object|upper name="myobj" ...} > > > Thoughts? What about something like this? --- <?php /* $Id$ */ /* * Smarty plugin * ------------------------------------------------------------- * File: compiler.object.php * Type: compiler * Name: object * Purpose: generate code that calls an object-method with * additional params * Limitations: methods have to be defined as * method(array $params, object &$smarty) * just like plugin-functions * Example: {object $myobj->get_header foo=$foo bar="bar"} * ------------------------------------------------------------- */ function smarty_compiler_object($tag_arg, &$smarty) { list($call_str, $params_str) = split(' ', $tag_arg, 2); if (isset($params_str)) { $params = ''; foreach ($smarty->_parse_attrs($params_str) as $key=>$value) { if (strlen($params)>0) $params .= ', '; $params .= '"'.$key.'"=>'.$value; } } if (preg_match('[(.*)->(.*)]', $call_str, $match)) { $code = $smarty->_parse_var($match[1]).'->'.$match[2].'('; $code .= 'array('.$params.'), $this);'; } return $code; } /* * Local variables: * c-indentation-style: bsd * c-basic-offset: 3 * indent-tabs-mode: nil * End: * * vim: expandtab shiftwidth=3: */ ?> --- it is just a test and needs robust error-handling. and okay, you cannot call arbitrary methods, but this could be done with slight modifications. i don't think it would be right to do this, since it gives a little bit too much power to the template and makes it very easy to wire application-logic into the template. but that is just my opinion btw: merry christmas to everybody on the list, and thank you for such a great tool smarty is. i love to use it for private purposes and at work. go on with it ! :) messju > Monte > > > -- > Monte Ohrt <monte@ispi.net>

« previous php.smarty.dev (#490) next »