Skip to content Skip to sidebar Skip to footer

Append/add Laravel Blade Template To Current View Using Javascript

So, I have this: item.blade.php(locaton: 'pages/components/item.blade.php')
// some php logic test
list.blade.php // some laravel blade logic

Solution 1:

As the answer pointed you can't do that, as blade is server side processed. There is a workaround however but it's expensive and prone to vulnerabilities: create custom routes which output the desired blade files when needed.

In routes.php:

Route::get('/blades/{path_to_blade}', function($path_to_blade){
    return view($path_to_blade);
})->where('path_to_blade', '^[a-z0-9\_\-\.]+$');

Then you can use an ajax call or whatever to call the requested template. I'm using jQuery as demo:

$.ajax({
    url: "/blades/template.path",
    cache: false,
    success: function(html){
        $("#element_in_which_to_insert").append(html);
    }
});

You can also request the template in browser: /blades/template.path. But again: this method is prone to vulnerabilities. You should use a frontend framework such as Vue or Angular instead.

Solution 2:

You should use

function onAddInvoiceItem()

not

function addItem()

Because you call it with onAddInvoiceItem() here :

<buttononclick="onAddInvoiceItem()">add item</button>

Post a Comment for "Append/add Laravel Blade Template To Current View Using Javascript"