LiipThemeBundle可以让您为您的每个Bundle添加主题。该主题一般位于您的Bundle目录的<code>Resources/themes/<主题名></code>或正常的 <code>Resources/views</code>(如果没有找到的话)文件夹中。
进入Symfony2.1.x的根目录,打开 <code>composer.json</code> 文件,在"require":选项中添加:
"require": {
... ,
"liip/theme-bundle": "dev-master"
},
然后运行composer脚本,下载 LiipThemeBundle
php composer.phar update
下载完成后,在内核中启用 LiipThemeBundle
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Liip\ThemeBundle\LiipThemeBundle(),
);
}
# app/config/routing.yml
liip_theme:
resource: "@LiipThemeBundle/Resources/config/routing.xml"
prefix: /theme
在配置文件中需要列出所有的主题列表及当前主题。注意,当前主题必须在主题列表中
# app/config/config.yml
themes: ['web', 'tablet', 'phone']
active_theme: 'web'
可以基于 Cookie 来选择当前主题:
cookie:
name: NameOfTheCookie
lifetime: 31536000 # 1 year in seconds
path: /
domain: ~
secure: false
http_only: false
也可以基于用户代理自动设置主题 Cookie:
autodetect_theme: true
autodetect_theme 选项也可以设为 DIC 服务 ID,该服务需要实现 <code>Liip\ThemeBundle\Helper\DeviceDetectionInterface</code> 接口。
如果不想让用户自己改变主题,可以停用该 Bundle 的控制器:
load_controllers: false
Bundle 在应用模板时是按以下层级顺序进行的(让我们先假设有一个名为 <code>phone</code> 的主题,其模板文件为 <code>BundleName/Resources/template.html.twig</code> @BundleName/Resources/template.html.twig):
覆写主题目录: <code>app/Resources/themes/phone/BundleName/template.html.twig</code>
覆写视图目录: <code>app/Resources/BundleName/views/template.html.twig</code>
Bundle的主题目录: <code>src/BundleName/Resources/themes/phone/template.html.twig</code>
Bundle的视图目录: <code>src/BundleName/Resources/views/template.html.twig</code>
举个例子,如果您想将一些自定义的 TwigBundle 错误页面集成到您的主题架构中,那么您将需要使用这个目录结构: <code>app/Resources/themes/phone/TwigBundle/Exception/error404.html.twig</code>
当使用全局范围的模板时,将使用下列顺序(如模板文件为 <code>::template.html.twig</code> 的名为 <code>phone</code> 的主题):
覆写主题目录: <code>app/Resources/themes/phone/template.html.twig</code>
覆写视图目录: <code>app/Resources/views/template.html.twig</code>
您可以通过一些配置命令来改变层级顺序: <code>path_patterns.app_resource</code> 、 <code>path_patterns.bundle_resource</code> 和 <code>path_patterns.bundle_resource_dir</code> 。 如:
path_patterns:
app_resource:
- %%app_path%%/themes/%%current_theme%%/%%template%%
- %%app_path%%/themes/fallback_theme/%%template%%
- %%app_path%%/views/%%template%%
bundle_resource:
- %%bundle_path%%/Resources/themes/%%current_theme%%/%%template%%
- %%bundle_path%%/Resources/themes/fallback_theme/%%template%%
bundle_resource_dir:
- %%dir%%/themes/%%current_theme%%/%%bundle_name%%/%%template%%
- %%dir%%/themes/fallback_theme/%%bundle_name%%/%%template%%
- %%dir%%/%%bundle_name%%/%%override_path%%
占位符
说明
示例
<code>%app_path%</code>
应用程序Path where application located
<code>app</code>
<code>%bundle_path%</code>
Bundle所在目录
<code>src/Vendor/CoolBundle/VendorCoolBundle</code>
<code>%bundle_name%</code>
Bundle名
<code>VendorCoolBundle</code>
<code>%dir%</code>
首先查找的目录
<code>%current_theme%</code>
当前激活的主题名
<code>%template%</code>
模板名
<code>view.html.twig</code>
<code>%override_path%</code>
同上,只是带视图目录
<code>views/list.html.twig</code>
对于这个问题,可参见 ThemeRequestListener.
如果在请求初期没有模板被渲染,您可以使用主题服务,从而主地改变主题:
$activeTheme = $container->get('liip_theme.active_theme');
echo $activeTheme->getName();
$activeTheme->setName("phone");
当使用 assetic:dump 命令进行资源转储时, assetic 只检查用以生成资源的 Resources/views 目录,它并不检查 Resources/{theme}/views 目录。
当前最简单的解决办法:既然问题出在 assetic 不扫描主题目录,那么将 assetic 区块放入 Bundle/Ressources/views 目录中的模板,并从主题模板中包含它。
本文转自 firehare 51CTO博客,原文链接:http://blog.51cto.com/firehare/1072438,如需转载请自行联系原作者