gpt4 book ai didi

php - 从数据库存储和渲染 Laravel Blade 模板

转载 作者:行者123 更新时间:2023-12-05 03:07:06 24 4
gpt4 key购买 nike

如何从数据库中渲染 Blade 模板(而不是使用 Blade 模板文件)?

我检查过这个Render template from blade template in database但由于某种原因没有替换模板中的变量。它给出了一个通知: undefined variable 。

有什么想法吗?

提前致谢

最佳答案

一种最简单的方法是将内容存储在临时 Blade 文件中并呈现它。

假设我们获取存储在数据库中的 Blade 字符串:

$content_from_db = "Hello {{ $user_name }} This is your message";

过程:

$filename = hash('sha1', $content_from_db);

// temp file location where this file will be uploaded
// it can easily be cleared with 'php artisan view:clear'
$file_location = storage_path('framework/views/');

// filepath with filename
$filepath = storage_path('framework/views/'.$filename.'.blade.php');

file_put_contents($filepath, $content_from_db);

// add framework location so that it won't look into resource/*
view()->addLocation($file_location);

$data = [ 'user_name' => 'John Doe' ];

$rendered_content = view($filename, $data)->render();

$rendered_content = trim(preg_replace('/\s\s+/', ' ', $rendered_content));

关于php - 从数据库存储和渲染 Laravel Blade 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48429440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com