|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Troublegum Smarty Rookie
Joined: 07 Sep 2003 Posts: 33 Location: Germany
|
Posted: Fri Sep 19, 2003 12:27 pm Post subject: compiler functions => no block functions? |
|
|
Hi everyone,
I want to use the following block-function within my templates:
{translate username=$username}hello_world{/translate}
The function should fetch the translated phrase and if necessary insert the current username.. I thought about registering a custom block-function, but then, the php-function is called twice per block (yes, I know it is mentioned in the documentation, but I do not understand why).
The content of the compiled template is the following:
[php:1:57d7902602]<?php
$_params = $this->_tag_stack[] = array('language', array('username' => 'test'));
tpl_language($_params[1], null, $this, $_block_repeat=true);
unset($_params);
while ($_block_repeat) {
ob_start();
?>
hello.world
<?php
$this->_block_content = ob_get_contents();
ob_end_clean();
echo tpl_language($this->_tag_stack[count($this->_tag_stack)-1][1], $this->_block_content, $this, $_block_repeat=false);
}
array_pop($this->_tag_stack);
?>[/php:1:57d7902602]
The function tpl_language is called twice although it is only one block.
I think this is a bit too heavy - in this case only <?php echo tpl_language(...); ?> would be enough.
This would be much faster - especially if {language} is used very often within a template.
My second thought was that I could use a compiler function that translates {language attributes}content{/language} to echo tpl_language(...); but Smarty's compiler functions does not support block-functions - only normal function calls are supported.
What would you do? Any recommendation? |
|
Back to top |
|
alan Smarty Regular
Joined: 17 Apr 2003 Posts: 43
|
Posted: Sun Sep 21, 2003 11:47 pm Post subject: |
|
|
Your block function is called twice - once for the 'open' tag, and once for the 'close' tag.
So, your block function (assume myblock) would be written (roughly) like:
Code: |
function smarty_block_myblock( $block, &$smarty )
{
if ($block)
{
// the {/myblock} tag has just been encountered, therefore...
// ... $block contains the contents of our block.
// perform some processing on the block...
// ... and echo it back
echo strrev($block);
}
}
|
|
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Sep 22, 2003 7:11 am Post subject: |
|
|
alan is right. just a slight addition: i'd prefer "if (isset($block)) {..." over "if ($block) {..." .
reason: there are some rare cases ( {block}0{/block}, {block) {/block}, {block}{/block} ) where $block is false two times, so the block-plugins get's never called for this block. this may have nasty side-effects that are hard to trace. "if (isset())" is better here from a correctness point-of-view.
greetings
messju |
|
Back to top |
|
Troublegum Smarty Rookie
Joined: 07 Sep 2003 Posts: 33 Location: Germany
|
Posted: Mon Sep 22, 2003 4:34 pm Post subject: |
|
|
Thank you for your replies.
I already found that out. I use "if (is_null($block)) return '';".
But I still do not understand WHY block functions are called twice.
There is only one default block function ("textformat") and this block function does nothing when it is called the first time (if ($content == null) return true;). I can only imagine block functions where this first call is not necessary. Could you give and explain an example of a block function where it is imperative that the function is called twice - once with $block = null and once with $block = 'content of the blockfunction' ? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Sep 22, 2003 5:53 pm Post subject: |
|
|
To steal Bart's line: "Think harder homer."
Right off the top of my head, you can take a look at my timeblock plugin. On entry, the time is taken and on exit, the time elapsed is calculated.
Cheers! |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|