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
Related
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
So I created a custom layout for my design, it works on other blade but not for this one blade. I did some recheck just to make sure that it is the same as other blade that works.
Also the controller is prettymuch the same since it has the same function for my other blade.
My checkout.blade :
#extends('layouts.custom')
#section('content')
<h1> Content </h1>
#endsection
My custom layout
<!DOCTYPE html>
<html>
<head>
</head>
<body>
#yield('content')
</body>
<footer>
</footer>
</html>
My Controller:
public function checkout()
{
$keranjangItems = \Cart::session(auth()->id())->getContent();
return view('keranjang.checkout', compact('keranjangItems'));
}
And I've tried to put the content on other blade and got no problem.
And I've tried to change my other controller to view this blade and also working.
Can anyone help me find where the problem is? thanks
Update: I also tried to just put together the code no include layout or what so ever and it's still have the same result
On your custom.blade file it says
#yield('layouts.header')
and on your checkout.blade file it says
#section('content')
<h1> Content </h1>
#endsection
Either update the layout to match the section name on your checkout view or update the checkout view to match the yielded section name on the layout.
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
I want to show the result when search a keyword successful.
In routes\web.php:
Route::get('tim-kiem', 'Frontend\ListBaiVietController#timkiemBaiViet');
In controller ListBaiVietController, I have a function:
public function timkiemBaiViet() {
$tukhoa = \Request::get('tukhoa');
$ketquatimkiems = Post::where('title','like','%'.$tukhoa.'%')
->orderBy('title')
->paginate(20);
// var_dump($ketquatimkiems);
return view('post/searchresult',compact('ketquatimkiems'));
}
I am using var_dump($ketquatimkiems), it shows 2 results.
In post/index.php I am calling content:
<body>
#yield('content')
</body>
And post/searchresult.php:
#extends('post.index')
#section('content')
#foreach($ketquatimkiems as $ketqua)
<div class="container-artical">
<div class="list-excerpt">
{!! $ketqua->excerpt !!}
</div>
</div>
#endforeach
<nav class="blog-pag">
{{ $ketquatimkiems->links() }}
</nav>
</div>
#endsection
When I am typing text quận 8. It is only showing code, not result.
your file name must have .blade extension
post/searchresult.php: to post/searchresult.blade.php:
post/index.php to post/index.blade.php
for more information
Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. Blade view files use the .blade.php file
Ref: https://laravel.com/docs/5.5/blade
you are not using laravel blade engine. Rename view
files like so index.blade.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.