将刀片模板保存到数据库而不是文件 [英] Save blade templates to database rather than file

查看:85
本文介绍了将刀片模板保存到数据库而不是文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的blade模板保存到数据库中,因为用户的每个页面is customizableheader and footer.我想让我的用户自己创建布局,然后针对给定用户的每个请求,使用该用户指定的布局来服务页面.

I want to save my blade templates to database, because the header and footer of each page is customizable for the user. I want to let my users create the layout themselves and then for each request from a given user, I want to serve the page, using the layout specified by that user.

文档中提供了控制器传递的必要变量.

The necessary variables that are passed by the controller are provided to them in the documentation.

注意:我信任我的用户.他们都是项目的利益相关者,都是程序员,因此可以接受服务器端代码执行.

Note: I trust my users. They are all stake-holders of the project and are programmers, so server side code execution is acceptable.

推荐答案

尽管这是一篇旧文章,但以防万一有人像我一样偶然发现它.在使用Laravel Framework时,通过将视图保存在数据库中,我获得了类似的效果,以便每当需要显示视图时,都可以从DB中检索视图,并使用file_put_contents() php函数将其加载到文件中,并使用view()方法.例如;

Although this is an old post but just in case someone stumbles across it like I did. I achieved similar while using the Laravel Framework, by saving the view in database such that, whenever I need to display the view, I retrieve it from DB, and load it into a file using the file_put_contents() php function and render it with the view() method. For example;

$blade = DB::table('pages')->where('name', 'index')->first();
file_put_contents('template.blade.php', $blade->view);

//Note if I also need to pass data to the view I can also pass it like so
//$data = ['page_title' => 'Testing Blade Compilation using views Saved in DB'];
// return view(template, $data);

return view('template');

在我自己的情况下,为了增加安全性,我使用刀片模板方案&创建了基本模板.在使用 HTMLPurifier 清理生成的输入并呈现视图之后,将用户创建的输入注入模板.例如

While again in my own case for added security, I created base templates with the blade templating scheme & injected user created inputs into the template after sanitizing the generated input using HTMLPurifier and rendering the view. For example

$view = view('base.template')->render();
//similarly like the above I can load any data into the view like so
//$data = ['page_title' => 'Testing Blade Compilation using views Saved in DB'];
//$view = view('base.template', $data)->render();

$purifier = new HTMLPurifier(HTMLPurifier_Config::createDefault());
$with_purified_input = $purifier->purify($user_generated_input);
str_replace('view_variable', $with_purified_input, $view);

return $view;

这篇关于将刀片模板保存到数据库而不是文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆