0

Smarty に .php ファイルを含め、Smarty の検索入力から $_POST データを処理し、結果を .tpl ファイルに表示する方法は? smartyコントローラーまたは構成ファイルでsearch.phpを正しく定義する方法は? 私は現在 smarty エンジンの初心者であり、このエンジンに関する多くのことやトリックを知りません
index.php smarty コア

<?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.'-->';

http://www.smarty.net/docs/en/language.function.foreach.tpl#idp8696576search.php

<?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. index.tpl ファイルを search.php ファイルから表示しないでください。index.php ファイルには独自の TPL ファイルが含まれている必要があります。代わりに、search.php ファイルでデータベースにアクセスし、結果を配列として返し、TPL ファイルへの検索で使用して結果を表示できる smarty 変数に割り当てます。

これが私がそれをする方法です。

インデックス.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/');
   }
}
?>

Search.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}

必要な場合の詳細情報は次のとおりです。

スマートな if ステートメント
スマートな foreach ステートメント

また、速報。私と同じように、config、templates、templates_C、および caches ディレクトリを宣言できる smarty-setup.php ファイルを作成できます。詳細については、こちら ( http://www.smarty.net/docs/en/installing.smarty.extended.tpl ) を参照してください。

重要な注意事項 この検索フォームは決して安全ではありません。サイトのハッキングに使用できます。このような攻撃から保護するには、次の記事を参照してください。

PHP フォームのセキュリティ保護
SQL インジェクション(データベースにクエリを実行する場合にのみ必要)。

于 2015-08-12T17:50:23.517 に答える