0

如何在 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>
4

1 回答 1

0

这里有几个指针:

  1. 你不必创建多个 smarty 实例,smarty 唯一应该被初始化的地方是你的 index.php 文件
  2. 不要从你的 search.php 文件中显示你的 index.tpl 文件,index.php 文件应该包含它自己的 TPL 文件。相反,让您的 search.php 文件访问数据库并将结果作为数组返回,并将其分配给 smarty 变量,您可以在搜索 TPL 文件时使用该变量来显示结果。

我就是这样做的。

索引.php

<?php
require('smarty-setup.php');

$smarty = new Smarty_Setup(); //must match class created in smarty setup file in order for it to work


require('search.php');

//uncomment these lines for debugging
// $smarty->debugging = true;
// $smarty->error_reporting = E_ALL & ~E_NOTICE;
$smarty->display('test.tpl');

smarty-setup.php

<?php

// load Smarty library
require('/smarty/Smarty.class.php');


// The setup.php file is a good place to load
// required application library files, and you
// can do that right here. An example:
// require('guestbook/guestbook.lib.php');

class Smarty_Setup extends Smarty {

   function __construct()

   {

        // Class Constructor.
        // These automatically get set with each new instance.
        parent::__construct();


        $this->setTemplateDir('/templates/');
        $this->setCompileDir('/smarty/templates_c/');
        $this->setConfigDir('/smarty/configs/');
        $this->setCacheDir('/smarty/cache/');
   }
}
?>

搜索.php

  <?php

  if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['search'])))
  {

      $ResultsArray = array();
      //conduct your search using $_POST['search'] to get the search
      //query and put the results in the $ResultsArray above.

      $smarty->assign('searchResults', $ResultsArray);
      $smarty->assign('displayForm', 'false');
  } else {

      $smarty->assign('displayForm', 'true');
  }
  ?>

索引.tpl

<!DOCTYPE html>
<html lang="en">
    <body>
        {include file='search.tpl'}
    </body>
</html>

搜索.tpl

{if $displayForm eq true}
    <form method="post" action="{$smarty.server.SCRIPT_NAME}" class="searchform cf">
          <input type="text" name="search" placeholder="Search" />
          <button type="submit">Search</button>
    </form>

{else}

    <h1>Search Results</h1>
    <ul>
        {foreach from=$searchResults item=result}
            <li>{$result}</li>
        {/foreach}
    </ul>
{/if}

如果您需要,这里有更多信息:

Smarty if 语句
Smarty foreach 语句

另外,速记。您可以创建一个 smarty-setup.php 文件,您可以像我一样在其中声明您的配置、模板、模板_C 和缓存目录。有关这方面的更详细信息,请参见此处 ( http://www.smarty.net/docs/en/installing.smarty.extended.tpl )。

重要提示 此搜索表在任何方面都不安全。它可用于入侵您的网站。为了防止此类攻击,请查阅以下文章:

保护 PHP 形成
SQL 注入(只有在查询数据库时才需要)。

于 2015-08-12T17:50:23.517 回答