laravel blade append to #section in #forelse - laravel

My main template blade has a #yield('section_name') in the <head> tag to append css/ js.
Now I have a forelse like this:
#forelse($collection as $item)
some text
#section('section_name')
// link to stylesheet
#endsection
#empty
// something went wrong
#endforelse
When this loops for like 5 times, only one stylesheet is added. After that, it seems like the #yield is not working anymore. Is there a way to make this work so I can keep appending items to that section?

Use #append instead of #endsection
#forelse($collection as $item)
some text
#section('section_name')
// link to stylesheet
#append
#empty
// something went wrong
#endforelse

Related

Can #yield() and #section() be used with #include() files?

I have 3 views:
post/index.blade.php
post/container.blade.php
layouts/app.blade.php
Index view #expands('layout.app') and also #include('container'). The Idea behind the container is to use the same structure to show posts in different views. But the thing is that container.blade.php is not going to be 100% the same on each view. So I thought maybe I can add an #yield('append') on the container.blade.php so that way I could add more on that post container, using #section(). Like this:
inside the index.blade.php
#extends('layouts.app')
#section('content')
#include('post.container')
#section('append')
<p>This is not being outputted correctly.</p>
#endsection
#endsection
This #section('append') belongs to, or actually matches the #yield('append') on the container.blade.php. The question is how can this be done? How to add #section('') that should belong to #include() files in this case container.blade.php?
You can pass data to the #Include
#php $data=[
'title' => "test title",
'description' => ""
] #endphp
#extends('layouts.app')
#section('content')
#include('post.container', $data)
#endsection

Collect multiple files into single PDF

I use in my project barryvdh laravel-dompdf package.
Users might generate a PDF with some personal information, till here everything is ok:
$pdf = PDF::loadView('personalInfo', $userData);
$pdfFileName = "personal-info-{$userData['username']}.pdf";
return $pdf->download($pdfFileName);
Admin should be able to download information of users, and in case of selecting several users, instead of generating several files I would like to collect all users PDFs into a single file.
How can I do it?
You can create like this below
For example you have personalInfo blade template which is now for particular user .So loop that view and store it in array like below
$pages=[];
foreach($userDataList as $key=>$userData){
$pages[]=(string)view('personalInfo', $userData);
}
$pdf = App::make('dompdf.wrapper');
$pdf->loadView('index', ['pages' => $pages]);
return $pdf->stream();
then you can create seperate blade template to show these views.Here i will name it as index.blade.php
<style type="text/css" media="print">
div.page
{
page-break-after: always;
page-break-inside: avoid;
}
</style>
#foreach($pages as $page)
<div class="page">
{!! html_entity_decode($page) !!}
</div>
#endforeach
Method:2
loop all user detail inside personalInfo blade template and at the end of each loop add below page break style
<p style="page-break-before: always;"></p>
Example inside personalinfo blade
#foreach($pages as $page)
personal information content
<p style="page-break-before: always;"></p>
#endforeach
Also i personally prefer laravel snappy for multiple page because it render faster than laravel dompdf.Only thing is to server configuration bit head ache if you are using windows
Ref:https://github.com/barryvdh/laravel-snappy

Change interpolation brackets - Angular2

I want to use Laravel Blade and AngularJS.
Is some way to change interpolate sintax, change {{text}} for [{text}] or somthing like that?
I already change all components.ts files adding the line:
interpolation: ["{[", "]}"],
but, where I write blade, app breaks...
Thanks a lot everybody ;)
You can define your Blade content tags in your routes.php file:
Blade::setContentTags('<%', '%>');
Blade::setEscapedContentTags('<%%', '%%>');
EDIT: You can add # in front of the brackets, so Blade won't render it.
#{ var }
or you can pass the directive for blade not to render part of the HTML using #verbatim keyword.
#verbatim
<div>
{{ var }}
</div>
#endverbatim

how to add a dynamic image src with laravels blade

My question is very simple. i use blade in laravel to show the images like
<img src="{{asset('assets/images/image.jpg')}}">
but what if my image is dynamic like:
<img src="{{asset('assets/images/$imgsrc')}}">
i cant use
<img src="{{asset('assets/images/{{ $imgsrc')}}">
because then i will end with:
<img src="http://localhost/assets/images/{{ $imgsrc">
How can i call to a $variable in a blade {{}} call?
How about
<img src="{{asset('assets/images/').'/'.$imgsrc}}">
EDIT
when you need printing more than one strings where there may be variables,functions... you need to concatenate them
Also asset('assets/images/') is equivalent to asset('assets/images') so you need to add / before printing your variable $imgsrc
Finally, in Blade {{"foo"}} is equivalent to <?php echo "foo" ?>
How can i call to a $variable in a blade {{}} call?
The normal PHP way. A blade {{}} is just the equilivant of <?php echo() ?>
So the answer to your question is:
<img src="{{ asset('assets/images/' . $imgsrc) }}">
Mine had multiple dynamic parts in the src and this is how I did it after getting in the right direction, thanks to #Laurence's answer.
<img src="{{asset('storage/'.$parentFolderName.'/'. $clientNameFolder .'/logo/').'/'.$event[0]->event_logo}}" class="event-page-country-logo">

blade template, #yield at extended view

I would like to define a section at the layout or at an include, such as:
/views/master.blade.php
#section('mysection')
content here
#stop
#yield('content')
And be able to yield it at the view inheriting the layout:
/views/home.blade.php
#extends('master')
#section('content')
#yield('mysection')
#stop
From what I've tested this doesn't work. Is there another way of doing this?
Some sections are not supposed to always render and also not in the same place as they are on the layout, so the yield method would do what I need.
You can try to modify the master.blade.php as:
/views/master.blade.php
#section('mysection')
content here
#show
#yield('content')

Resources