a SensioLabs Product

The PHP micro-framework
based on the Symfony Components

You are reading the documentation for Silex 1.3. Switch to the documentation for Silex 2.0.

Questions & Feedback

License

Creative Commons License Silex documentation is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.

Managing Assets in TemplatesΒΆ

A Silex application is not always hosted at the web root directory. To avoid repeating the base path whenever you link to another page, it is highly recommended to use the URL generator service provider.

But what about images, stylesheets, or JavaScript files? Their URLs are not managed by the Silex router, but nonetheless, they need to get prefixed by the base path. Fortunately, the Request object contain the application base path that needs to be prepended:

// generate a link to the stylesheets in /css/styles.css
$request->getBasePath().'/css/styles.css';

And doing the same in a Twig template is as easy as it can get:

1
{{ app.request.basepath }}/css/styles.css

If your assets are hosted under a different host, you might want to abstract the path by defining a Silex parameter:

$app['asset_path'] = 'http://assets.examples.com';

Using it in a template is as easy as before:

1
{{ app.asset_path }}/css/styles.css

If you need to implement some logic independently of the asset, define a service instead:

1
2
3
4
5
$app['asset_path'] = $app->share(function () {
    // implement whatever logic you need to determine the asset path

    return 'http://assets.examples.com';
});

Usage is exactly the same as before:

1
{{ app.asset_path }}/css/styles.css

If the asset location depends on the asset type or path, you will need more abstraction; here is one way to do that with a Twig function:

1
2
3
4
5
6
7
8
9
$app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
    $twig->addFunction(new \Twig_SimpleFunction('asset', function ($asset) {
        // implement whatever logic you need to determine the asset path

        return sprintf('http://assets.examples.com/%s', ltrim($asset, '/'));
    }));

    return $twig;
}));

The asset function can then be used in your templates:

1
{{ asset('/css/styles.css') }}
Website powered by Symfony and Twig, deployed on
The Silex logo is © 2010-2017 SensioLabs