Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Change the function plugin for {html_image}

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
Alexey
Smarty Rookie


Joined: 10 Nov 2003
Posts: 11
Location: Marburg, Germany

PostPosted: Fri Apr 02, 2004 9:07 am    Post subject: Change the function plugin for {html_image} Reply with quote

In order to measure the performance loss of {html_image} I'm trying to set this plugin to my own (without disk hits):
[php:1:1277826ff8]<?php
// From within constructor of class MySmarty extends Smarty

$this->register_function("html_image", Array ($this,"print_img"));
?>[/php:1:1277826ff8]
Now I get this error:
Quote:
Fatal error: Call to undefined function: smarty_function_html_image() in /www/website/smarty/templates_c/1200/100/49/%%-83/%%-

Is the "custom" function {html_image} not overridable or any ideas?

BTW: My website designer loves {html_image}
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Apr 02, 2004 9:18 am    Post subject: Reply with quote

You have to recompile your templates. (user $smarty->clear_compiled_tpl() or $smarty->force_compile or clear templates_c by hand).

BTW: the registration should be:
$this->register_function("html_image", Array (&$this,"print_img"));

(a reference to $this is passed as callback then, not a copy)
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Alexey
Smarty Rookie


Joined: 10 Nov 2003
Posts: 11
Location: Marburg, Germany

PostPosted: Fri Apr 02, 2004 9:20 am    Post subject: Reply with quote

Hmm... I've got it. I had to delete all the compiled templates and let them to recompile (or set force_compile=true). Otherwise the template can't check that my plugin has changed.

Other question: It should be possible to transform {html_image} to <img ...> tag in the compiling phase (assume there are no variable parameters of {html_image}). I plan to write a simple pre-filter. So we can still use {html_image} without width and height but generate no disk hits once the templates are compiled since {html_image} is mostly used without any variable parameters. Did anybody try such pre-filter before?
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Apr 02, 2004 10:45 pm    Post subject: Reply with quote

Alexey wrote:
Other question: It should be possible to transform {html_image} to <img ...> tag in the compiling phase (assume there are no variable parameters of {html_image}). I plan to write a simple pre-filter. So we can still use {html_image} without width and height but generate no disk hits once the templates are compiled since {html_image} is mostly used without any variable parameters.


Good idea. Or perhaps {html_image} should be converted into a compile-time function?
Back to top
View user's profile Send private message
Alexey
Smarty Rookie


Joined: 10 Nov 2003
Posts: 11
Location: Marburg, Germany

PostPosted: Mon Apr 05, 2004 9:26 am    Post subject: Add width and height to {html_image} at compile-time Reply with quote

I wrote a simple prefilter to add width and height to {html_image} tag at compile-time. Not as cleen as Smarty compiler but it does it's work for me and helps to save some ms of runtime:

[php:1:7bd1e2e03b]
###########################################################################
## Smarty prefilter
## Add width and height to {html_image file="..."} if file could be found
###########################################################################

function html_image_prefilter($source, &$smarty)
{
$ldq = preg_quote($smarty->left_delimiter, '!');
$rdq = preg_quote($smarty->right_delimiter, '!');

return preg_replace_callback ("!({$ldq}html_image\s*)(.*?)(\s*{$rdq})!s", Array(&$this,"_html_image_callback"), $source);
}

function _html_image_callback($matches)
{
if (preg_match('!file="([^"]+)"!s',$matches[2],$file))
{
if (!strstr($matches[2],"width=") && !strstr($matches[2],"height="))
{ if (substr($file[1],0,1) == '/')
{ $basedir = isset($server_vars['DOCUMENT_ROOT']) ? $server_vars['DOCUMENT_ROOT'] : '';
$_image_path = $basedir.$file[1];
}
else
$_image_path = $file[1];

if ($_image_data = @getimagesize($_image_path))
return $matches[1].$matches[2]." ".$_image_data[3].$matches[3];
}
}

return $matches[1].$matches[2].$matches[3];
}
[/php:1:7bd1e2e03b]
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP