视图助手、部分视图或其他东西 [英] View Helper, Partial View or Something Else

查看:28
本文介绍了视图助手、部分视图或其他东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Zend Framework 的新手,我对我正在尝试做的事情有疑问.

我正在处理的应用程序的大多数页面的主要内容将由 1 个或多个 div 元素组成,这些元素需要具有相同的样式.

这是我要生成的 HTML 示例:

<header class="panel-header"><h2>位置</h2></标题><div class='panel-content'><div id='locations-table' class='google-vis-table'></div><form id='locations'>...</表单>

我知道我可以通过将表单推送到我的控制器中的视图脚本然后将此代码添加到我的控制器来轻松完成此操作.

<header class="panel-header"><h2>位置</h2></标题><div class='panel-content'><div id='locations-table' class="google_vis_table"></div><?phpecho $this->formLocations;?>

但这不是 DRY.

我在这里使用的示例的内容中有一个 Google 可视化表和一个 Zend 表单.有时面板需要包含一个表单.有时他们不会,所以我不认为表单装饰器是要走的路.所以基本上,面板的 id、面板标题文本和 div class='panel-content' 的内容需要是动态的.从面板到面板,其他所有内容都将保持不变.

我最好的选择是什么?

解决方案

您可能需要考虑使用部分:http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.partial

例如,您可以有一个 admin-locations.phtml 部分,其中包含:

<header class="panel-header"><h2>位置</h2></标题><div class='panel-content'><div id='locations-table' class="google_vis_table"></div><?php echo $this->form;?>

现在您可以简单地在视图中重复调用部分,无论是否提供表单:

<代码>...echo $this->partial('admin-locations.phtml');echo $this->partial('admin-locations.phtml', array('form' => $this->yourForm);echo $this->partial('admin-locations.phtml');...

希望这会有所帮助.

I am new to Zend Framework and I have a question about something I am trying to do.

The main content of most pages of the application that I am working on will consist of 1 or more div elements that need to be styled the same.

Here is an example of the HTML that I want to generate:

<div id='admin-locations' class='panel'>
    <header class="panel-header">
        <h2>Locations</h2>
    </header>
    <div class='panel-content'>
        <div id='locations-table' class='google-vis-table'></div>
        <form id='locations'>
            ...
        </form>
    </div>
</div>

I know I can easily do this by pushing the form to my view script in my controller then adding this code to my controller.

<div id='admin-locations' class='panel'>
    <header class="panel-header">
        <h2>Locations</h2>
    </header>
    <div class='panel-content'>
        <div id='locations-table' class="google_vis_table"></div>
        <?php 
            echo $this->formLocations;
        ?>
    </div>
</div>

But that is not DRY.

The example I used here has a Google Visualization Table and a Zend Form in it's content. Sometimes the panels will need to contain a form. Sometimes they won't, so I don't think form decorators are the way to go. So basically, the id of the panel, the panel header text and the content of div class='panel-content' need to be dynamic. Everything else will stay the same from panel to panel.

What is my best option here?

解决方案

You might want to consider using partials: http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.partial

For example, you could have an admin-locations.phtml partial that contains:

<div id='admin-locations' class='panel'>
    <header class="panel-header">
        <h2>Locations</h2>
    </header>
    <div class='panel-content'>
        <div id='locations-table' class="google_vis_table"></div>
        <?php echo $this->form; ?>
    </div>
</div>

Now you can simply repeatedly call the partial within a view, with or without supplying a form:

...
echo $this->partial('admin-locations.phtml');
echo $this->partial('admin-locations.phtml', array('form' => $this->yourForm);
echo $this->partial('admin-locations.phtml');
...

Hope this helps.

这篇关于视图助手、部分视图或其他东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆