Php Smarty从类中获取while模板
我对smarty完全陌生。。。这让我毛骨悚然:) 我在/inc/class/search.php中得到了以下类:Php Smarty从类中获取while模板,php,smarty,Php,Smarty,我对smarty完全陌生。。。这让我毛骨悚然:) 我在/inc/class/search.php中得到了以下类: Class search { function __construct($request) { global $dbconn; $request = htmlspecialchars($request); $sql = "SELECT * FROM table WHERE id LIKE '%{$request}%'";
Class search
{
function __construct($request) {
global $dbconn;
$request = htmlspecialchars($request);
$sql = "SELECT * FROM table WHERE id LIKE '%{$request}%'";
$res = $dbconn->Query($sql);
$entity = $res->fetchArray();
return $entity;
}
}
我在php_head.php中有以下内容:
if (isset($_REQUEST['search']) && !empty($_REQUEST['search'])) {
$is = new search($_REQUEST['search']);
$smarty->assign("searchValues", $is);
}
php_head中的这段代码是为以后ajax调用而设计的。但是当我运行index.php?search=string时,我得到了整个smarty模板。请提供帮助。您需要清除ajax所需的模板,如果您想将其包含在其他模板中
{include file="path_to_template.tpl"}
当您只需要此模板的结果时,请使用
echo $smarty->fetch('path_to_template.tpl');
例如,您有:
$smarty->display('index.tpl');// this will return index.tpl
在index.tpl中:
<div id="result_ajax">
{include file="ajax_template.tpl"}
</div>
当搜索在URL中时,您需要做的是只显示输出的一部分 因此,您可以通过以下方式修改代码:
if (isset($_REQUEST['search']) && !empty($_REQUEST['search'])) {
$is = new search($_REQUEST['search']);
$smarty->assign("searchValues", $is);
$smarty->display('searchvalues.tpl'); // custom base template
exit; // stop execution of later code
}
您应该创建
searchvalues.tpl
模板,并在此处仅显示希望显示的部分,而不是整个基本模板 这不是一个选项,因为我还没有做jQuery。我只是通过浏览器向index.php?search=string
if (isset($_REQUEST['search']) && !empty($_REQUEST['search'])) {
$is = new search($_REQUEST['search']);
$smarty->assign("searchValues", $is);
$smarty->display('searchvalues.tpl'); // custom base template
exit; // stop execution of later code
}