4

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.

Community
  • 1
  • 1
тнє Sufi
  • 574
  • 2
  • 9
  • 23

3 Answers3

6

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''}
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • Well, in whmcs templates, `{php}` does work. Only problem I am facing with `php include`. – тнє Sufi Jul 25 '14 at 20:30
  • @тнєSufi What error Smart displays then? What is your directory structure? How you use Smarty? Without more code and details it seems I wouldn't be able to help you – Marcin Nabiałek Jul 26 '14 at 06:55
  • As I has earlier said in my question, I am not familiar with smarty system, and this is WHMCS template. The structure is like `whmcsdirectory/templates/templatename/[tpl-files]`. I think they use v2 of smarty. And I don't know how to enable error check up on smarty. I am going to search about that, and will update accordingly. – тнє Sufi Jul 26 '14 at 11:00
  • You can set error_reporting using `$smarty->error_reporting = E_ALL`. You should probably try all the possibilities: `whmcsdirectory/templates/templatename/phpfile.php`, `templates/templatename/phpfile.php`, `templatename/phpfile.php` and `phpfile.php` - one of them should rather work. You might also have `$smarty->security` set to on and then you can only include PHP files from `trusted_dir` -> http://www.smarty.net/docsv2/en/variable.trusted.dir.tpl – Marcin Nabiałek Jul 26 '14 at 11:58
5

Your using Smarty the wrong way. The whole point of Smarty is to NOT include any PHP in your presentation bits (views, a.k.a. the good ol' HTML).

So, whatever you're trying to do in that PHP file, just let it do its magic, but send the actual result to Smarty. For example, do you want to show a table of users you get out of a database? Execute the query, fetch the result and pass the results (like an array of results) to smarty like this:

<?php
$smarty = new Smarty();
$users = $db->query('SELECT * FROM users');

// Assign query results to template file.
$smarty->assign('users', $users);

// Compile and display the template.
$smarty->display('header.tpl');

Now, in your smarty template you can access that array like this:

<html>
    {foreach from=$users item=user}
        Username: {$user->username}<br />
    {/foreach}
</html>

I hope you see where I am going. Keep your application logic in the PHP file, and let the template just take care of the looks. Keep the template as dumb as possible!

Gladen
  • 792
  • 2
  • 8
  • 17
  • Thanks for the details explanation. I tried with you way, but it did not work. Both my php and tpl files are in the same directory. And they are not in same name. Is that the problem? Or am I missing something else? – тнє Sufi Jul 26 '14 at 02:35
  • No, you don't have to use the same name. Just make sure that you also do `$smarty->display('yourtemplate.tpl');` to actually compile and show your template. What are you tying to do in that PHP file? – Gladen Jul 26 '14 at 06:47
  • In that PHP file, I am getting several database results, some in arrays, some in strings, which I need to show in 3 different tpl files. I will try with the `$smarty->display` and update. – тнє Sufi Jul 26 '14 at 10:45
2

You get data into Smarty by assigning it. Embedding PHP is not recommended, and deprecated from Smarty 3.

php:

$smarty->assign('foo','bar');

smarty:

{$foo}
mohrt
  • 464
  • 5
  • 3