在Laravel 5.3布局中,包含PHP变量到视图中

3

根据 Laravel 的通知电子邮件模板,这是我尝试做的事情。

我试图拥有一个包含变量的电子邮件布局,以便在我的布局中定义一些样式,并在我的电子邮件视图中使用。

这里是一个例子:

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>

在我的电子邮件视图中:

@extends('emails.layout')

@section('content')

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

@endsection

但是,由于我的部分中无法访问$style变量,所以这不起作用。

因此,我使其工作的唯一方法是在我的布局和视图中都包含一个PHP文件,而我非常确定这不是使用Laravel的正确方式...

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

有没有一种方法可以实现这个呢? 谢谢大家!

当你扩展时,可以像在这个答案中所见,通过传递变量来实现:https://dev59.com/DWQo5IYBdhLWcg3wXeX5 不确定它是否对此情况有所帮助,但也许你可以稍微改变一下使其工作。 - Antony Thompson
3个回答

3
理想情况下,您不应该尝试以这种方式包含 PHP 变量。在从控制器返回视图时,应将所有需要呈现视图的变量都传递给它:
class YourController
{
    public function yourMethod()
    {
        $styles = getYourStyles();

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

这将使你的样式在视图中通过$styles变量可用。

另外,你也可以使用配置文件存放你的变量,这样你可以随时访问它们。

config/styles.php中。

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

在你的模板中:
<p style="{{ config('styles.paragraph') }}">

或者只需使用View::share() - Roma Rush

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]);

    });

现在你的变量将在整个 blade 模板中可用,如果你想要包含其他模板,只需给出名称即可,而不是使用 *。例如:/view/user/view.blade.php

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

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

            });

1

有几种方法可以解决这个问题。其中一种是在控制器中设置您的命名变量,另外一个选项是直接在Blade视图中设置变量。

在Blade视图中设置变量 - [ 奇怪的想法 ]

     <!--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>

在控制器中设置变量以便于被Blade视图使用。
     <?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>

如果您想了解更详细的信息,请参见此链接


谢谢您的回答,但我想在所有视图中共享$style,而不想在每个控制器中重新定义它。 - Guillaume

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接