简体   繁体   English

Symfony2 使用树枝基页继承

[英]Symfony2 using twig Base Page Inheritance

I am evaluating if I want to use symfony2 and twig for a project I have coming up, I am very new to the symfony2/Twig world, have been primarily building out the framework for each of our sites one by one.我正在评估是否要在我即将进行的项目中使用 symfony2 和 twig,我对 symfony2/Twig 世界非常陌生,主要是为我们的每个站点一一构建框架。 My primary question is if I am using a base twig template, That all of my pages are inheriting from is it possible for that base page to have its own controller?我的主要问题是,如果我使用的是基本树枝模板,我的所有页面都继承自该基本页面是否有可能拥有自己的控制器? as well as the child page to have its own controller?以及子页面拥有自己的控制器?

So for example if the header had some dynamic content that the logic would never change I wouldn't need to replace that logic in the front controller of every page, but the content block would be generated in the front controller.因此,例如,如果标题有一些逻辑永远不会改变的动态内容,我就不需要在每个页面的前端控制器中替换该逻辑,但是内容块将在前端控制器中生成。

I will have multiple developers on the project and to be honest if I can have them writing very little amounts of conde after I configure the project that would be fantastic.我将有多个开发人员参与该项目,老实说,如果我可以让他们在配置项目后编写很少量的 conde,那将是非常棒的。

EX:前任:

<!-- Very Simplified example:baseTemplate -->
<body>
    <div id="headerContainer">
         {% block headerBlock %}{% endblock %}
    </div>
    <div id="bodyContainer">
         {% block bodyBlock %}{% endblock %}
    </div>
    <div id="footerContainer">
         {% block footerBlock %}{% endblock %}
    </div>
 </body>

Another way I have contemplated doing this is by building a class that extends the controller class and having functions that will handle this code for the other developers and they can just call them when rendering the template.我考虑的另一种方法是构建一个扩展控制器类的类,并具有为其他开发人员处理此代码的函数,他们可以在呈现模板时调用它们。

How would you handle this?你会如何处理这件事? Once again I am by no means a seasoned Symfony developer so looking more at best practices as well as if it's possible.再一次,我绝不是一个经验丰富的 Symfony 开发人员,所以更多地关注最佳实践以及如果可能的话。

The best way to do this is to use DI container, for example if you want to create complex menu, you can use KnpLabs/KnpMenuBundle, create MenuBuilder and use it from controller where you need it.最好的方法是使用 DI 容器,例如,如果你想创建复杂的菜单,你可以使用 KnpLabs/KnpMenuBundle,创建 MenuBuilder 并在你需要的地方使用它。 Here you can find more examples:在这里您可以找到更多示例:

http://knpuniversity.com/screencast/question-answer-day/symfony2-users-menu-cms#menus http://knpuniversity.com/screencast/question-answer-day/symfony2-users-menu-cms#menus

Good luck =)祝你好运=)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM