如何在<<这样的刀片组件?

huangapple go评论61阅读模式
英文:

How to include blade component like <x-some-component> in a <<<HTML HEREDOC?

问题

在我的 Laravel 9 项目中,我使用 PHP 8.2 运行。

在 Blade 模板中,我可以使用以下示例 Blade 组件:

<x-frontend.module-button href="#" text="示例按钮" />

但是当我使用 HEREDOC 格式时:

$html = <<<HTML
    <x-frontend.module-button href="#" text="这将不会输出任何内容" />
HTML;

x 组件就不会输出任何内容。为了确认,我确认在标准的 Blade 模板中是可以正常工作的。

这是使用 HTML HEREDOC 的限制吗,还是有其他方法可以解决这个问题?我尝试过的唯一方法是添加一个特殊的全局函数叫做 component,该函数返回组件视图,代码如下:

function component(string $componentName, array $data = [], bool $render = true)
{
    $view = view('components.'.$componentName, $data);
    return $render ? $view->render() : $view;
}

然后在 HEREDOC 中使用 echo 输出,但是这样做有点违背了使用 HEREDOC 的初衷。

如果您有任何想法或建议,请随时提出,谢谢!

英文:

I'm using Laravel 9 and running PHP 8.2.

In blade views I can use the following example blade component:

&lt;x-frontend.module-button href=&quot;#&quot; text=&quot;Example button&quot; /&gt;

But when using a HEREDOC like so:

$html = &lt;&lt;&lt;HTML
    &lt;x-frontend.module-button href=&quot;#&quot; text=&quot;This will not output anything&quot; /&gt;
HTML;

The x-component doesn't output anything at all, and just to confirm that yes it does work in a standard blade view.

Is this just a limitation of using a HTML HEREDOC or is there some way around this? The only thing I tried was adding a special global function called component that returns the component view like so:

function component(string $componentName, array $data = [], bool $render = true)
{
    $view = view(&#39;components.&#39;.$componentName, $data);
    return $render ? $view-&gt;render() : $view;
}

And echoing that out within the HEREDOC, but then that kinda defeats the purpose of using the HEREDOC in the first place.

Any idea's or suggestions welcome, thank you!

答案1

得分: 1

从Laravel 9开始,您现在可以使用 Illuminate\Support\Facades\Blade::render($someString) 来内联渲染Blade模板

我没有您自定义的Blade组件的示例,所以我模拟了一个:

@props([
    'html',
    'text'
])

<div>
    <button>{{ $text }}</button>
</div>

然后,要通过字符串渲染此组件:

<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Blade;

Route::get('/', function () {
    $html = Blade::render(<<<HTML
        <x-frontend.module-button href="#" text="This will not output anything" />
    HTML);

    return view('welcome', compact('html'));
});

注意: 要使此正确渲染,我不得不禁用Blade的自动转义功能,所以我的 welcome.blade.php 只包含 {!! $html !!}

英文:

From Laravel 9 you can now render a Blade template inline using Illuminate\Support\Facades\Blade::render($someString).

I don't have an example of your custom Blade component so I mocked one up:

@props([
    &#39;html&#39;,
    &#39;text&#39;
])

&lt;div&gt;
    &lt;button&gt;{{ $text }}&lt;/button&gt;
&lt;/div&gt;

Then to render this component via a string:

&lt;?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Blade;

Route::get(&#39;/&#39;, function () {
    $html = Blade::render(&lt;&lt;&lt;HTML
        &lt;x-frontend.module-button href=&quot;#&quot; text=&quot;This will not output anything&quot; /&gt;
    HTML);

    return view(&#39;welcome&#39;, compact(&#39;html&#39;));
});

Note: To get this to render correctly I had to disable the auto-escaping feature of Blade, so my welcome.blade.php simply contained {!! $html !!}.

huangapple
  • 本文由 发表于 2023年2月6日 21:40:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75362064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定