Including PHP file inside smarty template file

тнє Sufi picture тнє Sufi · Jul 25, 2014 · Viewed 11.6k times · Source

I have completely no idea about smarty template system. What I am trying to do is include a php file to get some variable inside a .tpl file (this is a WHMCS template).

I have tried like:

{php} include ('file.php'); {/php} //doesn't work

{include_php file='file.php'}  //doesn't work

I have also followed the answer of this question. Still couldn't get it working.

How can I include code.php inside header.tpl of WHMCS? Any help please?

Just for reference: both tpl and php file is in the same directory, if that helps anyway.

Answer

Marcin Nabiałek picture Marcin Nabiałek · Jul 25, 2014

It's really not recommended to use php code in Smarty. In fact it's now deprecated and you should avoid such solution as much as possible because it often doesn't have sense.

However if you really want to use PHP in your Smarty files for some reason you need to use SmartyBC (Smarty Backward Compatibility) class instead of Smarty class.

So for example instead of:

require_once(_PS_SMARTY_DIR_.'Smarty.class.php');
$smarty = new Smarty();

you should use:

require_once('SmartyBC.class.php');
$smarty = new SmartyBC();

Then you will be able to use PHP in your Smarty template files

EDIT

If you have just problem with including it's probably your directories problem (however you haven't showed any errors).

I assume you have your files inside your templates directory and you set it properly using:

$smarty->setTemplateDir('templates');

if you simple display index.tpl file in Smarty and you have this PHP file in the same directory (in template directory) you can include it without a path.

However if you include in this index.tpl file another tpl file, when you want to include php file you need to pass the full path to this PHP file, for example:

{include_php 'templates/file.php''}