[英]include template using smarty
我正在為我的網站使用模板引擎Smarty。
我想做的是將navbar.tpl包含到homepage.tpl中。 每個tpl文件都有一個php文件,該文件用以下參數填充其變量:
$smarty->assign('{the var name}', "{the value}");
如果我只打開主頁,但是使用以下命令添加了導航欄,它就會起作用:
{include 'NavBar.tpl'}
它包括我的菜單,但是應該從我的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');
?>
這是我的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>
以及homepage.tpl中的實際調用:
<div id="wrap">
{include '../views/NavBar.tpl'}
</div>
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');
?>
有誰知道如何強制navbar.php填充navbar.tpl中的變量,看起來navbar.tpl不知道php文件,但是當我包含php文件時,它將文件顯示為純文本。
我認為您不了解Smarty模板的工作方式。 模板不能調用PHP文件。 對於您的示例,您可以將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);
在此文件中,您只需為導航欄填充數組並為模板分配變量。
然后修改homepage.php。 //declare vars
在此文件中//declare vars
之前,只需包含navbar.php(用於eaxample)即可。 在homepage.php
您包括所有需要的文件,然后是Smarty,然后是navbar.php,其中包含類別初始化,然后是此腳本的主要任務。
您應該將navbar.php包括到所有要在模板中使用navbar.tpl的php文件中。
當您調用$smarty->display('homepage.tpl');
然后Smarty顯示模板homepage.tpl和d包含帶有導航欄的模板。 但是您應該在PHP中分配模板的變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.