Yaf_Dispatcher::setView

(Yaf >=1.0.0)

Yaf_Dispatcher::setViewカスタムビューエンジンを設定する

説明

public Yaf_Dispatcher::setView(Yaf_View_Interface $view): Yaf_Dispatcher

このメソッドは、Yaf_View_Simple ではなくカスタムビューエンジンを使いたいときに利用します。

パラメータ

view

Yaf_View_Interface のインスタンス。

戻り値

例1 カスタムビューエンジンの例

<?php
require "/path/to/smarty/Smarty.class.php";

class
Smarty_Adapter implements Yaf_View_Interface
{
/**
* Smarty オブジェクト
* @var Smarty
*/
public $_smarty;

/**
* コンストラクタ
*
* @param string $tmplPath
* @param array $extraParams
* @return void
*/
public function __construct($tmplPath = null, $extraParams = array()) {
$this->_smarty = new Smarty;

if (
null !== $tmplPath) {
$this->setScriptPath($tmplPath);
}

foreach (
$extraParams as $key => $value) {
$this->_smarty->$key = $value;
}
}

/**
* テンプレートへのパスを設定する
*
* @param string $path パスに設定するディレクトリ
* @return void
*/
public function setScriptPath($path)
{
if (
is_readable($path)) {
$this->_smarty->template_dir = $path;
return;
}

throw new
Exception('Invalid path provided');
}

/**
* 変数をテンプレートに代入する
*
* @param string $key 変数名
* @param mixed $val 変数の値
* @return void
*/
public function __set($key, $val)
{
$this->_smarty->assign($key, $val);
}

/**
* empty() と isset() による確認をできるようにする
*
* @param string $key
* @return boolean
*/
public function __isset($key)
{
return (
null !== $this->_smarty->get_template_vars($key));
}

/**
* オブジェクトのプロパティを unset() できるようにする
*
* @param string $key
* @return void
*/
public function __unset($key)
{
$this->_smarty->clear_assign($key);
}

/**
* 変数をテンプレートに代入する
*
* 指定したキーに値を代入したり、
* key => value ペアの配列で一括代入したりできるようにします。
*
* @see __set()
* @param string|array $spec 代入方法に合わせて、キー あるいは key => value ペアの配列)
* @param mixed $value (オプション) 変数名を指定した場合に、ここで値を指定する
* @return void
*/
public function assign($spec, $value = null) {
if (
is_array($spec)) {
$this->_smarty->assign($spec);
return;
}

$this->_smarty->assign($spec, $value);
}

/**
* 代入済みのすべての変数をクリアする
*
* {@link assign()} あるいはプロパティのオーバーロード
* ({@link __get()}/{@link __set()}) で Yaf_View に代入したすべての変数をクリアします
*
* @return void
*/
public function clearVars() {
$this->_smarty->clear_all_assign();
}

/**
* テンプレートを処理して出力を返す
*
* @param string $name 処理するテンプレート
* @return string 出力
*/
public function render($name, $value = NULL) {
return
$this->_smarty->fetch($name);
}

public function
display($name, $value = NULL) {
echo
$this->_smarty->fetch($name);
}

}
?>

例2 Yaf_Dispatcher::setView() の例

<?php
class Bootstrap extends Yaf_Bootstrap_Abstract {

/**
* smarty 用の設定をしておきます
*
* smarty.left_delimiter = "{{"
* smarty.right_delimiter = "}}"
* smarty.template_dir = APPLICATION_PATH "/views/scripts/"
* smarty.compile_dir = APPLICATION_PATH "/views/templates_c/"
* smarty.cache_dir = APPLICATION_PATH "/views/templates_d/"
*
*/
public function _initConfig() {
$config = Yaf_Application::app()->getConfig();
Yaf_Registry::set("config", $config);
}

public function
_initLocalName() {
/** Smarty_Adapter クラスをローカルライブラリディレクトリに配置します */
Yaf_Loader::getInstance()->registerLocalNamespace('Smarty');
}

public function
_initSmarty(Yaf_Dispatcher $dispatcher) {
$smarty = new Smarty_Adapter(null, Yaf_Registry::get("config")->get("smarty"));
$dispatcher->setView($smarty);
/* これで、Smarty ビューエンジンが Yaf のデフォルトビューエンジンになりました */
}
}
?>
add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top