简体   繁体   English

使用smarty包含模板

[英]include template using smarty

i'm using the template engine Smarty for my website. 我正在为我的网站使用模板引擎Smarty。

what i try to do is include my navbar.tpl into my homepage.tpl. 我想做的是将navbar.tpl包含到homepage.tpl中。 every tpl file has a php file that fills their variables with: 每个tpl文件都有一个php文件,该文件用以下参数填充其变量:

$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: 它包括我的菜单,但是应该从我的NavBar.php文件填充的变量没有被填充,这里是我的navbar.php文件:

    <?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: 这是我的navbar.tpl文件:

<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: 以及homepage.tpl中的实际调用:

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

the homepage.php file: homepage.php文件:

<?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. 有谁知道如何强制navbar.php填充navbar.tpl中的变量,看起来navbar.tpl不知道php文件,但是当我包含php文件时,它将文件显示为纯文本。

I think you don't understand how work Smarty template. 我认为您不了解Smarty模板的工作方式。 Templates not call PHP files. 模板不能调用PHP文件。 For you example you can modify navbar.php to next: 对于您的示例,您可以将navbar.php修改为下一个:

$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. 然后修改homepage.php。 Just include navbar.php, for eaxample, before //declare vars in this file. //declare vars在此文件中//declare vars之前,只需包含navbar.php(用于eaxample)即可。 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. homepage.php您包括所有需要的文件,然后是Smarty,然后是navbar.php,其中包含类别初始化,然后是此脚本的主要任务。

You should include navbar.php to all php files where in template you want use navbar.tpl. 您应该将navbar.php包括到所有要在模板中使用navbar.tpl的php文件中。

When you call $smarty->display('homepage.tpl'); 当您调用$smarty->display('homepage.tpl'); then Smarty show template homepage.tpl andd include template with navbar. 然后Smarty显示模板homepage.tpl和d包含带有导航栏的模板。 but variables for templates you should assign inside PHP. 但是您应该在PHP中分配模板的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM