Extend Blade Template from Database stored string

2019-06-08 12:56发布

问题:

My problem is that I need to pass string as an argument to view, which contains the Blade Template tags to be rendered. I've done my research and I believe the other problems asked don't relate to mine. Hence following is what I want to achieve.

I want to retrieve the master layout from database and extend the child views from it. So in my case, the view that is to be passed to view() method needs to be extended from a string in database.

So something like the following should work in page.blade.php

@extends("<html>@yield('content')</html>") ---> the string shall be passed from database.
@section('content')
...
...
@endsection

Or some other code from the Controller itself may be okay, such as I call for the main layout first, then pass it the child view, which replaces the 'content' section.

回答1:

Well, considering the over-whelming response from the community even after many views, I decided to go for a work-around, until I find a better solution. (Sarcasm intended)

Documenting it here, so as to give a clue to those who might be trying the same.

  1. Extend the child view from a master view (say db_master.blade.php)
  2. In your Controller, put contents from the database column into the master view (db_master.blade.php) using either FileSystem class or file_put_contents (whatever you feel more secure with).
  3. Now return the child view as usual.

Note: Make sure you pass secure code to the master view (that is, take care of giving users access to blade templating engine, as they can inject actual php code)


Please help with feedback and suggestions.