【问题标题】:Symfony2 Twig theming from database?来自数据库的 Symfony2 Twig 主题?
【发布时间】:2012-03-17 06:02:37
【问题描述】:

如何使用 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

我已经检查了这两个捆绑包:

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

【问题讨论】:

    标签: symfony twig


    【解决方案1】:

    我们使用 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);
    }
    

    您需要考虑 one problem 和 LiipThemeBundle。

    【讨论】:

    • 感谢您的回答,但我真的不需要使用捆绑包来更改路径!请问您为什么要更改 RouteListener 中的活动主题?编辑:无需切换主题传递主题参数。
    • @Gremo LiipThemeBundle 是一种将主题支持添加到项目中的方法。那是因为“Symfony 必须从 Resources/views/{theme_name} 加载正确的模板,如果模板不存在,则回退到 Resources/views/Default。”。而且它不是 RouteListener,它是 EarlyKernelRequest 监听器。 “即不需要切换主题传递主题参数”是什么意思?
    • @Gremo - 你有没有得到这个工作我有同样的问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多