Re: [SMARTY-DEV] register_block() for objects?
| From: | Bharat Mediratta | Date: | Sat, 01 Mar 2003 08:44:56 +0000 |
| Subject: | Re: [SMARTY-DEV] register_block() for objects? | ||
| References: | 1 | Groups: | php.smarty.dev |
| Request: | Send a blank email to php-smarty-dev+get-875@lists.php.net to get a copy of this message | ||
From: "Bharat Mediratta" <bharat@menalto.com>
...
>
> Hi all. In looking through the new OO code, it looks like there's
> no support for registering a block method for an object callback.
> Did I miss something?
I didn't hear back from anybody on this so I implemented this
against the latest CVS version of Smarty. My patch adds an
optional extra argument to register_object where you can specify
which object methods are block methods.
Here's an example of how it works with the patch:
/* PHP */
class Example {
function block($params, $content, &$smarty) {
print "BLOCK: (($content))";
}
function regular($params) {
print "REGULAR: (( $params[text] ))";
}
}
$example = new Example();
$smarty->register_object('example', $example, null, true, array('block'));
{* template *}
{example->regular text="foo"}
{example->block text="foo"}
bar
{/example->block}
Two things to note about this patch:
1. Block methods are only called once, at the end. Not twice like
other block methods. This can be easily changed, though.
2. Modifiers must be applied to the close of the block method, not
the open, eg:
{example->block text="foo"}
bar
{/example->block|default:"empty!"}
I'm not sure if that's the way that regular block functions work,
but it was a natural fit for the way I modified the code.
The patch is a bit cumbersome to paste here so I'll publish it on
my site so that you can download it.
http://www.menalto.com/.outgoing/smarty/block_method_patch.txt
This is functionality that I need in Gallery 2.x so I'm motivated
to get this back into the tree in some form. Monte I'll be happy to
work with you to tune the patch if you're unhappy with it.
regards,
-Bharat