Use Laravel @include directive with non PHP files?

2019-07-11 13:55发布

问题:

I have been looking for a way to use Laravels Blade templating engine with non PHP file extensions (instead of file_name.blade.php being able to use file_name.blade.js or file_name.blade.css) to no avail. I found a post that seems to describe how to do exactly this for Laravel 4, however it no longer seems to work in Laravel 5. The reason why I would like to do this is I have a Javascript file that looks something like this:

//JS stuff
@foreach($Model->Values as $Value)
    {{ $Value->name . " = " . $Value->content . ";"}}
@endforeach
//More JS stuff

And a blade/php file that looks something like this:

//Blade stuff
<script type="text/javascript">
@include('js');
</script>

And this seems to be the nicest way of passing each value to javascript. Even if there is a nicer way of passing these values to javascript, the reason behind this question is to find if there is a way to parse a non PHP file with blade as I think that could be immensely useful.

回答1:

I thought View::addExtension('blade.js', 'blade') would work in L5 as well. The method hasn't changed. But do note you'll want the blade engine, not the html one like in the post you linked to. Where to put it is a good question, though. Also, you mentioned your two files have the same name before the extensions - that might screw it up, too.