Search code examples
phpsmartyassign

Smarty - assign variables depend on the page


I want to build a web that uses a master page layout and loads the pages by $_GET['page'],

I thought about using Smarty to separate my HTML code from PHP code, as it is built for it.

But I don't want to assign the all variables pages in one place like where the load page is, like index.php, and make some order in the chaos.

Let me example it, Let's say I have two pages with the names 'Home.tpl' and 'Create.tpl' which are;

templates/Home.tpl;

<title>{$title}</title>
<div>{$varA}</div>

templates/Create.tpl;

<title>{$title}</title>
<div>{$varID}</div>

Now I don't want to assign all variables pages in one place like;

require('smarty-4.2.0/libs/Smarty.class.php');
$smarty = new Smarty();

$smarty->setTemplateDir('templates');
$smarty->setCompileDir('templates_c');
$smarty->setCacheDir('cache');
$smarty->setConfigDir('configs');

// variables for Home.tpl
$smarty->assign('title', 'Home page'); // this variable gonna be overwrite anyway 
$smarty->assign('varA', 'text text text');

// variables for Create.tpl
$smarty->assign('title', 'Create page');
$smarty->assign('varID', 33442222244);

$smarty->display($_GET['page'].'.tpl');

Let's say for

$_GET['page'] = 'Home'

I want (and also thought) that when I call the display function, it will take the HTML code from 'templates/Home.tpl' and compile the code with 'templates_c/Home.php' which will be for example;

$smarty->assign('title', 'Home page');
$smarty->assign('varA', 'text text text');

but that's not the situation.

Should I use the include PHP function for my PHP code (like below) or there is something built-in in Smarty?

include('Home.php');
$smarty->display('Home.tpl');

PS. what the templates_c directory is for if not store the PHP code and why not display the compiled template instead of saving it, especially when it have a cache directory just for compile save time?

I'm a new user with Smarty, and I didn't understand how it works even though I googled it and look at Smarty documentation. Sorry for my English!


Solution

  • You can still use include to load site specific PHP scripts, which assign site specific variables/values into the template engine and display the template of your choice. For simplification (there are thousands of other ways to do it, more secure, more clear, more elegant…), you can do something like this:

    In the "index.php" file:

     $smarty = new Smarty();
    
     if (isset($_GET['page'])) {
         if ($_GET['page'] == 'whatever') {
             include 'whatever.php';
         } else if ($_GET['page'] == 'create') {
             include 'create.php';
         }
         // [...]
     } else {
         include 'home.php';
     }
    

    Each include file assign its own values for the template to display.

    Example "create.php" file:

    // set template variables
    $smarty->assign('title', 'Home page');
    $smarty->assign('varA', 'text text text');
    
    $smarty->display('home.tpl');
    

    The common smarty settings and values are configured/assigned in the "index.php" file and the site specific values are assigned in their individual "home.php", "create.php", "whatever.php", … files.