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

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
ninjinka
Smarty n00b


Joined: 14 Jun 2012
Posts: 3

PostPosted: Thu Jun 14, 2012 12:44 pm    Post subject: Undefined variable handling Reply with quote

Hello, i want to override the function "getVariable" in class "Smarty_Internal_Data" to manage undefined variable (for example log the variable name or give a default value for some kind of variable or anything else)

Is it possible without modifying directly the smarty core? i want to mantain compatibility to new release as for function bock and modifiers.
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 698
Location: Germany, border to Switzerland

PostPosted: Thu Jun 14, 2012 1:34 pm    Post subject: Reply with quote

Smarty 3.2 will have a callback function (default_variable_handler) that will allow you to do what you described. Smarty 3.1.x does not have that API. The inheritance path up to Smarty_Internal_Data is not configurable (basically because PHP does not allow dynamically extending classes).

I don't see a way around hacking that core function to achieve your goal right now.
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4654
Location: Hamburg / Germany

PostPosted: Thu Jun 14, 2012 1:40 pm    Post subject: Reply with quote

For default values use the default modifier, see http://www.smarty.net/docs/en/language.modifier.default.tpl.

It's unsafe to modify "getVariable" for this purpose as the variable handling did change internally between different Smarty 3 versions.

With the next major release 3.2 (which is still a bit down the road) we will introduce a default variable handler which is called on undefined template variables and allows all sorts of handling.
Back to top
View user's profile Send private message
ninjinka
Smarty n00b


Joined: 14 Jun 2012
Posts: 3

PostPosted: Thu Jun 14, 2012 1:49 pm    Post subject: Reply with quote

default modifyer ( |default: ) is usefull if you already know that a variable can be empty or not set.
sometimes you have construct like {${$varname}} that must have different default based on name (like for internationalization)
or anyway complex situation where it's better to handle undefined than default.

For a real case example:
i'm writing an application that have a huge "catalog", for retro-compatibility i can't call catalog with a function by must use directly the index of the catalog as variable, i must so assign each value in smarty (like 1000 variables) this is really memory consuming...

If i can say "if the variable is not set than look inside the catalog" it'll be really better..
Back to top
View user's profile Send private message
ninjinka
Smarty n00b


Joined: 14 Jun 2012
Posts: 3

PostPosted: Fri Jun 15, 2012 2:34 pm    Post subject: Reply with quote

by now i resolved using handling php error but it's not a real solution (i simply add a new variable after the error so next time the page will be loaded there won't be the error again).

I'll wait for the next smarty release or the implementation in php of monkey patch... the first that will come Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3 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