Laravel Blade使用自定义功能 [英] Laravel Blade Using custom function

查看:191
本文介绍了Laravel Blade使用自定义功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个刀片,可以在其中打印表格的内容. 对于某些列,我需要根据要打印的值添加CSS类.

I have a blade where I'm printing the content of a table. For some columns I need to add the CSS class according the value I'm printing.

例如如果为"OK",则添加绿色类,否则添加红色类. 当然,逻辑会更复杂,但要点是所有逻辑都将与样式相关.

E.g. if it's "OK", add class Green, otherwise class red. Of course the logic will be more complex, but the point is that all the logic will be related to the style.

保存这种功能/方法的最佳位置是哪个? 我需要创建模型吗?

Which one is the best recommended place to save this type of function/method? Do I need to create a Model?

**更新**

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

"statusWrapper" is the function that I'd like to call to decorate the Status value. So the status is a number, and the output will be something like <span class="..."> .... </span>

推荐答案

我向您推荐我经​​常做的自己.您无需制作模型,只需制作一个帮助文件并在其中编写所有自定义函数.例如,您可以在 app/Http/Helpers/路径中创建一个名为helper.php的文件.然后,您必须使您的项目知道此文件.为此,您只需将其添加到autoload-> files对象中的composer.json文件中,如下所示:

I recommend you what I always do myself. You don't need to make a model, you just have to make a helper file and write all your custom functions in it. For example you can make a file named helper.php in app/Http/Helpers/ path. Then you have to make your project aware of this file. To do that you just have to add it in your composer.json file in autoload -> files object like this:

"autoload": {
    "files":[

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

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

After this just run command composer dump-autoload. Now you can access to your custom functions which are in you helper.php file from anywhere.

这篇关于Laravel Blade使用自定义功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆