Laravel Blade Using custom function

假装没事ソ 提交于 2020-05-16 07:04:17

问题


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.

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?

** UPDATE **

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


回答1:


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

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.




回答2:


If status should include HTML like showing different colors I recommend you use @include

// resources/views/statusWrapper
@if($status == 'good')
    <span style="color: green;">thats really good</span>
@else
    <span style="color: red;">not good</span>
@endif

and then in your table view

@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 

You could also look at extending blade: https://laravel.com/docs/5.5/blade#extending-blade

But I will not recommend you to put HTML in your PHP code as it's easier to keep your HTML in your view files for future edits.




回答3:


app/providers/AppServiceProvider.php (You can create a different Service Provider, if you wish)

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

}

resources/views/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>



回答4:


If you're planning to use the function in multiple blade templates, then Tohid's answer is the best.

If you just need the function in a single template, you can define it directly in the .blade.php file like this:

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

Then in you can call the function from within the same template:

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

The conditional function definition is needed if you include the blade template multiple times in the same session.



来源:https://stackoverflow.com/questions/46524620/laravel-blade-using-custom-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!