Calling a View from another View

2019-02-24 05:29发布

问题:

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.

For sidebars rendered by $this->render('common/sidebar.phtnl') it works since my layout is a single one for all modules.

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?

回答1:

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!

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.

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();

and get the output:-

Logging in!

Couldn't be easier!

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')
    }
}

Then when you do echo $this->login() in your view or layout you will see the output you want.

Obviously, you can put whatever code you want into the login() method.



回答2:

From view script you can:

<?php echo  $this->render("menus/recetas.phtml"); ?>