Zend可重用的小部件/插件/迷你应用程序? [英] Zend reusable widgets / plugins / miniapplications?
问题描述
我是Zend框架的新手,并试图获得有关代码可重用性的一些见解.我当然了解模块,但是对于应该将哪些功能添加到模块中,什么不应该包含在模块中似乎有些不确定.
I'm new to Zend framework and trying to get some insights about code re-usability. I definitely know about modules but there seems to be a bit of uncertainty about what functionality should go into modules and what not.
我要完成的任务:
1)拥有可重复使用的小程序/小工具/插件(无论您怎么称呼它们),只要将它们插入布局或视图中即可将其插入任何站点:
1) to have reusable mini programs/widgets/plugins (whatever you may call them) that one can simply plug into any site be doing this in layout or view:
<?php echo $this->contactform;?>
在视图中
或>
<?php echo $this->layout()->blog;?>
我称它们为扩展名.所以基本上就是您在Joomla/WordPress/Concrete5模板中看到的那种.
I'd call them extension. so basically sort of what you'd see in Joomla/ WordPress/Concrete5 templates.
2)与该特定扩展名相关的所有代码都应位于其单独的目录中.
2) All code that is related to that specific extension should be in it's separate directory.
3)我们应该只能为某些需要扩展的模块/控制器输出扩展.如果不显示它们,则不应不必要地渲染它们.
3) We should be able to output extensions only for certain modules/controllers where they are required. they shouldn't be rendered needlessly if it won't be displayed.
4)每个扩展名可能在页面上输出多个内容区域.
4) each extension may output multiple content areas on the page.
您使用的布局/方法是否布局合理?
Do you have a nicely laid out structure / approach that you use?
推荐答案
It sounds like you need study up on view helpers. View helpers can be a simple as returning the App Version number or as complicated as adding html to multiple place holders. For example:
layout.phtml:
layout.phtml:
<h1><?php echo $this->placeholder('title'); ?>
<div class="sidebar">
<?php echo $this->placeholder('sidebar'); ?>
</div>
<div class="content">
<?php echo $this->layout()->content; ?>
</div>
例如,在您的视图脚本foo.phtml中:
in your view script foo.phtml for example:
<?php
$this->placeholder('title')->set('Hello World!');
$this->placeholder('sidebar')->set('Hello World!');
?>
<h1>Bar Bar!</h1>
现在,如果您希望能够一遍又一遍地重复使用它,可以执行以下操作:
Now if you want to be able to reuse that over and over again you can do this:
<?php
class Zend_View_Helper_MyHelper extends Zend_View_Helper_Abstract
{
public function myHelper()
{
$this->view->placeholder('title')->set('Hello World!');
$this->view->placeholder('sidebar')->set('Hello World!');
return '<h1>Bar Bar!</h1>';
}
}
现在,将foo.pthml中的代码替换为:
Now, replace the code in your foo.pthml with:
<?php
echo $this->myHelper();
两个foo.phtml输出示例:
Both examples of foo.phtml output:
当然,这是一个非常简化的示例,但是我希望这可以为您指明正确的方向.祝您黑客愉快!
Of course this is very simplified example, but I hope this helps point you in the right direction. Happy Hacking!
这篇关于Zend可重用的小部件/插件/迷你应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!