Laravel Blade-HTML块的自定义方法? [英] Laravel Blade - custom method for html block?

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

问题描述

是否可以创建自己的刀片方法/标签,或者对此有什么解决方案?

Is it possible to create own blade method/tag or what is the solution to this?

例如,在刀片文件中,它将包含多个form-block块,如下所示:

For example in a blade file, it will contain multiple form-block block like this:

<section class="container-fluid container-fixed-lg form-block">
    <div class="row b-b b-grey">
        <div class="col-xs-12 col-md-3 m-r-35">
            <h2>Title</h2>
            <p>Some content here</p>
        </div>
        <div class="col-xs-12 col-md-7">
            <div class="panel panel-default">
                <div class="panel-body">
                    // Custom Block Here
                </div>
            </div>
        </div>
    </div>
</section>

是否有一种方法可以提高可读性/可维护性,以使刀片文件中具有以下内容:

Is there a way to improve the readability/maintainability to have something like this in a blade file:

@form-block('Title','Some content here')
   // Custom Block Here
@end-form-block

推荐答案

自定义刀片指令被称为编写起来有点脏,因为您必须返回包含HTML以及可能包含以下内容的PHP代码的字符串:将运行.更糟糕的是,您没有分别获取参数,而只是获得一个像"('Title', 'Some content')"这样的字符串作为指令的输入.我刚刚发现的一个技巧是,您可以使用数组语法来分隔参数.

Custom blade directives, as they are called, are a bit dirty to write, because you have to return a string that contains the HTML, plus maybe PHP code that will be run. What makes is even more dirty is that you do not get the parameters separately, you just get one string like "('Title', 'Some content')" as input for your directives. A trick I just found out is that you could use array syntax to separate the parameters.

Blade::directive('formblock', function ($expression) {
    return '
  <section class="container-fluid container-fixed-lg form-block">
      <div class="row b-b b-grey">
          <div class="col-xs-12 col-md-3 m-r-35">
              <h2><?=array' . $expression . '[0];?></h2>
              <p><?=array' . $expression . '[1];?></p>
          </div>
          <div class="col-xs-12 col-md-7">
              <div class="panel panel-default">
                  <div class="panel-body">
';
});

Blade::directive('endformblock', function () {
    return '
                  </div>
              </div>
          </div>
      </div>
  </section>
';
});

正如我所说,它很脏,但这可以工作.请注意,指令名称中不能使用破折号,这就是为什么我将其命名为formblockendformblock而不是form-blockend-form-block的原因.

As I said, it's dirty, but this will work. Note that you cannot use dashes in your directive names, that is why I called them formblock and endformblock instead of form-block and end-form-block.

一种更简单的方法是只使用@include指令来包含部分内容.但是,您必须在刀片文件中编写的代码更加冗长:

An easier approach is to just use the @include directive to include partials. However, the code you have to write in your blade files is a bit more verbose:

@include('partials.form_block_start', ['title' => 'My Title', 'content' => 'The extra content..']);
Main content...
@include('partials.form_block_end');

然后在views/partials/form_block_start.blade.phpviews/partials/form_block_end.blade.php中创建两个文件.在第一个中,您可以仅将标题和内容用作变量$title$content.参见 https://laravel.com/docs/5.2/blade#control-structures了解更多信息.

Then create the two files in views/partials/form_block_start.blade.php and views/partials/form_block_end.blade.php. In the first one, you can use just the title and content as the variables $title and $content. See https://laravel.com/docs/5.2/blade#control-structures for more information.

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

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