Hack to print defines
| From: | Anders Janson | Date: | Fri, 21 Sep 2001 18:00:41 +0000 |
| Subject: | Hack to print defines | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-1512@lists.php.net to get a copy of this message | ||
Hello!
I needed to output several strings defined in PHP, such as:
define("FOO","This is FOO");
Having to assign lots of Smarty variables would have kept me busy
for a while...
So I coded a Q&D hack to give me a new functionality. In my templates,
I can now write :
{=FOO}
If FOO is defined when the template executes, it will print out the
defined string, otherwise do nothing. This was very useful for me,
and if anybody else wants it, it's an easy patch in Smarty_Compiler.
class.php....
function _compile_tag($template_tag)
{
// PATCH insert these lines:
// Anders 2001-09-21, Handle "defines"
if ($template_tag{0} == '=') {
$def = substr($template_tag ,1);
return "<?php if(defined('$def')){echo $def;} ?>";
}
// End of PATCH
Perhaps a future version of Smarty could allow us to use global,
defined constants? Perhaps even handled exactly as a Smarty variable,
using modifiers, being able to use them as arguments to functions
etc?
Until then, have fun with this hack ;)
/Anders Janson