smike003's avatar

Extend Blade Template from Database stored string

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.

Stumbled upon this guy's post (https://laracasts.com/discuss/channels/laravel/parse-string-to-view). Apparently, he gave up to using files. In contrast, my problem is that I give freedom to users to create their own master layout files. So every user account shall have a layout file associated, hence storing such template files in filesystem won't be a good idea. Furthermore, the user object has a template field, which is more easily available because the authenticated user is available in the current session.

So the questions like why store the master-layouts in database are discouraged, except if you have a better (efficient & easy) remedy. Please consider that the website can have many users. And yeah! I am aware of the security risks, so I'll be filtering the contents submitted against any possible php or blade code. The only line I allow them to use from blade engine is the @yield() statement, all else shall be filtered out. Now what to filter and how to do that, I'll open a new discussion for that.

Finally, all suggestions are welcome, but please avoid using why's and rather comment with your opinion. Thanks in advance everyone.

0 likes
2 replies

Please or to participate in this conversation.