Zend_View_Helper 与 Zend View 部分脚本

发布于 2024-11-09 16:38:42 字数 381 浏览 3 评论 0原文

这是我的岔路口。

我想在我的网页上显示某种按钮,并且我想在很多地方这样做。这个“按钮”实际上只是充当指向其他页面的链接,并且所有按钮实例都将转到一个页面。

我希望所有按钮都相同,除了它们的大小之外。

现在,我应该使用带有按钮 html 的部分脚本并调用部分视图助手来渲染,还是应该创建一个 Zend_View_Helper 来在调用按钮时返回按钮的 html?

我知道我可以选择任何一种方式,但你认为哪种更好?

我看到的一些事情:

  1. 帮助器可能会更好,因为它不必创建一个像 Zend_View 的克隆这样的大对象来完成部分工作。

  2. 部分脚本对于 html 人员来说会更容易使用。

This is my fork in the road.

I want to display some sort of button on my webpage and I want to do it in many places. This 'button' is really just gonna act like a link to some other page and all the button instances are gonna go to one page.

I want all the buttons to be the same except for maybe the size that they are.

Now, should I use a partial script with the html for the button and call the partial view helper to render or should I create a Zend_View_Helper that will return the html for the button when I call it?

I know that I could swing either way but which do you think is better?

Some things I see :

  1. The helper might be better because it wouldn't have to create a BIG object like a clone of a Zend_View to do the partial.

  2. The partial script would be easier for an html person to work with.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绻影浮沉 2024-11-16 16:38:42

使用 $this->render() 而不是 $this->partial()。

仅当您需要对范围进行更多控制时才使用部分。就像你说的,它有很多开销,因为它必须创建一个新的 Zend_View 实例。使用渲染则不会。

为了触及您的实际问题,我建议在视图助手上使用渲染(或部分),因为它对于设计来说更简单,而且开销也更少。视图助手用于自定义功能。

一个好的经验法则是:您想要包含 content/html,还是想要生成内容/应用转换。助手是为后者准备的。

Use $this->render() instead of $this->partial().

Only use partials when you need more control over the scope. Like you said, it has a lot of overhead since it has to create a new Zend_View instance. Using render doesn't.

To touch upon your actual question, I recommend using render (or partial) over a view helper because it's much simpler for designs to work with, and there is less overhead. View helpers are for custom functionality.

A good rule of thumb is: do you want to include content/html, or do you want to generate content / apply transformations. Helpers are for the latter.

昵称有卵用 2024-11-16 16:38:42

根据您的描述,我建议使用部分来完成您的任务。原因是你的按钮似乎主要是html,没有太多php。您可以将大小传递给部分,仅此而已。但是,如果您想在按钮的生成中包含更多逻辑(例如数据库、身份验证或 acl 查询),我建议查看助手。

附注
第三种选择是同时使用部分和视图助手,也许不适用于这种特殊情况,但在更一般的意义上。您可以有对 php 部分执行操作的帮助程序,并且该帮助程序调用部分以返回所需的 html。

Based on your description I would suggest using partial for your task. The reason is that your button seems to be mainly html, without much php. You could pass the size to the partial and that would be it. However, if you would like to include more logic into generation of your button (e.g. database, auth or acl queries) I would suggest view helper.

p.s.
The third option would be to use both partial and view helper, maybe not for this particular case, but in more general sense. You could have helper that does to php part and this helper call partial to return desired html.

梦回旧景 2024-11-16 16:38:42

每当我需要非常参数化的值时,我倾向于使用视图助手。例如:

echo $this->button('Button Text', '/foo/bar/baz');

看起来比:

$this->buttonText = 'Button Text';
$this->buttonUrl = '/foo/bar/baz';
echo $this->render('button.phtml');

另外,没有理由不能从视图助手中渲染视图脚本。

I tend to use a view helper any time I need very parameterized values. For example:

echo $this->button('Button Text', '/foo/bar/baz');

Looks much better than:

$this->buttonText = 'Button Text';
$this->buttonUrl = '/foo/bar/baz';
echo $this->render('button.phtml');

Also, there's no reason you can't render a view script from within your view helper.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文