英文:
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:
<x-frontend.module-button href="#" text="Example button" />
But when using a HEREDOC like so:
$html = <<<HTML
<x-frontend.module-button href="#" text="This will not output anything" />
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('components.'.$componentName, $data);
return $render ? $view->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([
'html',
'text'
])
<div>
<button>{{ $text }}</button>
</div>
Then to render this component via a string:
<?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'));
});
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 !!}
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论