How to insert a code in a blade view dynamically - php

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);

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;
?>

For instance let's say you don't want to insert something like
{{$user->name}}
but more styled html chunk of code instead with the same string showed,
say by bootstrap 4. then you have to assign to the name a string like
$user->update(['name' => '<span class="badge badge-success">SHILPILDOQ</span>']);
AND FINALLY for the blade file you have a little change now :
{!! $user->name !!}

You are not suppose to just put html and javascript in your controller so you can dump it out into your blade view. This goes against the seperation of concerns.
You should just create a master layout and extend this master layout on other pages you create. This way you can dynamically adjust the content of the master layout page.
Naturally, you should never ever mix php functions into your blade view. Run the php functionality in your controller, put it into a variable and pass it along to your view to use.

Related

Laravel 8 – Blade template – read variable from another template file

This must be really simple, but since I’m new to Laravel, I’m having difficulties on finding this feature on documentation or in stackoverflow.
I’m going to try to build a very simple example to try to show what I want.
Let’s say I have a file called data.blade.php
#php
$myVariable = 'xyz';
#endphp
Then, I have my layout template file, called: home.blade.php
#extends('data.blade')
#section('content')
#php
echo $myVariable;
#endphp
#endsection
When I try to do this, it returns me an error:
Undefined variable $myVariable (etc)
How can I use a variable defined in another blade file? I used extends to exemplify, but I´m not sure if that is the right architecture.
Can anyone point me to the right way of accomplishing this?
Thanks!
you are extending data.blade.php and this file need to have some element to serve as template and be extended.
You can take any html5 file and rename this as template.blade.php, but where you want to render a section in your template must have a yield('section-name').
Make a new app.blade.php should look like this
<html>
<head>
<title>App Name - #yield('title')</title>
</head>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
</html>
Then, in your home.blade.php
#extends('layouts.app')
#section('title', 'Page Title')
#section('sidebar')
#parent
<p>This is appended to the master sidebar.</p>
#endsection
#section('content')
#include(data)
{{$myVariable}}
<p>This is my body content.</p>
#endsection
From Laravel Doc

Laravel Blade code always can't running well

I have a little problem with Laravel Blade in Laravel version 5.2. Can someone tell me how to include a Blade template within a Blade template? I already try to include welcome.blade.php
by #include('welcome') but, I always get an error message.
first you need to define a master page or main template what ever you say.
we create main.blade.php as template page .
<html>
<div class="row">
#yield("content")
</div>
the above code is our simple template page.
inside your welcome.blade.php write as below
#extends("main")
#section("content")
your code
#stop
if welcome.blade.php is your template or master page do like below.
welcome.blade.php :
<html>
<div class="row">
#yield("content")
</div>
inside your other page you want use welcome page as template do as below :
#extends("welcome")
#section("content")
your code
#stop

Laravel blade inline section

I need to duplicate a piece of html in my blade template but it's so small that I don't want to create a new file. Can I create a section in the same file that I use it? for example:
#section('small')
<div>small piece of html</div>
#endsection
#include('small')
<div>some html here</div>
#include('small')
I think #stack and #push is what you need instead of #section
Let's say you're going to use that on other file
Page 1:
#stack('small')
Page 2 :
#push('small')
<div>small piece of html</div>
#endpush
#push('small')
<div>some html here</div>
#endpush
You need to use Component with Laravel and make your template in a example.blade.php and set your variables in the component, and send the variables to the component while calling it like:
#component('alert', ['foo' => 'bar'])
...
#endcomponent
So it's in a different file but while sending the variables to the component, it can be efficient.
Do you mean duplicate #section? if yes, so create #yield with another name, and call both sections in the same template.
Page 1:
#yield('section1')
#yield('section2')
Page 2 :
#section('section1')
<div>small piece of html</div>
#endsection
#section('section2')
<div>some html here</div>
#endsection

Is there a way I can separately include section in Laravel blade?

When extending a view in laravel blade, to have multiple places of inserting, it can be done as something like this:
<html>
<head>
<title>This is app/view/layout/default.blade.php</title>
{{ HTML::script('js/jquery-2.1.3.min.js') }}
{{ HTML::script('js/angular.js') }}
#yield('extra_libraries')
</head>
<body>
<div class="left-menu-bar">
#yield('left-menu-bar')
</div>
<div class="main-content">
#yield('main-content')
</div>
</body>
</html>
Then, when using the layout,
#extend ("layout.default")
#section('extra_libraries')
{{ HTML::script('js/underscore.js') }}
#stop
#section('left-menu-bar')
testing button
#stop
#section('main-content')
testing
#stop
What I want is a similar function when I am not extending but to include another blade into my current blade. i.e. something like this:
<html>
<head>
<title>This is app/view/layout/default.blade.php</title>
{{ HTML::script('js/jquery-2.1.3.min.js') }}
{{ HTML::script('js/angular.js') }}
</head>
<body ng-app="myTestingApp">
<div ng-controller="testing">
#include('components.componentA.htmlComponent')
#include('components.componentB.htmlComponent')
</div>
<script>
var app = angular.module('myTestingApp', []).controller("testing", testingController);
function testingController($scope) {
#include('components.componentA.angularCode')
#include('components.componentB.angularCode')
}
</script>
</body>
</html>
where componentA should be a blade file containing both anguarCode and htmlComponent. I know I can separate them into 2 files, but I want to see if there is a way to put them in the same file as they are closely related. Is there default function in laravel blade to achieve this?
P.S. The example shows why I want to put them together, if you know angularjs.
to include another blade into my current blade
Well, to include another blade file inside an existing one, it is definitely the #include tag that comes to save the day. If it still doesn't satisfies your requirements, you can always write your own methods by extending Blade.
However, the whole thing seems a little too complicated to me. Why don't you separate your JavaScript into different controllers and then use ng-view to call the corresponding HTML, as shown in that example at the bottom of the documentation.
Not sure but you could try
...
<body ng-app="myTestingApp">
<div ng-controller="testing">
#yield('componentA-html')
#yield('componentB-html')
</div>
<script>
var app = angular.module('myTestingApp', []).controller("testing", testingController);
function testingController($scope) {
#yield('componentA-angularCode')
#yield('componentB-angularCode')
}
</script>
</body>
#include('components.componentA')
#include('components.componentB')
</html>
And your componentA blade should be
#section('componentA-html')
html for A
#endsection
#section('componentA-angularCode')
angularCode for A
#endsection
the same way for compomentB blade

Loading a part of view using a PHP variable

I am loading a part of view in by main view using a PHP variable
here is the code of the view
<body>
<div id="wrapper">
<div id="gallery">
<?= $view_thing ?>
</div>
</div>
</body>
and my controller is
$data['view_thing']=$this->load->view('model/portion',$data); //$data gets data from model
$this->layout->view('model/model',$data);
but the problem is "$view_thing" do not show things in the div in which I wrote it, this page is shown below body tag
note:I have wrote only a part of code from my view
The view loader function needs a third argument to return it as a string, as said in the documentation, for example:
$data['view_thing']=$this->load->view('model/portion', $data, true);

Categories