3

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.

Community
  • 1
  • 1
C.Liddell
  • 1,084
  • 10
  • 19
  • 1
    U sort of mentioned this and disagreed but I don't think this is the best approach, and I'm not sure why you would want to use blade outside of an html file. The point of it is to add some more functionality to the html, but if ur in something like js u can already iterate without blade – tam5 Apr 07 '16 at 23:23
  • 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. – Joel Hinz Apr 07 '16 at 23:32
  • 1
    Excellent point @JoelHinz. Post that as an answer as it is the correct solution. – Bogdan Apr 07 '16 at 23:35
  • Thanks for confirming, @Bogdan, I will. :) – Joel Hinz Apr 07 '16 at 23:39

1 Answers1

0

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.

Joel Hinz
  • 24,719
  • 6
  • 62
  • 75
  • There's only one file with a unique name. I am using the blade engine not the html one that I linked to. You may very well be right about `View::addExtension('blade.js', 'blade')` still working, however after adding it to my BaseController it does not seem to work. My BaseController looks like [this](http://pastebin.com/xf5AEKPg). – C.Liddell Apr 07 '16 at 23:47
  • 1
    @C.Liddell Since I see you're trying to implement the old Laravel 4.2 controller layout approach (which was discontinued in favour of template inheritance) where exactly are you calling that `setupLayout` method? Because since Laravel 5 doesn't have that method as part of the core controller class, it does not call that automatically for you anymore. – Bogdan Apr 07 '16 at 23:57
  • @Bogdan Add your above comment as an answer. It was just as simple as calling the `setupLayout()` method, I was mistaken thinking that it was automatically called by Laravel. Thanks, both of you, for the help. – C.Liddell Apr 08 '16 at 00:12
  • Thanks both. Went to bed so I couldn't respond. @Bogdan, feel free to take credit and get the accepted answer. :) – Joel Hinz Apr 08 '16 at 09:47