Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Addon to detect undefined vars?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
marcguyer
Smarty Rookie


Joined: 06 Mar 2015
Posts: 6

PostPosted: Fri Mar 06, 2015 4:19 pm    Post subject: Addon to detect undefined vars? Reply with quote

Curious... Is there a way to hook into the parsing routine to check if a var that is embedded in a template exists in the assigned vars? For example, consider the following template:

Code:

A tomato is a {$vegetable}
The date is: {$invoice.wrongDate|date_format:"%m/%d/%Y"}


If during compilation, the $vegetable variable doesn't exist and the "wrongDate" index of the $invoice array doesn't exists, I'd like to throw an error/exception so that the composer of the template gets some feedback and can fix the error.

Is anyone aware of a pre-existing addon or plugin that does something like this? If there isn't, I'd consider creating it and making it available. Just looking for a little institutional knowledge...
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Fri Mar 06, 2015 10:57 pm    Post subject: Reply with quote

See http://www.smarty.net/forums/viewtopic.php?t=25436
Back to top
View user's profile Send private message
marcguyer
Smarty Rookie


Joined: 06 Mar 2015
Posts: 6

PostPosted: Sun Mar 08, 2015 2:07 pm    Post subject: Reply with quote

U.Tews wrote:
See http://www.smarty.net/forums/viewtopic.php?t=25436


Right, I should have been more specific. I'm talking about any undef vars not detected during compile.

Consider the following (hypothetical) template instead:

Code:

{if false}
    A tomato is a {$vegetable}
    The date is: {$invoice.wrongDate|date_format:"%m/%d/%Y"}
{/if}


In this case, the undef vars are not detected on compile so there are no php notices.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sun Mar 08, 2015 10:07 pm    Post subject: Reply with quote

That will not work as you may have variables which would be assigned at run time.

Code:

{foreach $foo as $bar}
       {$bar}
 {/foreach}
{counter assign='buh'}
{$buh}


The compiler will find $bar and $buh unassigned
Back to top
View user's profile Send private message
marcguyer
Smarty Rookie


Joined: 06 Mar 2015
Posts: 6

PostPosted: Mon Mar 09, 2015 2:57 pm    Post subject: Reply with quote

U.Tews wrote:
That will not work as you may have variables which would be assigned at run time.


Yes, understood. This is the motivation for the this post.

My code knows all of the variables that are defined at compile time. The compiler knows which variables are embedded in the template. I'd like to add some custom error handling to detect when a variable is embedded in the template but is not defined.

As an example, when the compiler finds
Code:
A tomato is a {$vegetable}
, which would be converted to
Code:
<?php echo $_smarty_tpl->tpl_vars['vegetable'];?>
, but `vegetable` isn't in the `tpl_vars`, I'd like to consider this a compiler exception of sorts.

To be clear, my code knows at compile time which vars will be assigned at run time (and more importantly which ones wont).
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Mon Mar 09, 2015 4:20 pm    Post subject: Reply with quote

Yes it's clear for all variables which have been assigned with Smarty::assign(); before compilation.
But what is with variables which will be assigned when the template code is executed and are unknown before?
Code:
{foreach $foo as $bar}
{$bar}
{/foreach}

$bar is unassigned during compile time.
Or consider variables assigned by plugins. Same problem they are not assigned at compile time.

I think there is no general approach.

Anyway have you thought of implementing your checks in a post filter which runs after compilation before the code is saved?
Back to top
View user's profile Send private message
marcguyer
Smarty Rookie


Joined: 06 Mar 2015
Posts: 6

PostPosted: Mon Mar 09, 2015 5:02 pm    Post subject: Reply with quote

Quote:
Yes it's clear for all variables which have been assigned with Smarty::assign(); before compilation.


Exactly. These are the variables I'm talking about for this purpose.

Quote:
But what is with variables which will be assigned when the template code is executed and are unknown before?


I don't consider these to be in the scope of this functionality. I would consider $foo to be in scope but not $bar. So, yeah, I can see how that would be problematic if Smarty doesn't distinguish between these types of variables. I see that $bar ends up as
Code:
$_smarty_tpl->tpl_vars['bar'] = new Smarty_Variable;
in the compiled template so maybe they are categorized as vars assigned exclusively at run time.

Quote:
Or consider variables assigned by plugins. Same problem they are not assigned at compile time.


Also not relevant for my purposes. Smile Also, I wonder if vars assigned via {assign} in the template are considered different (by Smarty) than those assigned by Smarty::assign();

Quote:
Anyway have you thought of implementing your checks in a post filter which runs after compilation before the code is saved?


I gave more thought to the possibility of a pre filter in which I'd have to write my own parser to find the embedded vars to check them against the Smarty::assign() vars. That's actually when I decided to start this thread because I thought "this would be really easy if I could hook into Smarty's parser in some way".

I have considered a post filter but I fail to see a way to find the undef vars in a post filter. If you have a thought on how it could be done, I'm all ears. I considered, for example, that maybe there's a property of Smarty_Internal_Template that has an array of vars found by the parser but I didn't run into anything like that while reading the code.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Mon Mar 09, 2015 9:40 pm    Post subject: Reply with quote

Yes you can find an array of used variables in the template object at properties['variables'] . This may help but does contain also variables assigned locally inside the template.

It would require a couple of changes inside the compiler to provide more specific information about the variable usage.

It might be something we can add in the Smarty 3.1.22 release. I will check this out during this week.
Back to top
View user's profile Send private message
marcguyer
Smarty Rookie


Joined: 06 Mar 2015
Posts: 6

PostPosted: Tue Mar 10, 2015 1:38 pm    Post subject: Reply with quote

Interesting. Thank you. In trying to prove this concept, I've found that the $template->properties['variables'] array is one-dimensional even when the variables used are elements of an associative array. So again, consider the following template:

Code:

A tomato is a {$vegetable}
The date is: {$invoice.wrongDate|date_format:"%m/%d/%Y"}


The 'variables' property in that case is:

Code:

array(2) {
  ["vegetable"]=>
  int(0)
  ["invoice"]=>
  int(0)
}


So, when the var used is an array, only the name of the array (invoice) is recorded, not the element of the array (wrongDate). It seems that finer granularity of the recorded variables property would be required along with the categorization of locally assigned vars. FWIW, the line number(s) where a var is used in the template would be useful for error display.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Mar 10, 2015 4:24 pm    Post subject: Reply with quote

What when the array index will be variable????
Back to top
View user's profile Send private message
marcguyer
Smarty Rookie


Joined: 06 Mar 2015
Posts: 6

PostPosted: Tue Mar 10, 2015 5:15 pm    Post subject: Reply with quote

No, just a normal associative array.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General 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