Parse string to view

Published 2 years ago by ilaurens

I am taking data from the database, and would like to place it inside a view and render it.

Does anyone know how to do this without using a package from someone else.

Best Answer (As Selected By ilaurens)
thomaskim

Hm...I'm still not understanding the benefit to this.

Don't content management systems typically keep the files as views and render them? The DB would just decide which file/view to render. Creating your own theme meant creating a sub-directory with files that follow that specific CMS's standards.

Personally, I feel like it would be a hassle to keep the actual views in the DB because that would slow me down in terms of coding. For example, traditionally, if I'm creating a theme for a CMS, I can just open up my text editor and get started right away. Share my theme? Just zip it up and upload. Or put it on Github. Both are very fast solutions. If it's in the DB, how exactly would I change the view? Do I have to get authenticated and then use the browser to edit it? How would I share it?

jlrdw
jlrdw
2 years ago (252,040 XP)

Laracast, docs, etc.

ilaurens

I ment literally taking an unparsed content from the database, and throwing it inside a view and parse it like it normally works.

I cannot find anything that works with a string, but I can find functions that works with files(this is something I do not want).

ilaurens

I was playing a bit, for fun ~ but I would like to avoid code like this:

    public function lol($content)
    {
        ob_start();
        
        eval('?>'.\Blade::compileString($content));

        $content = ob_get_clean();

        return $content;

    }

jlrdw
jlrdw
2 years ago (252,040 XP)

Retrieve data in a query and display in the view like you normally would.

Snapey
Snapey
2 years ago (1,038,075 XP)

Please read some of the docs. You don't have to use views and resource templates, and Blade but give them a go and come up with your own methods later if you are still not convinced.

start here http://laravel.com/docs/master/views

ilaurens

Sorry, I suck at explaining things.

I would like to use the blade system, but instead of using view('test.file.path'), I would like to inject a string that needs to compile directly.

The view: 'test.file.path' does not exists, but the content that is normally in that file is located in the database, and that is what I would like to use.

Snapey
Snapey
2 years ago (1,038,075 XP)

Sorry, I suck at explaining things.

Correct!

How much of the data is in the database? Is it fully rendered? Describe more of the context.

You could for instance have a full HTML Blog post in the database and still use a view. Your view could just have a single placeholder for the content.

post.blade.php

{!! $post->body !!}

It would be a strange way to build an app though.

edit: changed tags from double braces to exclamation marks to output the raw data

Jeffberry

He is storing blade syntax in the database and he wants it to render.

Most likely you will need to extend Laravel's View Factory and the View classes to accomplish this.

ilaurens

I have been playing around for a bit, what do you guys think about this approach?

        \Blade::extend(function($value) use ($page) {
            return preg_replace('/@cmsview/', $page->html , $value);
        });

It is much better than that eval goody, but still doubting, about storing it to the database vs local file storage, I can also make files on the go.

Since it is cached anyway, it does not matter whether I use the database or file, but database would have been the easiest choice.

pmall
pmall
2 years ago (547,945 XP)

Why do you want to do this ?

ilaurens

@pmall

To put it bluntly I am working an a CMS, or well, at least gathering a bit idea's before starting with the project. The blade syntax is stored in my database, and would like to use the received data the same way as calling a normal blade.

Normal way: view('test.abc.page')

What I would like view($bladeContent) --> obviously, it does not work that way.

So I would like to render the view from the database(string), instead of files.

pmall
pmall
2 years ago (547,945 XP)

I cant see what functionality you can implements with that. You want users to make custom blade templates ? What for ?

ilaurens

Well, seems like the above idea introduced me to some problems with caching, the same file is called over and over, so the caching kicks in. :P

return view('Cms::page.view') ->with('page', $page);

ilaurens

@pmall,

It's a CMS system, the template files are located in a database, and I would like to render it. Only authorized people is allowed there.

I think I will just use the database for the title, description etc, and a view file as resource. Guess this will be the easiest solution. Since it will introduce some probs when using it another way.

pmall
pmall
2 years ago (547,945 XP)

Why dont you want to just store it in views. Seems over complicated for me.

Please sign in or create an account to participate in this conversation.