テンプレート内に通知ボックスを表示するのに苦労しています。Smarty を利用しています。
main.tpl テンプレートがあります。
{include file='header.tpl'}
{include file='notification.tpl'}
<h1>Welcome</h1>
{eval var=$content}
{include file='footer.tpl'}
notification.tpl:
{if $notification}
<div id="notifications">
{foreach $notification as $detail}
<div class="{$detail.type}">
<p><span class="title">{$detail.title}</span><br>{$detail.content}</p>
</div>
{/foreach}
</div>
{/if}
smarty を強化し、クラスなどを含む私の index.php ページには、基本的なものがあります。
$smarty = new Smarty();
// some details removed for simplicity
// $content is from the database
$smarty->assign("content", $content);
$smarty->assign("notification", $registry->notifications->render());
echo $smarty->fetch('main.tpl');
上記の $content 内には、以下が含まれます。
<p>Have an account with us? Login to your account below:</p>
{widget name="login"}
ウィジェットプラグインを処理するプラグインとクラスを作成しました-上記は完全に機能します(フォームが含まれており、ログインできます)。ただし、間違ったユーザー名/パスワードが入力された場合、メイン テンプレートの通知領域に詳細を追加しようとしていますが、何らかの理由でウィジェット プラグイン内でこれを行うことができないようです。
私の function.widget.php ファイル:
function smarty_function_widget($params, $template){
if(empty($params['name'])){
user_error('Widget is missing name.', E_USER_ERROR);
}
if(file_exists($controller = sprintf('%s/controller.php', WIDGETS_PATH . strtolower($params['name'])))){
require_once($controller);
} else {
// Hide the unfound widget from the end user
eval(sprintf('class %s extends Smarty_Widget {}', strtolower($params['name'])));
}
$widget = call_user_func($params['name'] . '::factory', $params);
$widget->loadView($template->smarty)->execute($widget);
}
私のウィジェットクラス、widget.class.php:
class Smarty_Widget {
protected $smarty;
protected $view = 'default';
public $ext = '.tpl';
public $_vars = array();
public $widget_dir = WIDGETS_PATH;
public $widget_template_dir;
public $registry;
final public static function factory($params = array()){
$className = get_called_class();
return new $className($params);
}
private function __construct($params){
global $registry;
$this->registry = $registry;
$this->_vars = $params;
unset($params, $this->_vars['name']);
}
final public function loadView($smarty){
$this->smarty = $smarty;
$this->smarty->assign($this->_vars);
$this->widget_template_dir = $this->smarty->getTemplateDir('0') . 'widgets/';
return $this;
}
final protected function display(){
$className = strtolower(get_called_class());
if(is_file($this->widget_template_dir . $className . '/' . $this->view . $this->ext)){
// Only display is exists
$this->smarty->display($this->widget_template_dir . $className . '/' . $this->view . $this->ext);
}
}
public function execute(){
return $this->display();
}
}
レンダリングされる index.php 内に通知を追加すると、私の通知クラスは機能します。ただし、ウィジェットのプラグイン (function.widget.php) 内に追加すると、何らかの理由でこれらの通知が表示されません。すべてのサブアイテムはかなり後で処理されるようですが、すでに宣言されているため、追加されません。
だから私の質問は、ページに常に2つのウィジェットを配置できるので、すべてのエラーを2回表示したくない場合、通知部分をどこに正しく割り当てる必要があるかということです。