Laravel Blade使用自定义功能

时间:2017-10-02 11:29:31

标签: html css laravel laravel-5 blade

我有一个刀片,我正在打印一张桌子的内容。 对于某些列,我需要根据我正在打印的值添加CSS类。

E.g。如果它是“OK”,则添加类Green,否则为class red。 当然逻辑会更复杂,但重点是所有逻辑都与风格有关。

哪一个是保存此类功能/方法的最佳推荐位置? 我需要创建模型吗?

**更新**

            <thead>                                     
                <tr>                   
                    <th> ID </th>
                    <th> Name </th>
                    <th> Last Checked </th>
                    <th> Status </th>                    
                </tr>                                                   
            </thead>                                                    
            <tbody>     
                @foreach ($users as $u)
                    <tr>               
                        <td> {{ $u->id }}</td>
                        <td> {{ $u->name }}</td>
                        <td> {{ $u->last_login }}</td>
                        <td> {!! statusWrapper( $u->status ) !!}</td>
                    </tr>              
                @endforeach                                                      
            </tbody>                                                                         
        </table>          

“statusWrapper”是我要调用以装饰Status值的函数。 所以状态是一个数字,输出类似于<span class="..."> .... </span>

4 个答案:

答案 0 :(得分:2)

我向你推荐我自己做的事情。您不需要创建模型,只需创建一个帮助文件并在其中编写所有自定义函数。例如,您可以在 app / Http / Helpers / 路径中创建名为helper.php的文件。然后你必须让你的项目知道这个文件。要做到这一点,你只需要在autoload中的composer.json文件中添加它 - &gt;文件对象如下:

"autoload": {
    "files":[

        "app/Http/Helpers/helper.php"
    ]
}

在此运行命令 composer dump-autoload 之后。现在,您可以从任何地方访问您的helper.php文件中的自定义函数。

答案 1 :(得分:1)

如果状态应包含显示不同颜色的HTML,我建议您使用@include

async

然后在你的表格视图中

await

您还可以查看扩展刀片:https://laravel.com/docs/5.5/blade#extending-blade

但我不建议您将HTML放在PHP代码中,因为将HTML放在视图文件中以便日后编辑会更容易。

答案 2 :(得分:0)

app / providers / AppServiceProvider.php(如果需要,您可以创建其他服务提供商)

use Illuminate\Support\Facades\Blade;

...

class AppServiceProvider extends ServiceProvider
{

    ...


    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Blade::directive('statusWrapper', function ($status) {
            return "<?php echo App\ViewComponents\StatusWrapperComponent::statusWrapper( $status ); ?>"; 
        });
    }
}

app / ViewComponents / StatusWrapperComponent.php

namespace App\ViewComponents;

class StatusWrapperComponent {

    public static function statusWrapper ($status) {
        if($status == 'good')
            echo '<span style="color: green;">thats really good</span>';
        else
            echo '<span style="color: red;">not good</span>';
    }

}

资源/视图/yourview.blade.php

<thead>                                     
                <tr>                   
                    <th> ID </th>
                    <th> Name </th>
                    <th> Last Checked </th>
                    <th> Status </th>                    
                </tr>                                                   
            </thead>                                                    
            <tbody>     
                @foreach ($users as $u)
                    <tr>               
                        <td> {{ $u->id }}</td>
                        <td> {{ $u->name }}</td>
                        <td> {{ $u->last_login }}</td>
                        <td> @statusWrapper($u->status) </td>
                    </tr>              
                @endforeach                                                      
            </tbody>                                                                         
        </table>

答案 3 :(得分:0)

如果您打算在多个刀片服务器模板中使用该功能,则Tohid's answer是最佳选择。

如果只需要一个模板中的函数,则可以直接在.blade.php文件中定义它,如下所示:

@php
if ( !function_exists( 'mytemplatefunction' ) {
   function mytemplatefunction( $param ) {
      return $param . " World";
   }
 }
 @endphp

然后,您可以在同一模板中调用该函数:

<p>{{ mytemplatefunction("Hello") }}</p>

如果您在同一会话中多次包含刀片模板,则需要条件函数定义。

相关问题