rcrowe / TwigBridge

Give the power of Twig to Laravel
MIT License
894 stars 168 forks source link

Possible to use Twig's source() function for a non-view file? #315

Open cviebrock opened 7 years ago

cviebrock commented 7 years ago

I've been researching ways to inline SVGs into my Twig templates. One suggestion was to use Twig's source function, which returns the content of a template without rendering it.

However, in my L5.4 project I have my templates in /resources/views and my SVGs in /resources/assets/svg.

Because (I think) the way TwigBridge sets up the Twig_Loader_Filesystem, it's only looking in the first directory for the argument I pass to source(). So I get an error like:

Template "logo.svg" is not defined (View [logo.svg] not found.) in "partials.pageHeader" at line 3.

Any suggestions on how to configure the loader to look in two different places?

cviebrock commented 7 years ago

Follow-up to my own issue: I was able to kinda solve this by editing config/view.php in my Laravel project to tell it that I have another possible view directory:

    'paths' => [
        resource_path('views'),
        resource_path('assets/svg')
    ],

However, it still assumes that templates end with "twig" (as defined in config/twigbridge.php). So it only works if I rename my "logo.svg" file to "logo.twig".

I'd like to avoid this if possible, so my original question still stands.

Flvn404 commented 6 years ago

+1 I'd like to avoid this too if possible

willpower232 commented 5 years ago

If anyone finds this, the answer is to create your own extension to call file_get_contents.

Its safer if you don't pass a path to that function from your twig but you do you.

See https://github.com/willpower232/TOTPBTF3/commit/fcf5f66c8c6db52447f797c008eaf571feddf7fe for reference.