CakePHP 有类似 Symfony 的部分功能吗?
如果你想在视图中重用代码,Symfony 有两种基本机制:部分和槽。部分部分很好,因为您可以定义全局部分部分(可以在任何模块中使用它们)和模块部分部分(它们仅在某个模块中可用)。
然而,在 CakePHP 中,您只有常规模板和元素,后者在每个视图中都可用,无论您处于哪个模型/控制器中。CakePHP
有类似 Symfony 的部分功能吗?例如,最好避免为模型重复表单代码。您可以拥有两个“包含”通用表单的模板(添加和编辑)。
我知道您仍然可以使用元素,但是为模块提供“本地”元素目录似乎可以使事情更有条理。您能建议一个解决方法来模拟这个吗?
谢谢!
If you want to reuse code in views Symfony has two basic mechanisms: partials and slots. Partials are nice because you can define global partials (you can use them in any module) and module partials (they are only available in a certain module).
However, in CakePHP you only have regular templates and elements, the latter being available in every view, no matter which model/controller you are in.
Does CakePHP have anything like Symfony's partials? It would nice for example to avoid duplicating forms code for a model. You can have two templates (add and edit) that "include" a common form.
I know you can still use elements, but having a "local" elements directory for a module seems to keep things more organized. Can you suggest a workaround to simulate this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不在控制器特定视图目录中创建一个视图(module_partial.ctp)。这将使代码特定于您希望其所属的控制器。假设您有一个图书控制器。您想要将 BooksController 特定表单添加到某些图书视图中。
在
views/books/
目录中创建一个名为:search_partial.ctp
的视图search_partial.ctp
将包含您想要的 HTML 代码。然后,在任何视图中,只需调用:
这不会阻止其他控制器视图加载它,但它会保持代码库的可读性和隔离性,如您所期望的那样。
所有全局变量都将进入视图/元素。
Why not create a view (module_partial.ctp) inside the controller specific view directory. This will keep the code specific to the controller you want it to pertain to. So lets say you have a books controller. You want to add a BooksController specific form to some of your books views.
Create a view in the
views/books/
directory called:search_partial.ctp
The
search_partial.ctp
will contain the HTML code you want.Then, in any view, just call:
This will not prevent other controllers views from loading it, but it keeps the code base readable and segregated as you expect.
ALL of the globals would go into views/elements.
您可以将元素放入插件中。
你可以做类似 $this->element('something'); 的事情在布局中,并将元素放在插件和/或主应用程序视图文件夹中,例如...
为了不复制像添加/编辑这样的视图,请查看此 https://github.com/infinitas/infinitas/blob/beta/app_controller.php#L389
You can put elements in plugins.
You can do something like $this->element('something'); in the layout and have the element in a plugin and/or the main app views folder like such...
For not duplicating views like add/edit look at this https://github.com/infinitas/infinitas/blob/beta/app_controller.php#L389