This function generates an URL for project assets.

$app['twig']->addFunction(new \Twig_SimpleFunction('asset', function ($uri = '') use ($app) {
  return '//' . $app['request']->getHttpHost() . $app['request']->getBasePath() . '/PATH_TO_ASSETS/' . trim($uri, '/\');
}));

Usage:

{{ asset('css/styles.css') }}
{{ asset('js/main.js') }}

Here are other solutions:

http://silex.sensiolabs.org/doc/cookbook/assets.html

Categories: PHP