dflydev/theme-twig-extension

主题的 Twig 扩展

dev-master / 1.0.x-dev 2013-01-15 04:23 UTC

This package is auto-updated.

Last update: 2024-08-29 03:29:11 UTC


README

一个为 ThemeTwig 扩展。

安装

通过 Composer 作为 dflydev/theme-twig-extension 安装。

使用

以下示例使用 ResourceUrlGeneratorInterface 的 Symfony 路由实现。

<?php
use Dflydev\Theme\ResourceUrlGenerator\SymfonyRoutingResourceUrlGenerator;
use Dflydev\Twig\Extension\Theme\ThemeTwigExtension;

// Assumes $themeProvider, $pathMapper, $urlGenerator, and $twig
// are already created and ready to be used.

$resourceUrlGenerator = new SymfonyRoutingResourceUrlGenerator(
    $themeProvider,
    $pathMapper,
    $urlGenerator
);

$themeTwigExtension = new ThemeTwigExtension(
    $resourceUrlGenerator
);

$twig->addExtension($themeTwigExtension);

一旦启用,主题资源可以通过以下方式引用

{{ theme_resource('css/main.css') }}

如果 css/main.css 可以直接通过主题实例的 docroot 访问(由 PathMapperInterface 确定),则将使用指向公共资源的 URL。

如果 css/main.css 不能 直接访问,将使用 UrlGeneratorInterface 实现来生成指向嵌入式主题控制器的 URL。

ResourceUrlGeneratorInterface 的所有实现都应遵循以下模式

  1. 检查资源是否可以公开访问,如果可以,则使用该资源。
  2. 生成一个回退 URL 以暴露非公共资源。

控制器需求示例(Silex + Symfony 路由)

  • _dflydev_typed_theme_handler:期望三个参数,typenameresource。名称可以通过调用 setTypedRouteName() 来更改。

    示例

    <?php
    $app->get('/_theme_typed/{type}/{name}/resources/{resource}', function($type, $name, $resource) use ($app)  {
        // do something to handle the theme request and return the
        // contents of the theme resource manually.
    })
    ->assert('name', '.+')
    ->assert('resource', '.+')
    ->bind('_dflydev_typed_theme_handler');
  • _dflydev_theme_handler:期望两个参数,nameresource。名称可以通过调用 setTypedRouteName() 来更改。

    示例

    <?php
    $app->get('/_theme/{name}/resources/{resource}', function($name, $resource) use ($app) {
        // do something to handle the theme request and return the
        // contents of the theme resource manually.
    })
    ->assert('name', '.+')
    ->assert('resource', '.+')
    ->bind('_dflydev_theme_handler');

许可

MIT,请参阅 LICENSE。

社区

如果您有问题或想帮忙,请加入我们 irc.freenode.net 上的 #dflydev 频道。