Using "path" and "asset" for data without templates

When creating templates with Twig, the path and asset functions are easy to use.

 <a href="{{ path('my_route') }}"><img src="{{ asset('bundles/acmedemo/my_image.png') }}" /></a> 

However, some of my data comes from non-twig files or from a database. What will be the correct way to solve these functions?

So far, I am using regex replace ( preg_replace_callback ) for the path function. But is there a better way?

+2
source share
1 answer

I am proud to present my first public mini-project, StaticBundle . This largely allows you to include any file in a bunch directly into the template.

Customization

EDIT The package can now be installed using the composer, see readme instructions.

Add the following to deps :

 [KGStaticBundle] git=git://github.com/kgilden/KGStaticBundle.git target=bundles/KG/StaticBundle 

Run bin/vendors install .

Register the namespace in app/autoload.php :

 'KG' => __DIR__.'/../vendor/bundles', 

Register the node in app/AppKernel.php :

 new KG\StaticBundle\KGStaticBUndle(), 

Main use

Suppose the src/Acme/Bundle/DemoBundle/Static/hello.txt ready to be included in the template. We need to use the file function:

 {# src/Acme/Bundle/DemoBundle/Resources/views/Demo/index.html.twig #} {{ file('@AcmeDemoBundle/Static/hello.txt') }} 

The logical name resolves to the actual path, and a simple file_get_contents retrieves the data.

+3
source

Source: https://habr.com/ru/post/904172/


All Articles