scaytrase / symfony-switchable-theme
Web配置表单
Requires
- php: >=5.3.3
- doctrine/doctrine-bundle: ~1.2
- doctrine/orm: ~2.2,>=2.2.3
- scaytrase/symfony-autoregistry: ~2.0
- sensio/framework-extra-bundle: ~3.0
- symfony/assetic-bundle: ~2.6
- symfony/console: ~2.3
- symfony/form: ~2.3
- symfony/framework-bundle: ~2.3
- twig/extensions: ~1.2
- twig/twig: ~1.17
This package is not auto-updated.
Last update: 2022-02-01 12:40:53 UTC
README
Twig {% extends %}
子句的运行时选择
运行时可切换和用户可配置的Twig布局
最初是为了与 BraincraftedBootstrapBundle 主题切换器一起使用而开发的,允许您在运行时动态定义扩展的Twigt模板。此外,如果您需要,您还可以为您的主题编译资产。当前版本支持主题配置,因此主题可以预先配置(并且可以为单个主题提供多个配置实例),以及每个配置都可以单独编译。
用法
基本示例
请参阅 测试 以获取基本用法示例。您可以找到简单的 ThemeInterface
实现和逻辑示例。
通用
要使用主题切换,您应该简单扩展由 ThemeRegistry::getTemplate(theme,layout)
方法返回的模板名称。该方法返回一个表示实际扩展的模板的字符串(即 SomeTheme::some_layout.html.twig
)或null。
class MyTheme implements ThemeInterface { public function get($layout = 'base') {return 'MyBundle:MyTheme:base.html.twig';} public function all() {return array('base' => 'MyBundle:MyTheme:base.html.twig');} public function getType() {return 'my_theme';} }
{# MyBundle:MyTheme:base.html.twig #}
Here your theme basic template goes.
{# MyBundle::base.twig.html #} {% extends theme_registry.template('my_theme','base') %}
后备布局
如果您不确定模板是否存在,则可以使用多扩展Twigt子句并提供后备模板来使用主题切换,例如:
{# MyBundle::base.twig.html #} {% extends [theme_registry.template('my_theme','base'), 'MyBundle::fallback.html.twig'] %}
主要思想是“my_theme”和“base”字符串可以用从数据库或用户会话中填充的变量替换。这允许您根据给定条件动态选择父模板。
可配置主题
要使用具有主题配置(主题实例)的主题,您应该为getTemplate的第一个参数提供ThemeInstance
对象。可配置主题应实现ConfigurableThemeInterface
以能够进行配置。
{# MyBundle::base.twig.html #} {% extends theme_registry.template(themeInstance,'layout') %}
可编译主题
某些主题在可以使用之前应进行处理。例如,某些主题资产应进行编译。为了处理这些情况,创建了CompilableThemeInterface
。可以通过调用CompilableThemeInterface::compile()
方法来编译主题。这种方式最好与可配置接口一起使用,这样您可以为单个主题的不同配置编译。
作为一个快速示例,有一个 抽象基类 用于创建基于Bootstrap 3.0的主题。您可以实现访问方法,将类指向Bootstrap less模板、供应商资产等,并在调用CompilableThemeInterface::compile()
时生成正确的编译结果bootstrap.css
。
基于braincrafted/bootstrap-bundle的Bootstrap编译工作流程的主题编译方法