Symfony2 Twig来自数据库吗?

时间:2012-02-27 18:53:11

标签: symfony twig

如何使用Symfony2添加对主题的支持,其中主题(当前主题的路径)存储在每个用户的数据库中?例如:

-------------------------------------------------------------
| User              | id | username | password | theme_name |
-------------------------------------------------------------
  Bob                 1    Bob        327n829    /Default
  Alice               2    Alice      2c839n42   /Pink

因此,对于给定用户,如果模板不存在,Symfony必须从Resources/views/{theme_name}加载正确的模板并回退到Resources/views/Default

我检查过这两个包:

但两者似乎都不符合我的需要。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:4)

我们使用LiipThemeBundle和我们的内核监听器:

public function onEarlyKernelRequest(GetResponseEvent $event)
{
    $request = $event->getRequest();

    if ($request->attributes->has('_theme') === false) {
        // if you use annotation with doctrine, you'll need to register them before that line
        $user = $this->container->get('doctrine.orm.entity_manager')->getRepository('UserBundle:User')->findBy(...);

       // you probably will want to set _theme in request context, not in the $request object
        $request->attributes->set('_theme', $user->getTheme());
        $this->container->get('liip_theme.active_theme')->setName($user->getTheme());
    }
    //$this->router->setContext($context);
}

你需要考虑使用LiipThemeBundle one problem