mtoninelli
mtoninelli

Reputation: 714

Zend Framework - get layout from view

-> Short version: I would like to show a layout in my view. How can I call it?

-> Long version I would like to have a table-box in some (not all) pages of my web site. This will be like a widget who shows info about users. So, I created a layout (myLayout.phtml) where I call an helper (MyHelper.php) who, by means a model gets all the infos. Here the point, now I would like from a simple view use this layout for show the table-box. How can i call that layout in my view?

:)

P.S. I followed this good forum (http://inchoo.net/tools-frameworks/zend/zend-framework-custom-view-helper/), but they called the "myLayout.phtml" from the main layout and not from a simple view :/

SOLVED

I put "myLayout.phtml" not in "layouts" folder but in "scripts" folder.

./views/helpers/MyHelper.php

class Zend_View_Helper_MyHelper
{
    public function myHelper()
    {
        $mm = new MyModel();
        return $mm->myModel();
    }

}

./views/scripts/myViews/myHelperView.phtml (ex myLayout.phtml)

<ul>
    <?php foreach($this->myHelper() as $item): ?>
        <li><?php echo $item ?></li>
    <?php endforeach; ?>
</ul>

and in a simple view i call myHelperView

render("myViews/myHelperView.phtml"); ?>

For a kind of my files order I would have preferred keep the last script in a layout folder, but i don't know how read it to that position. I hope this way is a good choise...

Upvotes: 0

Views: 1927

Answers (2)

Config
Config

Reputation: 1692

It sounds like your myLayout.phtml file is a partial script. You can simply load this in your view file as follows:

<?php echo $this->partial('myLayout.phtml') ?>

This is the same as calling it from the layout. It doesn't matter whether you're calling your script from the layout or a view. The layout is just a special view script.

Upvotes: 1

drew010
drew010

Reputation: 69957

It sounds like you have created a custom View Helper. If that is the case, then all you need to in a view to display it is make a call similar to <?php echo $this->myViewHelperName() ?>.

The Placeholder Helper may also be of interest to you. Using the placeholder helper, you call the placeholder unconditionally in your layout, but only assign the contents of your view helper to it from the views you want. This way, if you don't assign anything to the placeholder, it will not display any content, but if you call your view helper and assign the contents to the placeholder, it will show the content you want.

Hope that helps.

Upvotes: 1

Related Questions