1

これについてはよくわかりませんが、smartyの手順http://www.smarty.net/docs/en/installing.smarty.basic.tplを読むと、$ template_dir、$ compile_dir、$config_dirを設定する必要があります。新しいPHPスクリプトがあるたびに$cache_dir。つまり、PHPスクリプトごとに次のコード行を追加する必要があります。

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

あれは正しいですか?これを避けるために、皆さんは「ショートカット」を実行しましたか?

4

2 に答える 2

2

これらすべてを共通の構成ファイルに設定し、必要なときに含める必要があります。

include( 'path/to/common_config.php' );

次に、common_config.phpで、次のようにすることができます。

//set up Smarty
require_once( dirname( __FILE__ ) . '/smarty/Smarty.class.php' );
$smarty = new Smarty;
$smarty->error_reporting = E_ALL & ~E_NOTICE;
$smarty->setTemplateDir( dirname( __FILE__ ) . '/../templates' );
$smarty->setCompileDir( dirname( __FILE__ ) . '/../smarty/templates_c' );

「dirname(FILE)」を使用すると、パスが常に共通の構成ファイルに対して相対的であることが保証されます。

これで、テンプレートファイルの名前でdisplayメソッドを使用するだけです。

$smarty->display( 'index.tpl' );
于 2012-08-14T02:25:40.877 に答える
0

私の意見では、より良い解決策はSmartyクラスを拡張することです。

<?php
require_once SMARTY_DIR . 'Smarty.class.php';

class Application extends Smarty {

    public function __construct() {
        parent::__construct();

        $this
            ->addTemplateDir (TPL_DIR)
            ->setCompileDir  (COMPILE_DIR)
            ->setConfigDir   (CONFIG_DIR)
            ->setCacheDir    (CACHE_DIR)
            ->setPluginsDir  ( array ( SMARTY_DIR.'plugins', PRESENTATION_DIR.'smarty_plugins' ) );

        $this->caching = false;       // set Smarty caching off by default
        $this->muteExpectedErrors();  // can't remember what this does exactly, but it tunes down the sensitivity of errors
        $this->debugging = SMARTY_DEBUG; // setting controlled in config. file
    }
}
?>

次に、新しい「アプリケーション」クラスを開始するだけです。

于 2013-10-02T20:14:12.740 に答える