For Programmers: Free Programming Magazines  


Home > Archive > PHP Smarty Templates > October 2004 > bug in block_function handling?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author bug in block_function handling?
Paolo Nesti Poggi

2004-10-20, 4:08 pm

Hej folks,

I think I stumbled into a bug of the handling of block functions.
Anyhow I need some clarification and the manual can possibly also be clearer
in some points.

I have written a block function that I want to keep uncacheable.

the function is written in a file named:
block.php_component_nc.php

the function itself is named like below:

function smarty_block_php_component_nc($params, $content, &$this)
{
extract($params,EXTR_SKIP);
...
return $component_output;
}

inside my php application it is included and registered by doing:

include_once('Smarty/Smarty_nocache_cmpt/block.php_component_nc.php');//on
the include dir path
$smartyDisplay->register_block('php_component_nc', 'php_component_nc',
false);

Now if I run it with:
$this->caching=false;

everything is all right but when
$this->caching=true;

I get:
Fatal error: Smarty error: [in candidate/it.tpl line 29]: [plugin] could not
load plugin file 'block.php_component_nc.php' (core.load_plugins.php, line
118) in /usr/share/munk_php/Smarty-2.6.6/libs/Smarty.class.php on line 1088

For completness my extended smarty function contains:
$this->Smarty();
$this->template_dir = SMARTY_DIR . 'Smarty_templates/job.eaktion.com';
$this->config_dir=SMARTY_DIR . 'Smarty_configs/job.eaktion.com/akaCMS';
$this->cache_dir=SMARTY_DIR . 'Smarty_cache/job.eaktion.com';
$this->compile_dir=SMARTY_DIR . 'Smarty_templates_c/job.eaktion.com';
$this->plugins_dir=array(SMARTY_DIR .'plugins',SMARTY_DIR .
'Smarty_custom_plugins');
$this->debugging = false;
$this->debugging_ctrl = 'URL';
$this->compile_check=true;
$this->force_compile=false;
$this->caching=true;
$this->cache_lifetime=3600;


Actually I found a way to make it work and it is the following:
including the file as stated above with:
include_once('Smarty/Smarty_nocache_cmpt/block.php_component_nc.php')
but also keeping the same file inside the custom_plugins dir. In this way
the file is found (as a custom plugin), and the function is registered (as a
registered function). To make the trick really work though and avoid a:
Fatal error: Cannot redeclare smarty_block_php_component_nc() (previously
declared in
/usr/share/munk_php/Smarty/Smarty_custom_plugins/block.php_component_nc.php

the function declared inside the file not in the custom_plugins dir has to
be declared as:
php_component_nc (and not smarty_block_php_component_nc)

If I omit the block.php_component_nc.php file from my custom_plugins dir I
get:
Fatal error: Smarty error: [in candidate/it.tpl line 29]: [plugin] could not
load plugin file 'block.php_component_nc.php' (core.load_plugins.php, line
118) in /usr/share/munk_php/Smarty-2.6.6/libs/Smarty.class.php on line 1088

Trying to make this work I've been reading the manual several times to find
an error in what I was doing, and I found places where there are mistakes or
typos (in the manual):

I assume the naming conventions are correct
<quote>The plugin functions inside the plugin files must be named as
follows:
smarty_type_name()
</quote>

, but then in the example 1 at:
http://smarty.php.net/manual/en/api.register.block.php

<quote>
Example 1. register_block

<?php
$smarty->register_block("translate", "do_translation");

function do_translation ($params, $content, &$smarty, &$repeat)
{
if (isset($content)) {
$lang = $params['lang'];
// do some translation with $content
return $translation;
}
}
?>
</quote>

The name of the function in the example should be
smarty_block_do_translation()


The same happens in page:
http://smarty.php.net/manual/en/caching.cacheable.php

<quote>
Example 13-10. Preventing a plugin's output from being cached

<?php
require('Smarty.class.php');
$smarty = new Smarty;
$smarty->caching = true;

function remaining_seconds($params, &$smarty) {
$remain = $params['endtime'] - time();
if ($remain >=0)
return $remain . " second(s)";
else
return "done";
}

$smarty->register_function('remaining', 'remaining_seconds', false,
array('endtime'));

if (!$smarty->is_cached('index.tpl')) {
// fetch $obj from db and assign...
$smarty->assign_by_ref('obj', $obj);
}

$smarty->display('index.tpl');
?>
</quote>

where the function should be declared as:
smarty_function_remaining_seconds

a couple of lines below we find what I guess is a typo
<quote>
where index.tpl is:

Time Remaining: {remain endtime=$obj->endtime}
</quote>

It should be:
{remaining endtime=$obj->endtime}

One more example is provided on the same page and here the error is in the
way the function gets registered:

<quote>
Example 13-11. Preventing a whole passage of a template from being cached

index.php:

<?php
....

function smarty_block_dynamic($param, $content, &$smarty) {
return $content;
}
$smarty->register_block('dynamic', 'smarty_block_dynamic', false);
</quote>

The function should be registered as :
$smarty->register_block('dynamic', 'dynamic', false);


I hope for myself that all this is not the result of my lack of sleeping
time.
If you found the mistake in what I do please tell me, otherwise I'm sure
you'll fix the bug.

Anyhow I won't ever be able to thank you enough for this terrific
application.

Greets
Paolo
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com