3

Based on the Laravel notification email template, here is what I'm trying to do.

I try to have a layout for emailings, with variables, in order to have some styles, defined in my layout, but accessible in my email views.

Here is an example:

emails/layout.blade.php

<!DOCTYPE html>
<html>
<head> [...]
</head>
@php
$style = ['body' => 'font-size:12px;color:#000;', 'paragraph' => 'font-size:10px;color:#eee;'];
@endphp
<body style="{{ $style['body'] }}">
@yield('content')
</body>
</html>

And in my email view :

@extends('emails.layout')

@section('content')

<p style="{{ $style['paragraph'] }}">
   Hi there!
</p>

@endsection

BUT this is not working as the $style variable is not accessible in my section.

SO, the only way I make it working is including a PHP file in both my layout and my view, and I'm pretty sure this is not the way I should do this with Laravel...

<?php include_once(base_path('resources/views/emails/variables.php')); ?>

Is there a way to achieve this? Thanks guys!

Guillaume
  • 443
  • 5
  • 11
  • When you extend you can pass a variable through as seen on this answer: http://stackoverflow.com/questions/16118104/how-do-i-pass-a-variable-to-the-layout-using-laravel-blade-templating not sure if that helps in this situation, but maybe you can change it a bit to make it work – Antony Thompson Nov 24 '16 at 11:57

3 Answers3

3

Ideally, you shouldn't be trying to include PHP variables in this way. When returning your view from your controller, you should be passing it all the variables it needs to render the view:

class YourController
{
    public function yourMethod()
    {
        $styles = getYourStyles();

        return view('emails.view')->with([
            'styles' => $styles
        ]);
    }
}

This will make your styles available in the $styles variable throughout your view.

Alternatively, you could use the config to house your variables and then you can simply access them whenever you need them.

In config/styles.php

return [
    'paragraph' => 'font-size: 12px; font-weight: bold;'
];

In your template:

<p style="{{ config('styles.paragraph') }}">
Jonathon
  • 15,873
  • 11
  • 73
  • 92
2
The standard way to do this would be
Inside /app/Providers/ComposerServiceProvider.php boot function

 view()->composer('*',function($view){
        $style = "datas";

        $view->with(['style' => $style]);

    });

now your variable will be available across the blade template if you want to include other template only just give name instead of * for eg. /view/user/view.blade.php

view()->composer('user.view',function($view){
                $style = "datas";

                $view->with(['style' => $style]);

            });
ujwal dhakal
  • 2,289
  • 2
  • 30
  • 50
1

There are a couple of ways to go about this. One is to set your named variable in the Controller and yet another one of the many options would be to simply set the variable directly within your Blade View.

SETTING VARIABLE WITHIN BLADE VIEW - [ ODD IDEA ]

     <!--view_script.blade-->
     <?php  
          $style = [
                     'body'      => 'font-size:12px;color:#000;', 
                     'paragraph' => 'font-size:10px;color:#eee;'
                   ];
     ?>
     <!DOCTYPE html>
         <html>
         <head> [...]
         </head>
         <body style="{{ $style['body'] }}">
             @yield('content')
         </body>
    </html>

SETTING VARIABLE IN CONTROLLER TO BE CONSUMED BY THE BLADE VIEW

     <?php  
         // FileName: SampleController.php
          public function welcome(){
              $css = [
                       'body'       => 'font-size:12px;color:#000;', 
                       'paragraph'  => 'font-size:10px;color:#eee;'
                     ];

              return view('sample.view')
                     ->with(['style' => $css]);
              }
          } 
     ?>

     <!-- THEN INSIDE THE VIEW, YOU'D DO -->
     <!DOCTYPE html>
         <html>
         <head> [...]
         </head>
         <body style="{{ $style['body'] }}">
             @yield('content')
         </body>
    </html>

For a more verbose information on how to go about this, you may want to see THIS LINK.

Poiz
  • 7,611
  • 2
  • 15
  • 17
  • Thanks for your answer, but I'd like to share $style in all my views, i don't want to re-define it in every controllers. – Guillaume Nov 24 '16 at 12:50