简体   繁体   English

Zend Framework - 从视图中获取布局

[英]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. 所以,我创建了一个布局(myLayout.phtml),我称之为帮助器(MyHelper.php),通过模型获取所有信息。 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? 如何在我的视图中调用该布局?

:) :)

PS 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 :/ PS我跟着这个好论坛(http://inchoo.net/tools-frameworks/zend/zend-framework-custom-view-helper/),但他们从主要布局调用了“myLayout.phtml”,而不是从简单的看法:/

SOLVED 解决了

I put "myLayout.phtml" not in "layouts" folder but in "scripts" folder. 我把“myLayout.phtml”放在“layouts”文件夹中,但放在“scripts”文件夹中。

./views/helpers/MyHelper.php ./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) ./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 在一个简单的视图中,我称之为myHelperView

render("myViews/myHelperView.phtml"); 呈现( “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... 我希望这种方式是一个很好的选择......

It sounds like you have created a custom View Helper . 听起来你已经创建了一个自定义的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() ?> . 如果是这种情况,那么在视图中显示它所需的只是进行类似于<?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. 希望有所帮助。

It sounds like your myLayout.phtml file is a partial script. 听起来你的myLayout.phtml文件是partial脚本。 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. 布局只是一个特殊的视图脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM