Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
b045442c
Commit
b045442c
authored
Nov 02, 2013
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added a cookbook about managing assets
parent
34fe312a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
0 deletions
+69
-0
doc/cookbook/assets.rst
doc/cookbook/assets.rst
+66
-0
doc/cookbook/index.rst
doc/cookbook/index.rst
+3
-0
No files found.
doc/cookbook/assets.rst
0 → 100644
View file @
b045442c
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 :doc:`URL generator service provider
</providers/url_generator>`.
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:
.. code-block:: jinja
{{ 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:
.. code-block:: jinja
{{ app.asset_path }}/css/styles.css
If you need to implement some logic independently of the asset, define a
service instead::
$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:
.. code-block:: jinja
{{ 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::
$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:
.. code-block:: jinja
{{ asset('/css/styles.css') }}
doc/cookbook/index.rst
View file @
b045442c
...
...
@@ -15,6 +15,7 @@ The cookbook section contains recipes for solving specific problems.
sub_requests
error_handler
multiple_loggers
assets
Recipes
-------
...
...
@@ -38,3 +39,5 @@ Recipes
* :doc:`Converting Errors to Exceptions <error_handler>`.
* :doc:`Using multiple Monolog Loggers <multiple_loggers>`.
* :doc:`Managing Assets in Templates <assets>`.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment