如何在 smarty 中包含 .php 文件,在 Smarty 中处理来自搜索输入的 $_POST 数据并在 .tpl 文件中显示结果?如何在 smarty 控制器或配置文件中正确定义 search.php?我目前是 smarty 引擎的初学者,对这个引擎
index.php smarty core了解不多
<?php
//ob_start('ob_gzhandler');
$t1 = microtime ( 1 );
session_start ();
header ( "Content-Type: text/html; charset=utf-8" );
require_once ("inc/initf.php");
//require_once("/verjani/public_html/inc/search.php");//how to include ?
$smarty = new Smarty ();
// $smarty->debugging = true;
// $smarty->error_reporting = E_ALL & ~E_NOTICE;
$smarty->cache_dir = THEM_PATH . "/cache";
$smarty->template_dir = THEM_PATH . "/template";
$smarty->compile_dir = THEM_PATH . "/template_c";
Helper::register($smarty);
$frontEnd = new frontEnd ();
$module = $frontEnd->getModule ();
$module->viewHeaders ();
if ($module->displayTpl !== false) {
$smarty->assign ( 'COOKIE', $_COOKIE );
$smarty->assign ( 'this', $module );
$smarty->display ( $module->displayTpl, md5 ( $_SERVER ['REQUEST_URI'] ) );
}
$t = microtime();
echo '<!--'.$t.'-->';
search.php来自http://www.smarty.net/docs/en/language.function.foreach.tpl#idp8696576
<?php
include('Smarty.class.php');
$smarty = new Smarty;
$dsn = 'mysql:host=localhost;dbname=test';
$login = 'test';
$passwd = 'test';
// setting PDO to use buffered queries in mysql is
// important if you plan on using multiple result cursors
// in the template.
$db = new PDO($dsn, $login, $passwd, array(
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));
$res = $db->prepare("select * from users");
$res->execute();
$res->setFetchMode(PDO::FETCH_LAZY);
// assign to smarty
$smarty->assign('res',$res);
$smarty->display('index.tpl');?>
?>
header.tpl.html
<form method="post" action="../search.php" class="searchform cf">
<input type="text" placeholder="">
<button type="submit">Search</button>
</form>