CakePHP 有类似 Symfony 的部分功能吗?

发布于 2024-11-02 18:28:43 字数 314 浏览 5 评论 0原文

如果你想在视图中重用代码,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 技术交流群。

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

发布评论

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

评论(2

生死何惧 2024-11-09 18:28:43

为什么不在控制器特定视图目录中创建一个视图(module_partial.ctp)。这将使代码特定于您希望其所属的控制器。假设您有一个图书控制器。您想要将 BooksController 特定表单添加到某些图书视图中。

views/books/ 目录中创建一个名为:search_partial.ctp 的视图

search_partial.ctp 将包含您想要的 HTML 代码。

然后,在任何视图中,只需调用:

<?php echo $this->render('search_partial'); ?>

这不会阻止其他控制器视图加载它,但它会保持代码库的可读性和隔离性,如您所期望的那样。

所有全局变量都将进入视图/元素。

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:

<?php echo $this->render('search_partial'); ?>

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.

空城缀染半城烟沙 2024-11-09 18:28:43

您可以将元素放入插件中。

你可以做类似 $this->element('something'); 的事情在布局中,并将元素放在插件和/或主应用程序视图文件夹中,例如...

App/plugins/a_plugin/views/elements/something.ctp //only called when a controller from 'a_plugin' is called.

App/views/elements/something.ctp // called if the current plugin does not have 'something.ctp' in the elements folder

为了不复制像添加/编辑这样的视图,请查看此 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...

App/plugins/a_plugin/views/elements/something.ctp //only called when a controller from 'a_plugin' is called.

App/views/elements/something.ctp // called if the current plugin does not have 'something.ctp' in the elements folder

For not duplicating views like add/edit look at this https://github.com/infinitas/infinitas/blob/beta/app_controller.php#L389

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