从另一个视图调用一个视图 [英] Calling a View from another View
问题描述
我有一个 login.phtml 视图,它想放入一个通用路径并让任何模块通过应用程序访问它.
I have a login.phtml view that would like to put in a common path and get it accessed by any module through the application.
对于由 $this->render('common/sidebar.phtnl')
呈现的侧边栏,它可以工作,因为我的布局是所有模块的单一布局.
For sidebars rendered by $this->render('common/sidebar.phtnl')
it works since my layout is a single one for all modules.
但是当涉及到内容 $this->layout()->content
时,如果我在结果视图中添加一个 helper,例如 $this->login()
Zend 一直在模块脚本路径上寻找它.
But when it comes to the content $this->layout()->content
, if I add a helper to the result view like $this->login()
Zend keeps looking for it on the module scripts path.
即使我的流程是模块的结果,如何让我的内容视图通过助手呈现另一个常见视图?
How can it be possible to make my content view render another common view througth a helper even if my flow is a result of a module?
推荐答案
这对于 自定义视图助手.编写自己的代码非常简单,一旦尝试过,您将无法停止!
This looks like a good job for a custom view helper. Writing your own is very easy and once you've tried it you won't be able to stop!
你的自定义视图助手应该在 Applications/views/helpers/NameOfHelper.php 中,并且应该有一个名为 nameOfHelper() 的公共方法.我将使用登录作为示例,因为这是您在这种情况下的用例.
Your custom view helper should go in applications/views/helpers/NameOfHelper.php and should have a public method called nameOfHelper(). I'll use login as an example as that is your use case on this occassion.
首先创建applications/views/helpers/Login.php:-
First create applications/views/helpers/Login.php:-
class Zend_View_Helper_Login extends Zend_View_Helper_Abstract
{
public function login()
{
return "Logging in!";
}
}
然后在视图或布局中简单地做:-
Then in the view or layout simply do:-
echo $this->login();
并得到输出:-
登录!
再简单不过了!
或者,如果您想使用视图脚本,您可以在 login() 方法中执行此操作:-
Alternatively if you want to use a view script you can do this in your login() method:-
class Zend_View_Helper_Login extends Zend_View_Helper_Abstract
{
public function login()
{
$this->view->exampleVar = 'example value';
return $this->view->render('login.phtml')
}
}
然后当您在视图或布局中执行 echo $this->login()
时,您将看到您想要的输出.
Then when you do echo $this->login()
in your view or layout you will see the output you want.
显然,您可以将任何代码放入 login() 方法中.
Obviously, you can put whatever code you want into the login() method.
这篇关于从另一个视图调用一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!