Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Handling compiler exceptions, identifying template errors

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


Joined: 27 Apr 2008
Posts: 28

PostPosted: Tue Sep 17, 2013 9:27 am    Post subject: Handling compiler exceptions, identifying template errors Reply with quote

I'm looking to have user-facing smarty templates, and in order to do that I'm going heavy on all the security features and I also need to handle errors gracefully. I've been looking at the Smarty compiler exceptions and I'm wondering if there's a better way to obtain the information I need, or whether it's something that could be improved in Smarty. A typical exception looks something like this (when I var_export it):
Code:

SmartyCompilerException::__set_state(
array( 'message' => 'Syntax Error in template "my.tpl" on line 10 "<h1>test template[[blah]]</h1>" unknown tag "blah"', 'string' => '', 'code' => 0, 'file' => 'smarty/sysplugins/smarty_internal_templatecompilerbase.php', 'line' => 667, 'trace' => ...

There is an extensive trace attached to this, but it's all about the compiler calling context, not the template and the only information about the location of the error within the template is in the message field of the top-level exception. I'd really like to extract:
    The error type ('Syntax error')
    The template resource id or name ('type:id' or 'my.tpl')
    The error message ('unknown tag "blah"')
    The error context ('<h1>test template[[blah]]</h1>')
    The line number (10)

but doing that by parsing that message field seems like an unreliable approach, especially in a multilingual context. What I'd really like is for those things to be available as properties within the exception. I assume these values are available somewhere deep in the internals when the exception is created, but it would be much better to have them as separate properties, not assembed into a string.
Back to top
View user's profile Send private message
Synchro
Smarty Rookie


Joined: 27 Apr 2008
Posts: 28

PostPosted: Tue Sep 17, 2013 4:03 pm    Post subject: Reply with quote

I'm happy to report that I managed to make this work as I wanted by altering the SmartyCompilerException class. I'll post my changes on the google code project.

I was looking for how to contribute code to Smarty. I eventually found it on google code, but I'm shocked that there isn't a link to that on the home page, nor in the resources section of the docs, nor in the FAQs, nor could I find any mention of how to contribute. It's like you're trying to hide from would-be contributors! I've seen various mentions of moving to GitHub, some several years old, but it's not there yet - it can't happen soon enough!
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7271
Location: Lincoln Nebraska, USA

PostPosted: Tue Sep 17, 2013 6:41 pm    Post subject: Reply with quote

Synchro wrote:
I'm happy to report that I managed to make this work as I wanted by altering the SmartyCompilerException class. I'll post my changes on the google code project.

I was looking for how to contribute code to Smarty. I eventually found it on google code, but I'm shocked that there isn't a link to that on the home page, nor in the resources section of the docs, nor in the FAQs, nor could I find any mention of how to contribute. It's like you're trying to hide from would-be contributors! I've seen various mentions of moving to GitHub, some several years old, but it's not there yet - it can't happen soon enough!


Smarty 3.2 will be moving to github, easing the contribution path.
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


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

PostPosted: Tue Sep 17, 2013 8:47 pm    Post subject: Reply with quote

I have comitted your patch to the SVN trunk.

The patch does add the following addition properties to the SmartyCompilerException class:

/**
* The line number of the template error
* @type int|null
*/
public $line = null;
/**
* The template source snippet relating to the error
* @type string|null
*/
public $source = null;
/**
* The raw text of the error message
* @type string|null
*/
public $desc = null;
/**
* The resource identifier or template name
* @type string|null
*/
public $template = null;
Back to top
View user's profile Send private message
Synchro
Smarty Rookie


Joined: 27 Apr 2008
Posts: 28

PostPosted: Tue Sep 17, 2013 11:25 pm    Post subject: Reply with quote

Great news!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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