PATCH: Implement block-style object callbacks

From: Date: Sun, 13 Apr 2003 00:27:43 +0000
Subject: PATCH: Implement block-style object callbacks
References: 1 2  Groups: php.smarty.dev 
Request: Send a blank email to php-smarty-dev+get-1141@lists.php.net to get a copy of this message
I've written a patch that allows you to use object methods as block callbacks. This is very useful in my project which has a Theme class which defines the API for themes. Some of the theme definitions include block template functions. The patch works by adding 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 and I'll be happy to change it if you want. 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 or Messju, I'll be happy to work with you to tune the patch if you're unhappy with it. regards, -Bharat

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