Ferryzijl
Ferryzijl

Reputation: 652

include template using smarty

i'm using the template engine Smarty for my website.

what i try to do is include my navbar.tpl into my homepage.tpl. every tpl file has a php file that fills their variables with:

$smarty->assign('{the var name}', "{the value}");

it does work if i only open my homepage but when i include my navbar using:

{include 'NavBar.tpl'}

it include my menu but the vars that should be filled from my NavBar.php file doesn't get filled here is my navbar.php file:

    <?php
include_once '../Models/DAO/Database.php';
include_once '../Models/category.php';
require_once '../smarty-3.1.29/libs/Smarty.class.php';


$smarty = new Smarty();

$smarty->template_dir = '../Views';
$smarty->compile_dir = '../tmp';

$categories = array();

$db = Database::getInstance();
$mysqli = $db->getConnection();
$sql_query = "select * from categories";
$result = $mysqli->query($sql_query);

while ($row = $result->fetch_object('category')){
    array_push($categories, $row);
}


//assign vars
$smarty->assign('categories', $categories);

$smarty->display('NavBar.tpl');
?>

and here is my navbar.tpl file:

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="Home.php"><img class="logo" alt="Logo" src="../views/images/logo.png"></a>
    </div>
    <ul class="nav navbar-nav">
      <li class="active"><a href="Home.php">Home</a></li>
      <li class="dropdown">
        <a class="dropdown-toggle" data-toggle="dropdown" href="#">Producten
        <span class="caret"></span></a>
        <ul class="dropdown-menu">

        {foreach from=$categories item=categorie}
        <li><a href="product_list.php?categorie={$categorie->name}">{$categorie->name}</a></li>
        {/foreach}
        </ul>
      </li>
      <li><a href="About.php">About</a></li> 
    </ul>
  </div>
</nav>

and the actual call in homepage.tpl:

    <div id="wrap">
        {include '../views/NavBar.tpl'}
    </div>

the homepage.php file:

<?php
include_once '../Models/DAO/Database.php';
include_once '../Models/product.php';
require_once '../smarty-3.1.29/libs/Smarty.class.php';


$smarty = new Smarty();

$smarty->template_dir = '../Views';
$smarty->compile_dir = '../tmp';

//declare vars
$categorieName = "";
$products = array();

//load vars
if (isset($_GET['categorie'])){
    $categorieName = $_GET['categorie'];
    $db = Database::getInstance();
    $mysqli = $db->getConnection();
    $sql_query = "select * from products where categories_category_id = (Select category_id from categories where `name` =".'"'.$categorieName.'")';
    $result = $mysqli->query($sql_query);

    while ($row = $result->fetch_object('product')){
        array_push($products, $row);
    }
}
//assign vars
$smarty->assign('categorie', $categorieName);
$smarty->assign('products', $products);

$smarty->display('homepage.tpl');


?>

does anyone know how to force the navbar.php to fill the vars in navbar.tpl, it looks like that navbar.tpl doesn't know the php file but when i include the php file it shows the file as plain text.

Upvotes: 0

Views: 1204

Answers (1)

newman
newman

Reputation: 2719

I think you don't understand how work Smarty template. Templates not call PHP files. For you example you can modify navbar.php to next:

$categories = array();

$db = Database::getInstance();
$mysqli = $db->getConnection();
$sql_query = "select * from categories";
$result = $mysqli->query($sql_query);

while ($row = $result->fetch_object('category')){
  array_push($categories, $row);
}
//assign vars
$smarty->assign('categories', $categories);

In this file you just fill array for navigation bar and assign vars for template.

Then modify homepage.php. Just include navbar.php, for eaxample, before //declare vars in this file. In homepage.php you include all needed files, then init Smarty, then include navbar.php with categories initialization and then main task of this script.

You should include navbar.php to all php files where in template you want use navbar.tpl.

When you call $smarty->display('homepage.tpl'); then Smarty show template homepage.tpl andd include template with navbar. but variables for templates you should assign inside PHP.

Upvotes: 1

Related Questions