14

I am new to smarty and I want to use php code in template file i-e tpl file. I have seen the documentation and searched on google but could not find how to use php code they say we need to configure smarty to allow php execution but could not find how to do it.

Kindly help me in this regard. Thanks

jawad waheed
  • 308
  • 1
  • 7
  • 16
  • 2
    You're not supposed to put PHP code in templates. What kind of code are you trying to put in there? – Galen Sep 16 '11 at 15:22
  • 1
    If you are new to this templating system, perhaps you don't use it the right way. It sounds like it if you need to insert PHP because the entire point of a templating system is to separate PHP from markup. Just saying this because you admitted you're new to it, so that you'll also consider the option of eliminating the need for PHP in your .tpl-files. But perhaps you know exactly what you're doing and it that case hopefully you'll get an answer here. – C. E. Sep 16 '11 at 15:22
  • 1
    @Galen, there are times where putting PHP in .tpl files is needed, for example when working with commercial, encoded PHP applications that only allow access to .tpl files. – Prisoner Sep 16 '11 at 15:23
  • If an application allows only editing of Smarty files, with no ability to introduce your own Smarty tag/modifier callbacks, but *does* allow you to embed PHP code directly into those templates, then it sounds like a rather badly-designed application to me. – IMSoP Jul 13 '13 at 14:18

4 Answers4

29

Easy as boiling an egg!

{php}echo "hello!"{/php}

Second link down, for reference.

Edit as of Smarty 3.1:

As of Smarty 3.1 the {php} tags are only available from SmartyBC.

Source: http://www.smarty.net/docs/en/language.function.php.tpl

Prisoner
  • 27,391
  • 11
  • 73
  • 102
  • 1
    IMNSHO, *that* is the perfect example why the `{php}` tag is Evil: had you written into the template `hello!` without *any* markup at all, the result would have been identical (not to mention faster). – Piskvor left the building Sep 16 '11 at 15:27
  • I have seen this but when I add anything in between the tags nothing happens, it is not executed. In doc I found this [link]smarty.net/docs/en/variable.php.handling.tpl but I could not config smarty to execute php code. – jawad waheed Sep 16 '11 at 15:33
  • 9
    @Piskvor, it was an example, get over it. – Prisoner Sep 17 '11 at 09:57
4

Find the file smarty.class.php in your host directory

Go to smarty.class.php

Edit var $php_handling = SMARTY_PHP_ALLOW;

Save the file in server.

Now you may add php in tpl file as <?php ....code.... ?>

ebo
  • 2,717
  • 1
  • 27
  • 22
softnwords
  • 41
  • 2
2

You may have seen the documentation, but you have missed {php}:

The {php} tags allow PHP code to be embedded directly into the template. They will not be escaped, regardless of the $php_handling setting. This is for advanced users only, not normally needed and not recommended.

Emphasis mine, source: http://www.smarty.net/docsv2/en/language.function.php.tpl

Note that putting PHP in template code is the easiest way to shoot yourself in the foot - the main purpose of Smarty is to separate PHP code and HTML templates. In other words, the mere fact of using this tag is a serious red flag; in most cases, it is possible to fix the underlying issue, and avoid PHP inside the template altogether.

Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
  • (I have worked with Smarty a fair bit and using `{php}` has *always* come back to bite me, every single time.) – Piskvor left the building Sep 16 '11 at 15:25
  • I have seen this but when I add anything in between the tags nothing happens, it is not executed. In doc I found this [link]http://www.smarty.net/docs/en/variable.php.handling.tpl but I could not config smarty to execute php code. – jawad waheed Sep 16 '11 at 15:32
  • @jawad waheed: Third sentence from the page you linked: "Note that this does NOT affect php code within {php}{/php} tags in the template." The problem is likely elsewhere (customized Smarty installation?) – Piskvor left the building Sep 16 '11 at 15:40
-1

Have you tried to enable error reporting?

error_reporting(E_ALL);
ini_set("display_errors", true);
Olli
  • 752
  • 6
  • 20