概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:40:43 | 142 次阅读 | 评论: 0 | 来源: 网络整理

Assetic filters can be applied to individual files, groups of files or even, as you’ll see here, files that have a specific extension. To show you how to handle each option, suppose that you want to use Assetic’s CoffeeScript filter, which compiles CoffeeScript files into JavaScript.

The main configuration is just the paths to coffee, node and node_modules. An example configuration might look like this:

  • YAML
    # app/config/config.yml
    assetic:
        filters:
            coffee:
                bin:        /usr/bin/coffee
                node:       /usr/bin/node
                node_paths: [/usr/lib/node_modules/]
    
  • XML
    <!-- app/config/config.xml -->
    <assetic:config>
        <assetic:filter
            name="coffee"
            bin="/usr/bin/coffee/"
            node="/usr/bin/node/">
            <assetic:node-path>/usr/lib/node_modules/</assetic:node-path>
        </assetic:filter>
    </assetic:config>
    
  • PHP
    // app/config/config.php
    $container->loadFromExtension('assetic', array(
        'filters' => array(
            'coffee' => array(
                'bin'  => '/usr/bin/coffee',
                'node' => '/usr/bin/node',
                'node_paths' => array('/usr/lib/node_modules/'),
            ),
        ),
    ));
    

Filter a single File

You can now serve up a single CoffeeScript file as JavaScript from within your templates:

  • Twig
    {% javascripts '@AppBundle/Resources/public/js/example.coffee' filter='coffee' %}
        <script src="{{ asset_url }}"></script>
    {% endjavascripts %}
    
  • PHP
    <?php foreach ($view['assetic']->javascripts(
        array('@AppBundle/Resources/public/js/example.coffee'),
        array('coffee')
    ) as $url): ?>
        <script src="<?php echo $view->escape($url) ?>"></script>
    <?php endforeach ?>
    

This is all that’s needed to compile this CoffeeScript file and serve it as the compiled JavaScript.

Filter multiple Files

You can also combine multiple CoffeeScript files into a single output file:

  • Twig
    {% javascripts '@AppBundle/Resources/public/js/example.coffee'
                   '@AppBundle/Resources/public/js/another.coffee'
        filter='coffee' %}
        <script src="{{ asset_url }}"></script>
    {% endjavascripts %}
    
  • PHP
    <?php foreach ($view['assetic']->javascripts(
        array(
            '@AppBundle/Resources/public/js/example.coffee',
            '@AppBundle/Resources/public/js/another.coffee',
        ),
        array('coffee')
    ) as $url): ?>
        <script src="<?php echo $view->escape($url) ?>"></script>
    <?php endforeach ?>
    

Both files will now be served up as a single file compiled into regular JavaScript.

Filtering Based on a File Extension

One of the great advantages of using Assetic is reducing the number of asset files to lower HTTP requests. In order to make full use of this, it would be good to combine all your JavaScript and CoffeeScript files together since they will ultimately all be served as JavaScript. Unfortunately just adding the JavaScript files to the files to be combined as above will not work as the regular JavaScript files will not survive the CoffeeScript compilation.

This problem can be avoided by using the apply_to option, which allows you to specify which filter should always be applied to particular file extensions. In this case you can specify that the coffee filter is applied to all .coffee files:

  • YAML
    # app/config/config.yml
    assetic:
        filters:
            coffee:
                bin:        /usr/bin/coffee
                node:       /usr/bin/node
                node_paths: [/usr/lib/node_modules/]
                apply_to:   ".coffee$"
    
  • XML
    <!-- app/config/config.xml -->
    <assetic:config>
        <assetic:filter
            name="coffee"
            bin="/usr/bin/coffee"
            node="/usr/bin/node"
            apply_to=".coffee$" />
            <assetic:node-paths>/usr/lib/node_modules/</assetic:node-path>
    </assetic:config>
    
  • PHP
    // app/config/config.php
    $container->loadFromExtension('assetic', array(
        'filters' => array(
            'coffee' => array(
                'bin'      => '/usr/bin/coffee',
                'node'     => '/usr/bin/node',
                'node_paths' => array('/usr/lib/node_modules/'),
                'apply_to' => '.coffee$',
            ),
        ),
    ));
    

With this option, you no longer need to specify the coffee filter in the template. You can also list regular JavaScript files, all of which will be combined and rendered as a single JavaScript file (with only the .coffee files being run through the CoffeeScript filter):

  • Twig
    {% javascripts '@AppBundle/Resources/public/js/example.coffee'
                   '@AppBundle/Resources/public/js/another.coffee'
                   '@AppBundle/Resources/public/js/regular.js' %}
        <script src="{{ asset_url }}"></script>
    {% endjavascripts %}
    
  • PHP
    <?php foreach ($view['assetic']->javascripts(
        array(
            '@AppBundle/Resources/public/js/example.coffee',
            '@AppBundle/Resources/public/js/another.coffee',
            '@AppBundle/Resources/public/js/regular.js',
        )
    ) as $url): ?>
        <script src="<?php echo $view->escape($url) ?>"></script>
    <?php endforeach ?>
    
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务