Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a code in a blade view dynamically

Using laravel 5.1, could I insert a custom code into a blade view dynamically? For example, I have this view.blade.php :

<html>
<head>
    <title>App Name - @yield('title')</title>
</head>
<body>
    @section('sidebar')
        This is the master sidebar.
    @show

    <div class="container">
        @yield('content')
        // Here, I want to insert a code dynamically, for example :
        <div> <p> something </p> </div>
    </div>
</body>

So, Is there a good way to do something like this in controller :

$customCode = "<div> <p> something </p> </div>";
$content = (string) $view;
//For example I want to insert this code into view file on line 11
updateContent($content, $customCode, $line_position_in_view);    
like image 657
BKF Avatar asked Oct 31 '22 09:10

BKF


1 Answers

Here is your code with jQuery :

Your HTML FILE OR VIEW FILE

<div class="container">
        @yield('content')
        // Here, I want insert a code dynamically, for example :
        <div> <p> something </p> </div>
    </div>

$.post("test.php",{},function (data){
     $(".container").append(data);
});

Note : if you want to use controller function then change test.php with function name with proper path.

test.php file or controller function

<?php
echo "<div> <p> something </p> </div>";
exit;
?>
like image 129
Mr. Engineer Avatar answered Nov 09 '22 11:11

Mr. Engineer