Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 使用自定义功能的Laravel刀片_Html_Css_Laravel_Laravel 5_Blade - Fatal编程技术网

Html 使用自定义功能的Laravel刀片

Html 使用自定义功能的Laravel刀片,html,css,laravel,laravel-5,blade,Html,Css,Laravel,Laravel 5,Blade,我有一个刀片,可以打印表格的内容。 对于某些列,我需要根据正在打印的值添加CSS类 例如,如果“OK”,则添加绿色类,否则添加红色类。 当然,逻辑会更复杂,但关键是所有的逻辑都与风格有关 建议保存此类函数/方法的最佳位置是哪一个? 我需要创建一个模型吗 **更新** <thead> <tr>

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

例如,如果“OK”,则添加绿色类,否则添加红色类。 当然,逻辑会更复杂,但关键是所有的逻辑都与风格有关

建议保存此类函数/方法的最佳位置是哪一个? 我需要创建一个模型吗

**更新**

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

身份证件
名称
最后检查
地位
@foreach(用户为$u)
{{$u->id}
{{$u->name}
{{$u->last_login}
{!!statusWrapper($u->status)!!}
@endforeach
“statusWrapper”是我想要调用的函数,用于修饰状态值。
因此状态是一个数字,输出类似于

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

// resources/views/statusWrapper
@if($status == 'good')
    <span style="color: green;">thats really good</span>
@else
    <span style="color: red;">not good</span>
@endif
//资源/视图/状态包装器
@如果($status=='good')
那真的很好
@否则
不好的
@恩迪夫
然后在您的表视图中

@foreach ($users as $u)
<tr>               
   <td> {{ $u->id }}</td>
   <td> {{ $u->name }}</td>
   <td> {{ $u->last_login }}</td>
   <td>
       @include('statusWrapper', ['status' => $u->status])
   </td>
</tr>              
@endforeach 
@foreach($u用户)
{{$u->id}
{{$u->name}
{{$u->last_login}
@包括('statusWrapper',['status'=>$u->status])
@endforeach
您还可以查看扩展刀片:


但我不建议您将HTML放入PHP代码中,因为将HTML保存在视图文件中以备将来编辑更为方便。

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

"autoload": {
    "files":[

        "app/Http/Helpers/helper.php"
    ]
}
@php
if ( !function_exists( 'mytemplatefunction' ) ) {
   function mytemplatefunction( $param ) {
      return $param . " World";
   }
 }
 @endphp

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

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 ); ?>"; 
        });
    }
}
使用light\Support\Facades\Blade;
...
类AppServiceProvider扩展了ServiceProvider
{
...
/**
*引导任何应用程序服务。
*
*@返回无效
*/
公共函数boot()
{
刀片::指令('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>';
    }

}
<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>
namespace-App\ViewComponents;
类StatusWrapperComponent{
公共静态函数statusWrapper($status){
如果($status=='good')
回音“那真的很好”;
其他的
回声“不好”;
}
}
resources/views/yourview.blade.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>';
    }

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

身份证件
名称
最后检查
地位
@foreach(用户为$u)
{{$u->id}
{{$u->name}
{{$u->last_login}
@statusWrapper($u->status)
@endforeach

如果您计划在多个刀片模板中使用该功能,那么这是最好的

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

"autoload": {
    "files":[

        "app/Http/Helpers/helper.php"
    ]
}
@php
if ( !function_exists( 'mytemplatefunction' ) ) {
   function mytemplatefunction( $param ) {
      return $param . " World";
   }
 }
 @endphp
然后在中,可以从同一模板中调用函数:

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


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

如果是简单的逻辑,请将其放在视图中?为什么需要模型?这很简单。分享你的一些你已经实现的代码。@Proeviz,如果逻辑稍微复杂一点,或者我可以与多个视图共享它怎么办?@Mahfuzhishishir,我用一个代码示例更新了这个问题