Our project allows the users to create custom theme files. Because of the way the project is structured, we have to render these files and save the output. The project uses Symfony2.
In order to render this Twig template and save the output to another file, I use a service which takes @templating as its argument.
The services.yml defines the service like this:
theme_renderer:
class: ApiBundle\Service\ThemeRenderer
arguments: [ @templating ]
This gives me a TwigEngine object that I can use like this:
public function __construct(TwigEngine $templateEngine) {
$this->templateEngine = $templateEngine;
}
public function renderTheme($filePath, $themeSettings) {
...
$renderedFileContents = $this->templateEngine->render($sourceFilePath, $themeSettings);
...
}
This works fine.
However, if the template contains an {{ asset('images', 'some_image') }} tag, the rendering fails. The exception is:
An exception has been thrown during the rendering of a template ("There is no "some_image" asset package.") in "<template_file_path_here>" at line 2
I need to be able to give a custom (CDN actually) URL for the rendering of the asset() tag. How would I go about doing this?
I think exception message There is no "some_image" asset package.
is clear. Second argument of asset
function is package name. Packages for templating component are defined under framework.templating
key in Symfony configuration (etc. config.yml).
http://symfony.com/doc/current/reference/configuration/framework.html#packages
# app/config/config.yml
framework:
# ...
templating:
packages:
avatars:
base_urls: 'http://static_cdn.example.com/avatars'
Then you can use asset('my_image.jpg', 'avatars')
. So, I suppose you have not defined package some_image
or configured it improperly.