如何从字符串而不是刀片文件创建渲染视图?

发布于 2025-02-09 02:36:42 字数 163 浏览 2 评论 0 原文

我有一些 {{{soimething}}} 占位符的HTML。

我想从此自定义HTML获得渲染视图。 我想避免手动更换字符串。 是否可以?

注意:我看到了建议的问题,但我最终找到了一种更简洁的方法来实现我的目标。因此,我向这个问题发布了答案。请保持打开状态。

I've some html with {{ soimething }} placeholders in a table.

I would like to get the rendered view from this custom html.
I would like to avoid to manually do string replace.
Is it possible?

Note : I seen suggested questions but I ended finding a more concise way to reach my goal. So I post an answer to this question. Please keep this open.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇纵 2025-02-16 02:36:42

您可以使用刀片外墙。

使用Illuminate \ support \ aftades \ blade;

use Illuminate\Support\Facades\Blade;

    public function __invoke()
    {
        $name='Peter Pan';
        return Blade::render("
        <h1> Hello {$name} </h1>
        ",['name'=>$name]);
    }

You can use Blade Facade.

use Illuminate\Support\Facades\Blade;

use Illuminate\Support\Facades\Blade;

    public function __invoke()
    {
        $name='Peter Pan';
        return Blade::render("
        <h1> Hello {$name} </h1>
        ",['name'=>$name]);
    }
朕就是辣么酷 2025-02-16 02:36:42

发现

我们可以使用 \ Illuminate \ view \ Compilers \ bladecompiler :: Render($ string,$ data)

其中

  • $ string 是解析的文本
    hi {{$ username}}
  • $ data 是我们通常可以传递到 view() helper的同一助理数组,例如 ['用户名'=&gt; $ this-&gt; email]

我从官方文档中错过了这一点: https://laravel.com/docs/9.x/blade#rendering-inline-blade-templates

,我们也可以使用

使用Illuminate \ support \ support \ facades \ aketades \ blade ;

Blade :: Render($ String,$ data)

Found

We can use \Illuminate\View\Compilers\BladeCompiler::render($string, $data)

Where

  • $string is the text to parse, for example
    Hi {{$username}}
  • $data is the same associate array we could normally pass down to view() helper, for example [ 'username' => $this->email ]

I was missing this from the official doc: https://laravel.com/docs/9.x/blade#rendering-inline-blade-templates

So we can also use

use Illuminate\Support\Facades\Blade;

Blade::render($string, $data)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文