|
| 1 | +.. index:: |
| 2 | + single: Templating Helpers; Assets Helper |
| 3 | + |
| 4 | +Assets Helper |
| 5 | +============= |
| 6 | + |
| 7 | +The assets helper's main purpose is to make your application more portable by |
| 8 | +generating asset paths: |
| 9 | + |
| 10 | +.. code-block:: html+php |
| 11 | + |
| 12 | + <link href="<?php echo $view['assets']->getUrl('css/style.css') ?>" rel="stylesheet"> |
| 13 | + |
| 14 | + <img src="<?php echo $view['assets']->getUrl('images/logo.png') ?>"> |
| 15 | + |
| 16 | +The assets helper can then be configured to render paths to a CDN or modify |
| 17 | +the paths in case your assets live in a sub-directory if your host (e.g. ``http://example.com/app``). |
| 18 | + |
| 19 | +Configure Paths |
| 20 | +--------------- |
| 21 | + |
| 22 | +By default, the assets helper will prefix all paths with a slash. You can |
| 23 | +configure this by passing a base assets path as the first argument of the |
| 24 | +constructor:: |
| 25 | + |
| 26 | + use Symfony\Component\Templating\Helper\AssetsHelper; |
| 27 | + |
| 28 | + // ... |
| 29 | + $templateEngine->set(new AssetsHelper('/foo/bar')); |
| 30 | + |
| 31 | +Now, if you use the helper, everything will be prefixed with ``/foo/bar``: |
| 32 | + |
| 33 | +.. code-block:: html+php |
| 34 | + |
| 35 | + <img src="<?php echo $view['assets']->getUrl('images/logo.png') ?>"> |
| 36 | + <!-- renders as: |
| 37 | + <img src="/foo/bar/images/logo.png"> |
| 38 | + --> |
| 39 | + |
| 40 | +Absolute Urls |
| 41 | +------------- |
| 42 | + |
| 43 | +You can also specify a url to use in the second parameter of the constructor:: |
| 44 | + |
| 45 | + // ... |
| 46 | + $templateEngine->set(new AssetsHelper(null, 'http://cdn.example.com/')); |
| 47 | + |
| 48 | +Now urls are rendered like ``http://cdn.example.com/images/logo.png``. |
| 49 | + |
| 50 | +Versioning |
| 51 | +---------- |
| 52 | + |
| 53 | +To avoid using the cached resource after updating the old resource, you can |
| 54 | +use versions which you bump every time you release a new project. The version |
| 55 | +can be specified in the third argument:: |
| 56 | + |
| 57 | + // ... |
| 58 | + $templateEngine->set(new AssetsHelper(null, null, '328rad75')); |
| 59 | + |
| 60 | +Now, every url is suffixed with ``?328rad75``. If you want to have a different |
| 61 | +format, you can specify the new format in fourth argument. It's a string that |
| 62 | +is used in :phpfunction:`sprintf`. The first argument is the path and the |
| 63 | +second is the version. For instance, ``%s?v=%s`` will be rendered as |
| 64 | +``/images/logo.png?v=328rad75``. |
| 65 | + |
| 66 | +Multiple Packages |
| 67 | +----------------- |
| 68 | + |
| 69 | +Asset path generation is handled internally by packages. The component provides |
| 70 | +2 packages by default: |
| 71 | + |
| 72 | +* :class:`Symfony\\Component\\Templating\\Asset\\PathPackage` |
| 73 | +* :class:`Symfony\\Component\\Templating\\Asset\\UrlPackage` |
| 74 | + |
| 75 | +You can also use multiple packages:: |
| 76 | + |
| 77 | + // ... |
| 78 | + $templateEngine->set(new AssetsHelper()); |
| 79 | + |
| 80 | + $templateEngine->get('assets')->addPackage('images', new PathPackage('/images/')); |
| 81 | + $templateEngine->get('assets')->addPackage('scripts', new PathPackage('/scripts/')); |
| 82 | + |
| 83 | +This will setup the assets helper with 3 packages: the default package which |
| 84 | +defaults to ``/`` (set by the constructor), the images package which prefixes |
| 85 | +it with ``/images/`` and the scripts package which prefixes it with |
| 86 | +``/scripts/``. |
| 87 | + |
| 88 | +If you want to set another default package, you can use |
| 89 | +:method:`Symfony\\Component\\Templating\\Helper\\AssetsHelper::setDefaultPackage`. |
| 90 | + |
| 91 | +You can specify which package you want to use in the second argument of |
| 92 | +:method:`Symfony\\Component\\Templating\\Helper\\AssetsHelper::getUrl`: |
| 93 | + |
| 94 | +.. code-block:: php+html |
| 95 | + |
| 96 | + <img src="<?php echo $view['assets']->getUrl('foo.png', 'images') ?>"> |
| 97 | + <!-- renders as: |
| 98 | + <img src="/images/foo.png"> |
| 99 | + --> |
| 100 | + |
| 101 | +Custom Packages |
| 102 | +--------------- |
| 103 | + |
| 104 | +You can create your own package by extending |
| 105 | +:class:`Symfony\\Component\\Templating\\Package\\Package`. |
0 commit comments