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

Smarty 3 compiled template path idea

 
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 -> Smarty Development
View previous topic :: View next topic  
Author Message
Michael White
Smarty Rookie


Joined: 08 Oct 2009
Posts: 20

PostPosted: Sun May 22, 2011 3:05 pm    Post subject: Smarty 3 compiled template path idea Reply with quote

Hi,

Today I was working on a template and it kept throwing a fatal error and I had an idea for how to get more accurate information about the actual template file causing the problem, instead of seeing only the compiled template file path.

In my library/framework I have an error handling system that is fairly robust, but when it comes to smarty templates it often falls short of it's normal usefulness because it only shows the path to the compiled version of the template file.

I decided to customize my error handler to look for smarty templates and then decompile the file path back to the full path of the original template file.

As I traced back to how the compiled template name was created I saw that it always contains a sha1() hash. Unfortunately that made what I wanted to do "impossible".

I wasn't going to let that stop me though so I went through and changed all places that set the templateUid to use base64_encode() instead of sha1().

As I did so I thought that such a solution might be made more permanent by using a globalized template/smarty method to actual do the hash creation.

$smarty->getTemplateHash($sourceData);

That would allow developers (such as myself) to override "getTempalteHash" in our own custom smarty classes that extend the base smarty class. Of course, the default return value of that method would still be a sha1() hash, but in our custom implementations we could use any hash creation system we wanted to.

I'm sure there may be other things to take into consideration before something like this could be implemented, but I feel it could be implemented fairly quickly without breaking backwards compatibility.

The good news is that I was able to achieve the goal of obtaining a full absolute file path to the original source template file from the compiled template file name. Very useful
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Mon May 23, 2011 12:21 pm    Post subject: Reply with quote

Here's another alternative:

Another way one could get the original pathname is to encode it somehow in a comment in the compiled template PHP file in a way that it would not be mistaken for some other comment. Then read the actual PHP file when the error occurs. For example:

/*****filepath:/var/www/blahblah/ */

Maybe it could be base64 encoded there, in case a */ is in the path.

That way you can still choose whichever hashing mechanism is most appropriate for compiling templates, rather than for processing errors. And, as an added bonus, there is no processing overhead unless an error occurs.

You could do the same with line numbers, to get the original line number of the template file, although that will cause the template files to increase significantly in size.
Back to top
View user's profile Send private message
rodneyrehm
Administrator


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

PostPosted: Mon May 23, 2011 12:44 pm    Post subject: Reply with quote

The compiled template already knows the source it's been derived of. The very first lines of a compiled template should look similar to:
Code:
<?php /* Smarty version Smarty 3.1-SVN$Rev: 3286 $, created on 2011-05-23 14:43:51
         compiled from "./templates/inherit.two.tpl" */ ?>


a bit of regex and you're set.
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


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

PostPosted: Mon May 23, 2011 3:38 pm    Post subject: Reply with quote

If the compiled or cached template file is loaded you can access file_dependency properties at the template object.

$tplobj->properties['file_dependency'] will contain an array which does also the source file pathes.
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 -> Smarty Development 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