【问题标题】:Laravel @yield showing content with HTML tagsLaravel @yield 显示带有 HTML 标签的内容
【发布时间】:2019-08-17 17:53:44
【问题描述】:

我是 Laravel 的新手,遇到了这个问题,当我使用 @yield('content') 查看一些 HTML 时,输出将是与将其解码为 HTML 的确切联系。

这是我的代码:

@yield('content',"<p>This is a great starting point for new custom pages</p>")

输出是:&lt;p&gt;This is a great starting point for new custom pages&lt;/p&gt;

这是一个截图:

有人可以帮我知道发生了什么吗?

【问题讨论】:

    标签: laravel laravel-blade yield


    【解决方案1】:

    在 Laravel 中,@yield 指令希望从生成 html 的任何文件中提取您记下的 @section。当您拉入@section 时,它在页面上被格式化为正确的html。

    所以,如果你有这样的部分:

    @section('paragraph')
        <p>here is some text</p>
    @stop
    

    并在@yield 声明中提取了这个:

    @yield('paragraph')
    

    这只会输出 html:

    这里有一些文字

    你做了什么:

    @yield('content',"<p>This is a great starting point for new custom pages</p>")
    

    是提供 默认 文本以在“内容”部分不可用时显示在屏幕上。 此文本已转义,因此您看到的是 html 标记

    【讨论】:

      【解决方案2】:

      @Mousa Alfhaily,您不必使用 html 标签传递返回值 如果你想分段返回值......然后你在呈现内容的地方这样做

      @yield('content',"<p>This is a great starting point for new custom pages</p>")
      

      应该是

      @yield('content')
      

      那么你的

      @section('content')
      $var = "This is a great starting point for new custom pages";
      <p>
       {{$var}}
      </p>
      @endsection
      

      【讨论】:

        【解决方案3】:

        @section 指令定义了一段内容,而@yield 指令用于显示给定部分的内容。

        所以,如果你有 @section 喜欢-@section('paragraph')...@endsection

        那么,@yield 必须像 - @yield('paragraph')

        例子

        @yielddirectives -

        @yield('content')
        

        @section 指令中-

        @section('content')
        ...
        your html content
        ...
        @endsection
        

        【讨论】:

          猜你喜欢
          • 2021-06-05
          • 1970-01-01
          • 1970-01-01
          • 2015-05-31
          • 2011-09-28
          • 2015-07-03
          • 1970-01-01
          • 2017-07-23
          • 1970-01-01
          相关资源
          最近更新 更多