Save blade templates to database rather than file

后端 未结 3 655
说谎
说谎 2020-12-20 01:44

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

3条回答
  •  星月不相逢
    2020-12-20 02:31

    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');
    

    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;
    

提交回复
热议问题