项目作者: fkrauthan

项目描述 :
A small assetic filter for symfony to fix all url paths at css documents to correct urls
高级语言: PHP
项目地址: git://github.com/fkrauthan/FkrCssURLRewriteBundle.git
创建时间: 2012-06-09T11:57:21Z
项目社区:https://github.com/fkrauthan/FkrCssURLRewriteBundle

开源协议:

下载


FkrCssURLRewriteBundle

A small assetic filter for Symfony2 to fix all url paths in css documents to correct urls. It also provides easy resource linking across platforms.

Installation

Bring in the vendor libraries

This can be done in three different ways:

Method #1) Use composer

  1. "require": {
  2. "fkr/cssurlrewrite-bundle": "*"
  3. }

Method #2) Use git submodules

  1. git submodule add git://github.com/fkrauthan/FkrCssURLRewriteBundle.git vendor/bundles/Fkr/CssURLRewriteBundle

Method #3) Use deps file

  1. [FkrCssURLRewriteBundle]
  2. git=git://github.com/fkrauthan/FkrCssURLRewriteBundle.git
  3. target=bundles/Fkr/CssURLRewriteBundle

Register the Fkr namespaces

If not using composer, register the Fkr namespace with Symfony’s
autoloader:

  1. // app/autoload.php
  2. $loader->registerNamespaces(array(
  3. 'Fkr' => __DIR__.'/../vendor/bundles',
  4. // your other namespaces
  5. ));

Add CssURLRewriteBundle to your application kernel

  1. // app/AppKernel.php
  2. public function registerBundles()
  3. {
  4. return array(
  5. // ...
  6. new Fkr\CssURLRewriteBundle\FkrCssURLRewriteBundle(),
  7. // ...
  8. );
  9. }

Configuration

  1. # app/config.yml
  2. fkr_css_url_rewrite:
  3. rewrite_only_if_file_exists: true
  4. clear_urls: true
  • rewrite_only_if_file_exists: If true (default) only rewrites url if the resource exists in the …/BundleFolder/Resources/public/ folder.
  • clear_urls: If true (default) the generated url gets normalized. For example if the url normaly is .../less/../img this option makes .../img.

Usage

Standard usage

If you place your css file for example in

  1. .../BundleFolder/Resources/public/css

and you have your images in

  1. ../BundleFolder/Resources/public/img

then you have in yours css file somthing like this

  1. background-image: url(../img/MyImageName.png)

Now you have to call

  1. app/console assets:install

Now if you have somthing like this in your template

  1. {% stylesheets filter='css_url_rewrite,?yui_css'
  2. '@BundleName/Resources/public/css/mycssfile.css'
  3. %}
  4. <link rel="stylesheet" href="{{ asset_url }}" type="text/css" />
  5. {% endstylesheets %}

Now the filter rewrites your url in your css file

  1. background-image: url(../img/MyImageName.png) => background-image: url(../bundles/bundlename/css/../img/MyImageName.png)

And everything works fine.

Extended usage

You can link images from other bundles by using the @ annotation. For example if you write this in you css file

  1. background-image: url(@BundleNameBundle/img/MyImageName.png)

Now the filter rewrite this url in your css file

  1. background-image: url(@BundleNameBundle/img/MyImageName.png) => background-image: url(../bundles/bundlename/img/MyImageName.png)

License

Resources/meta/LICENSE