【发布时间】:2017-06-07 02:51:49
【问题描述】:
所以,我对 Symfony 和 Twig 还是很陌生。我想知道如何在模板中最好地包含/创建可重用代码的 sn-p。例如,假设您有一个要在每个页面上显示的侧边栏。
{% extends 'AppBundle::base.html.twig' %}
{% block body %}
<div id="wrapper">
<div id="content-container">
{# Main content... #}
</div>
<div id="sidebar">
{% include 'sidebar.html.twig' %}
</div>
</div>
{% endblock %}
在那个侧边栏中有几个小部件,它们都有自己的逻辑。您如何创建/包含这些小部件?
到目前为止,我遇到了几种解决方案。
作为控制器
第一个是 Twig 中的embed the widget as a controller(s)。
class WidgetController extends Controller
{
public function recentArticlesWidgetAction()
{
// some logic to generate to required widget data
// ...
// Render custom widget template with data
return $this->render('widgets/recentArticles.html.twig', array('data' => $data)
);
}
public function subscribeButtonWidgetAction()
{
// ...
return $this->render('widgets/subscribeButton.html.twig', array('data' => $data)
}
// Many more widgets
// ...
}
并像这样将其包含在“sidebar.html.twig”中
<div id="sidebar">
{# Recent Articles widget #}
{{ render(controller('AppBundle:Widget:recentArticlesWidget' )) }}
{# Subscribe-Button widget #}
{{ render(controller('AppBundle:Widget:subscribeButtonWidget' )) }}
{# and so on #}
</div>
作为服务
我还看到一些人将小部件注册为服务(可以直接在 Twig 中使用)。与小部件主类
// src/AppBundle/Service/RecentArticlesWidget.php
namespace AppBundle\Service;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RecentArticlesWidget
{
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function getRecentArticles()
{
// do some logic (use container for doctrine etc.)
}
}
然后注册为服务,
# src/AppBundle/Resources/config/services.yml
services:
recentArticlesWidget:
class: AppBundle\Service\RecentArticlesWidget
arguments: ["@service_container"]
传递给控制器中的模板,
namespace AppBundle\Controller;
class SidebarController {
public function showAction($request) {
// Get the widget(s)
$recentArticlesWidget = $this->get('recentArticlesWidget');
// Pass it (them) along
return $this->render('sidebar.html.twig', array('recentArticlesWidget' => $recentArticlesWidget));
}
}
所以它可以在 Twig 中像这样简单地使用
{# sidebar.html.twig #}
{{ recentArticlesWidget.getRecentArticles()|raw }}
或者,您还可以通过将服务添加到 Twig 配置中来直接将服务添加到 Twig 全局变量中。这样,控制器就不需要将它传递到视图中。
#app/config/config.yml
twig:
globals:
# twig_var_name: symfony_service
recentArticlesWidget: "@recentArticlesWidget"
作为 Twig 扩展
这与使用上面的服务(see the documentation)非常相似。您创建了一个与之前显示的服务几乎相同的 twig 扩展类
// src/AppBundle/Twig/RecentArticlesWidgetExtension.php
namespace AppBundle\Twig;
class RecentArticlesWidgetExtension extends \Twig_Extension
{
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function getFunctions()
{
return array(
"getRecentArticles" => new Twig_Function_Method($this, "getRecentArticles")
// register more functions
);
}
public function getRecentArticles()
{
// do some logic (use container for doctrine etc.)
}
// Some more functions...
public function getName()
{
return 'WidgetExtension';
}
}
将其注册为带有添加标签的服务
# src/AppBundle/Resources/config/services.yml
services:
recentArticlesWidget:
class: AppBundle\Twig\RecentArticlesWidgetExtension
arguments: [@service_container]
tags:
- { name: twig.extension }
并像 Twig 中的全局函数一样简单地使用它
{# sidebar.html.twig #}
{{ getRecentArticles() }}
想法
我注意到的一件事是,对于最后两种方法,逻辑和视图似乎不再分离。您基本上编写了一个小部件函数并让该函数输出小部件的完整 html。这似乎违背了 Symfony 试图强制执行的模块化和模式。
另一方面,为每个小部件调用不同的控制器或控制器操作(使用它们自己的树枝渲染)似乎可能需要比可能需要的更多处理。我不确定它是否真的会减慢任何速度,但我想知道它是否过度。
长话短说,有没有在 Symfony 中使用可重用小部件的最佳实践?我确信其中一些方法也可以混合使用,所以我只是想知道如何最好地解决这个问题。
【问题讨论】:
标签: symfony widget twig embed reusability