是否可以自动将捆绑模板自动嵌入项目模板中?
我正在研究基于Symfony 6 的项目。除了项目外,我还创建了一些包含我在多个项目中使用的代码的捆绑包。
tl; dr:
是否有可能自动从项目特定模板中的捆绑包中自动嵌入模板?
通常,我正在寻找类似此伪代码的内容:
// .../SomeProject/config/routes.yaml
...
my_user_admin:
resource: '@MyUserBundle/config/admin_routes.yaml'
prefix: /admin
embed_in_template: 'admin_routes.yaml::content' <<< Is this possible?
详细信息:
假设MyuserBundle
包含共享代码来处理不同项目中的用户。例如,它包含控制器来列出所有注册用户或编辑单个用户:
// .../MyBundle/config/routes/admin_routes.yaml
my_user_admin_user_list:
path: /users
controller: MyUserBundle\Controller\AdminController::userList
my_user_admin_user_details:
path: /user/{user_id}
controller: MyUserBundle\Controller\AdminController::userDetails
// .../MyBundle/src/Controller
public function userList(Request $request): Response {
...
return $this->render('@MyUser/admin/user_list.html.twig', ['users' => $users]);
}
public function userDetails(Request $request): Response {
...
return $this->render('@MyUser/admin/user_details.html.twig', [...]);
}
// .../MyBundle/templates/admin/user_list.html.twig
<table>
{% for user in users %}
<tr><td>{{ user.name }}</td></tr>
{% endfor %}
</table>
在此示例中列出本身。
此控制器/路由可以轻松地用于使用此捆绑包的项目中:
// .../SomeProject/config/routes.yaml
...
my_user_admin:
resource: '@MyUserBundle/config/admin_routes.yaml'
prefix: /admin
https://someproject.xx/admin/users
现在将显示用户列表。但同样,只有纯用户列表没有任何周围的HTML代码。
问题
是否可以告诉Symfony 自动用其他模板嵌入该控制器/路由的输出?
例如,项目特定模板.../someProject/smotplates/admin_base.html.twig
包含用于该项目的所有管理后端页面的HTML骨架,包括Nav Menue等
// .../SomeProject/templates/admin_base.html.twig
<!DOCTYPE html>
<html>
<head>...</head>
<body>
<nav class="adamin_nav">...</nav>
{% block content %}
// user list of MyUserBundle\Controller\AdminController::userList should be here...
{% endblock %}
</body>
</html>
。解决方案
当然可以手动执行此解决方案:
- Override
MyuserBundle \ Controller \ adminController
在项目中,并根据admin_base.html.html.html.html.twig
渲染一个不同的项目特定模板。 - 请勿从
MyuserBundle
中导入路由,而是在someproject
中定义一个自定义路由和控制器,该使用{{render(myuserBundle \ controller \ controller \ adminController :: userList) )
在其模板中, - 通过提供
.../someProject/someplates/bundles/myuserbundle/user_list.html.twig
,可以扩展admin_base.html.twig
。但是,我不知道如何在其中渲染父模板文件的内容。
尽管这些解决方案会起作用,但它会非常麻烦。不仅有一个捆绑包的路由/控制器,应该以这种方式使用,而且还应从多个捆绑中使用多个路由/控制器。应该添加应添加的每个捆绑式控制器的项目,必须在项目中添加一个新的路由/控制器/模板覆盖。这真的是要走的路吗?
Symfony是如此灵活,并且提供了许多选择,我不确定这确实是最好的选择。
通常,我正在寻找类似的伪代码:
// .../SomeProject/config/routes.yaml
...
my_user_admin:
resource: '@MyUserBundle/config/admin_routes.yaml'
prefix: /admin
embed_in_template: 'admin_routes.yaml::content' <<< Is this possible?
I am working on a Symfony 6
based project. Beside the project it self I have created some bundles which contain code I use in multiple projects.
TL;DR:
Is it possible to automatically embed templates from such a bundle in a project specific template?
In general I am looking for something like this pseudo code:
// .../SomeProject/config/routes.yaml
...
my_user_admin:
resource: '@MyUserBundle/config/admin_routes.yaml'
prefix: /admin
embed_in_template: 'admin_routes.yaml::content' <<< Is this possible?
Details:
Assume MyUserBundle
contains shared code to handle users in different projects. For example it contains controllers to list all registered users or edit a single user:
// .../MyBundle/config/routes/admin_routes.yaml
my_user_admin_user_list:
path: /users
controller: MyUserBundle\Controller\AdminController::userList
my_user_admin_user_details:
path: /user/{user_id}
controller: MyUserBundle\Controller\AdminController::userDetails
// .../MyBundle/src/Controller
public function userList(Request $request): Response {
...
return $this->render('@MyUser/admin/user_list.html.twig', ['users' => $users]);
}
public function userDetails(Request $request): Response {
...
return $this->render('@MyUser/admin/user_details.html.twig', [...]);
}
// .../MyBundle/templates/admin/user_list.html.twig
<table>
{% for user in users %}
<tr><td>{{ user.name }}</td></tr>
{% endfor %}
</table>
In this example the userList
controller does NOT output a complete page with header, body, etc. but only the HTML code of the user list itself.
This controller/route can easily be used in a projects which uses this bundle:
// .../SomeProject/config/routes.yaml
...
my_user_admin:
resource: '@MyUserBundle/config/admin_routes.yaml'
prefix: /admin
https://someproject.xx/admin/users
will now show the user list. But again only the pure user list without any surrounding HTML code.
Question
Is it possible to tell Symfony to automatically embed the output of this controller/route with some other template?
For example the project specific template .../SomeProject/templates/admin_base.html.twig
contains the HTML skeleton to be used for all admin backend pages of this project, including the nav menue, etc.
// .../SomeProject/templates/admin_base.html.twig
<!DOCTYPE html>
<html>
<head>...</head>
<body>
<nav class="adamin_nav">...</nav>
{% block content %}
// user list of MyUserBundle\Controller\AdminController::userList should be here...
{% endblock %}
</body>
</html>
Manual solutions
There a some solution to do this manually of course:
- Override
MyUserBundle\Controller\AdminController
within the project and render a different, project specific template based onadmin_base.html.twig
- Do not import the routes from
MyUserBundle
but define a custom route and controller inSomeProject
which uses{{ render(controller(MyUserBundle\Controller\AdminController::userList))
in its template - Override the bundle template by providing
.../SomeProject/templates/bundles/MyUserBundle/user_list.html.twig
which can then extendadmin_base.html.twig
. However, I do not know how to render the content of the parent template file in there.
While these solution would work, it would be very cumbersome. There is not only route/controller from one bundle which should be used in this way but multiple routes/controller from multiple bundles. One would have have to add a new route/controller/template override, etc. to the project for each bundle controller which should be added. Is this really the way to go?
Symfony is so flexible and provides so many options, I am not sure, that this is really the best option.
In general I am looking for something like this pseudo code:
// .../SomeProject/config/routes.yaml
...
my_user_admin:
resource: '@MyUserBundle/config/admin_routes.yaml'
prefix: /admin
embed_in_template: 'admin_routes.yaml::content' <<< Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论