9

Include HTML files with blade

Can I include a .html file in stead of .php with Laravel 4 Blade?

My code:

@include('emails.templates.file')
  //file is email.html

file is automatically a .php file..

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Dirk Jan
  • 2,355
  • 3
  • 20
  • 38
  • 1
    Why would you need to? You technically already use HTML in blade templates. Just make sure the extension is .blade.php not just .php and that you spell "include" right :) – Winter Oct 15 '14 at 08:11
  • I have to include default emails in the browser – Dirk Jan Oct 15 '14 at 08:18
  • Right, but those default emails would display perfectly fine within a PHP file. With the answer by Weblineindia, it's no longer a blade include (defeats the purpose) – Winter Oct 15 '14 at 08:25
  • Possible duplicate of [Loading a laravel view with .html extension](https://stackoverflow.com/questions/20955571/loading-a-laravel-view-with-html-extension) – Prashant Pokhriyal Oct 23 '17 at 17:05

2 Answers2

19

While @PHPWeblineindia's solution worked for you, it's not really the Laravel way.

However, you can do what you want by telling Laravel's view system to also consider .html files. By default it looks for .blade.php files, and then falls back to .php files. You can add .html to the searched extensions by adding the following somewhere in your bootstrapping code:

// tells the view finder to look for `.html` files and run
// them through the normal PHP `include` process
View::addExtension('html', 'php');

This will actually put HTML as the highest priority, so make sure you don't have two different views called the same thing with different extensions.

alexrussell
  • 13,856
  • 5
  • 38
  • 49
  • You can also add this to the base controller to apply globally. – Peter Drinnan Mar 19 '15 at 20:49
  • True but I'd suggest in the application bootstrapping code (or a service provider, which is pretty much the same thing) rather than in a base controller just in case. – alexrussell Mar 20 '15 at 10:32
  • @alexrussell What do you mean by bootstrapping code and where would that be located at in Laravel? I am fairly new to Laravel running Laravel 5.2 and I've tried adding `View::addExtension` to my BaseController inside the protected method `setupLayout()`. The result of this is when trying to use include, Laravel simply returns `View [view.name] not found` where view.name has an `.html` extension. – C.Liddell Apr 07 '16 at 22:10
  • 2
    Based on the timing of my original answer, it was actually more geared to Laravel 4.2. However, application bootstrapping code is kinda universal - basically any code that runs before most of the rest. In L4.2 the standard (albeit dirty) way was to put it in the routes file. Since L5, we've have the service providers, so maybe the AppServiceProvider, or, ideally, new service provider that does the required view changes. That said, I don't know if the original `View::addExtension()` call still stands in L5. – alexrussell Apr 07 '16 at 22:31
  • @alexrussell Thanks for the help anyway. I'll open up a new question specific to Laravel 5, I do not believe that `View::addExtension()` works any longer. – C.Liddell Apr 07 '16 at 22:49
  • I was mistaken earlier. `View::addExtension()` does indeed work I just never called the `setupLayout()` method from within the controller. None the less if it helps anyone else, the new question is located [here](http://stackoverflow.com/questions/36488854/use-laravel-include-directive-with-non-php-files). – C.Liddell Apr 08 '16 at 00:17
  • @C.Liddell where I've to call `setupLayout()` method? – Prashant Pokhriyal Oct 06 '17 at 06:50
10

If its an external file then can you please try this:

<?php include app_path() . '/views/<path_to_layout/emails>/file.html'; ?>

Let me know if its still an issue.