Best Practices for Custom Helpers in Laravel 5

后端 未结 20 2279
暖寄归人
暖寄归人 2020-11-22 06:40

I would like to create helper functions to avoid repeating code between views in Laravel 5:

view.blade.php

Foo Formated text: {{ fo

20条回答
  •  轮回少年
    2020-11-22 07:32

    Custom Blade Directives in Laravel 5

    Yes, there is another way to do this!

    Step 1: Register a custom Blade directive:

    Custom Link';
             });
         }
        ...
    

    Step 2: Use your custom Blade directive:

    
    
    @shout('this is my custom blade directive!!')
    
    @customLink

    Outputs:

    THIS IS MY CUSTOM BLADE DIRECTIVE!!
    Custom Link


    Source: https://laravel.com/docs/5.1/blade#extending-blade

    Additional Reading: https://mattstauffer.co/blog/custom-conditionals-with-laravels-blade-directives


    If you want to learn how to best make custom classes that you can use anywhere, see Custom Classes in Laravel 5, the Easy Way

提交回复
热议问题