我有一个自定义块,其中包含一个带有表单的默认视图。当提交该表单时,我设置了一个控制器标志,并且(应该)更新该块以显示更多信息。
问题是我的观点是将其视为没有设置数据/变量
Controller.php这样
public $unlocked = false;
public $employer;
public $shortname = "not loaded";
public function on_page_view() { //already overridden because I'm compiling LESS
...
$this->setViewVariables();
}
function setViewVariables() {
$this->set('shortname', $this->shortname);
$this->set('is_unlocked', $this->unlocked);
...
}
public function action_accesscode_unlock() {
$this->unlocked = true;
$this->shortname = "fred";
//Have also tried calling $this->setViewVariables(); as well,
//before I realised view() and on_page_view() were called after this anyway
}
View.php
<?php if ( !$is_unlocked ) {
echo $shortname; //does correctly display the default value
?>
<form action="<?php echo $this->action('accesscode_unlock')?>" id="accessform" method="post">
...
</form>
<?php } else {
//THIS section is never displayed (always reloads form with default name)
echo $shortname;
} ?>
我在这里做错了什么,以至于新的变量值永远不会在视图中设置?
修改
在回复JohnTheFish之后我才意识到,我使用的LESS编译代码包括以下行(用于获取块路径)。这可能会改变用于生命周期不同部分的实例吗?
$bv = new BlockView();
$bv->setController($this);
$bv->setBlockObject($this->getBlockObject());
答案 0 :(得分:0)
on_page_view在action_accesscode_unlock之前运行,因此在设置变量之后才会发生action_accesscode_unlock的逻辑。
您可以尝试将setViewVariables调用添加到action_accesscode_unlock的末尾。
(回答你的编辑,是的,它可以)