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

checking a valid template within the template file

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Thu Aug 06, 2009 10:18 pm    Post subject: checking a valid template within the template file Reply with quote

I was producing a very dynamic output, to allow using non-trusted template files (which may have been defined in the database or somewhere).

Instead of going into errors, I needed to make a trick. I needed to check the validity of a dynamic template name, before I can use it in my application.

Hence, I wrote a small modifier plugin to validate. Later, I used it in the template file. I hope, everything is clear within the code below. Usage example is in the comment.

Code:
<?php
/**
* Checks the presense of a template.
* Usage example:
* {if $page.include_file|valid_template}{include file=$page.include_file}{/if}
*/
function smarty_modifier_valid_template($template_name='')
{
   # $smarty is a general assumtion of Smarty variable.
   global $smarty;
   return($smarty->template_exists($template_name));
}
?>
Back to top
View user's profile Send private message Visit poster's website
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Thu Aug 06, 2009 10:21 pm    Post subject: Nothing serious Reply with quote

Please do not take anything too seriously.

Simply: replace $page.include_file with your own variable.
That solves everyting out.

Here are some more usage examples:

* {if $page.include_file|valid_template}{include file=$page.include_file}{/if}
* {if $template_file|valid_template}{include file=$template_file}{/if}
* {if 'profile.tpl'|valid_template}{include file='profile.tpl'}{/if}
Back to top
View user's profile Send private message Visit poster's website
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 -> Tips and Tricks 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