Every time I do a format shortcut (Alt + Shift + F) for my .blade documents, codes that start with # do not indent the way I want them to be. Below is the sample code of an unformatted basic if/else + foreach on a .blade file.
// unformatted codeblock
<h1 class="poppins-medium text-xl text-darkergreen text-center">
#if (Auth::user()->role == 'auditor' || Auth::user()->role == 'accreditor')
#foreach ($documentGalleryTitle as $record)
#if ((Str::lower(Str::snake(Str::replaceLast(' Taskforce', '', $record->name)))) == $folder_name)
{{ Str::replaceLast(' Taskforce', '', $record->name) . ' Documents'}}
#endif
#endforeach
#elseif (Auth::user()->role == 'taskforce')
{{ $userRecord->nonOfficialsCategories->area_number . Auth::user()->subcategory . " Documents" }}
#endif
</h1>
As you can see on the previous codeblock, codes inside the parent #if/else should be indented. Same with the codes inside the #foreach, and the #if/endif codeblock.
I want the codeblock to look like the code below rather than the previous code above:
// how it should be
<h1 class="poppins-medium text-xl text-darkergreen text-center">
#if (Auth::user()->role == 'auditor' || Auth::user()->role == 'accreditor')
#foreach ($documentGalleryTitle as $record)
#if ((Str::lower(Str::snake(Str::replaceLast(' Taskforce', '', $record->name)))) == $folder_name)
{{ Str::replaceLast(' Taskforce', '', $record->name) . ' Documents'}}
#endif
#endforeach
#elseif (Auth::user()->role == 'taskforce')
{{ $userRecord->nonOfficialsCategories->area_number . Auth::user()->subcategory . " Documents" }}
#endif
</h1>
Is there a particular extension that I need to install + edit to create my desired format?
Related
I'm working with Laravel 5.8 and I want to set a session variable at cart.blade.php Blade, like this:
#php
$conflicted = '';
#endphp
#if($conflicting && ($prd[0]->prd_delivery == 'city_free_delivery' || $prd[0]->prd_delivery == 'country_free_delivery'))
#php Session::put($conflicted , '0') #endphp
<p style="color:red;">
This product has free delivery but it can not be set because of service area conflict of other products
</p>
#endif
#if(!$conflicting && ($prd[0]->prd_delivery == 'country_free_delivery'))
#php Session::put($conflicted , '1') #endphp
<p style="color:red;">
Country Free Delivery
</p>
#endif
#if(!$conflicting && ($prd[0]->prd_delivery == 'city_free_delivery'))
#php Session::put($conflicted , '2') #endphp
<p style="color:red;">
City Free Delivery
</p>
#endif
Now on checkout.blade.php, I need to check the session value of $conflicted variable.
So how can I check session value at Blade ?
Because with get and has method, I can check the session key name.
You can use session() helper:
#if (session('conflicted'))
{{ session('conflicted') }}
#endif
I've just started using the framework. In plain PHP after the opening foreach I would then set the variables then close the php tag but then from what I can work out you have to then do the Laravel #foreach tags and then open and close #php. Is there a way around this as it seems like a lot of extra work and code?
#foreach($steps as $row)
#php
$title = $row->title;
$text = $row->text;
$i = 1;
#endphp
<div class="steps-item grid-wrap">
<div class="number"
#if($text || $title)
<div class="text-wrap">
#if($title)
<h2>{{$title}}</h2>
#endif
{!! $text !!}
</div>
#php
$i++;
#endphp
#endif
</div>{{--END steps-item--}}
#endforeach
Since blade is no PHP, you have to return to PHP with that directive. But you can set/use the variables without doing that in your case:
#foreach($steps as $i => $row)
<div class="steps-item grid-wrap">
<div class="number"
#if($text || $title)
<div class="text-wrap">
#if($title)
<h2>{{ $row->title }}</h2>
#endif
{!! $row->text !!}
</div>
#php
$i++;
#endphp
#endif
</div>{{--END steps-item--}}
#endforeach
If you still want to set variables, there's a Laravel package called alexdover/blade-set. But as #brombeer pointed out, in most cases it's highly recommended to set all necessary variables in the controller before passing them to the view.
Use laravel provided loop variables:
$loop->iteration The current loop iteration (starts at 1).
It will increment in every loop iteration automatically.
e.g:
First iteration = $loop->iteration => 1 ;
Second iteration = $loop->iteration => 2 ;
so on until loop ends.
Check docs:
The Loop Variables
You can use a #for directive with sizeof($steps) like that:
#for($i=0; $i<= sizeof($steps)-1; $i++)
#endfor
#foreach ($steps as $row)
<div class="steps-item grid-wrap">
<div class="number">
<div class="text-wrap">
#if ($row->title != '')
<h2>{{$row->title}}</h2>
/* if you want to display another title when its blank you can
use if-else here otherwise not need to use if conditions for
title and text */
#endif
#if ($row->text != '')
{!! $row->text !!}
#endif
</div>
</div>
</div>
#endforeach
{{--
for an example you have $steps values like
$steps =
Array(
[0] -> 1,
[1] -> 'title',
[2] -> 'text'
);
if you want this array key value pair you have to use #foreach like
#foreach ($steps as $key=>$value)
#endforeach
you can use key value in #foreach loop only
--}}
I want to have pagination in single post page, datas comes from "contents" table. But i want to have pagination when catch spesific word (Section) as a title go to next page in same post.
Is this possible to make it with Laravel 5.4 .
Example :
Section 1 ( this is title)
text text text text text text
( next page > I want to add next page and it will go Section 2 page)
Section 2
text text text text text text
( next page >)
my Post.php
public function contents()
{
return $this->hasMany(Content::class);
}
blade view :
#if($post->contents)
#foreach ($post->contents as $content)
#if ($content->type == "header")
<h4>{{ $content->body }}</h4>
#endif
#if ($content->type == "text")
<p>{{ $content->body }}</p>
#endif
#endforeach
#endif
For pagination use paginate() inside your model.
public function contents()
{
return $this->hasMany(Content::class)->paginate(1);
}
View file
#if($post->contents)
#foreach ($post->contents() as $content)
#if ($content->type == "header")
<h4>{{ $content->body }}</h4>
#endif
#if ($content->type == "text")
<p>{{ $content->body }}</p>
#endif
#endforeach
{{ $post->contents()->links() }}
#endif
I felt this code look a bit messy, the logic is to show a link <a href or otherwise show text only.
How can I refactor this to look cleaner and maintainable?
<ol class="breadcrumb">
<li class="{{ $active == 'sign_in'? 'active':'' }}">
#if($active != 'sign_in')
#php($showLink = true)
#else
#php($showLink = false)
#endif
#if($showLink)
<a href="{{ url_secure('sign_in') }}">
#endif
Sign In
#if($showLink)
</a>
#endif
</li>
<li class="{{ $active == 'article'? 'active':'' }}">
#if($active != 'article' && $showLink)
#php($showLink= true)
#else
#php($showLink= false)
#endif
#if($showLink)
<a href="{{ url_secure('article')}}">
#endif
Articles
#if($showLink)</a>#endif
</li>
<li> </li> //repeat the code logic like above
</ol>
It would be good if there a way to reduce if conditions and use loop.
Why not just:
<li class="{{ $active == 'sign_in'? 'active':'' }}">
#if ($active != 'sign_in')
Sign In
#else
Sign In
#endif
</li>
Also it is unclear why you check $showLink that was set previoulsy.
Okay, as I've read your comments, I've modified template a bit with using foreach loop:
#php
// some initial variables
$allowed_to_click = true;
// this is a link to selected page
$selected_link = 'something';
#endphp
#foreach ($links as $link)
#php
// check if this link is ACTIVE
$active = $link == $selected_link;
// check if we can CLICK this link
$allowed_to_click = $active || $allowed_to_click;
#endphp
<li class="{{ $active ? 'active':'' }}">
#if ($allowed_to_click)
Some Caption
#else
Some Caption
#endif
</li>
#php
// if link is ACTIVE - following links are unclickable
if ($active) {
$allowed_to_click = false;
}
#endphp
#endforeach
Im currently using Laravel 5.3 and was wondering if there is a option for the customization of the Three Dots deperator. (skips page 9-10, which is to late)
Example
Currently the Three dots initiate if there are more than 11 pages... Which isnt quiet useful if your site is responsive. if there are to many pages so it breaks into 2 lines.
Example2
I cannot find anything regarding there being options for $resource->links(). But if there is please tell me! Much appreciated.
Edit: it has to do with the following function:
vendor/laravel/framework/src/Illuminate/Pagination/LengthAwarePaginator.php (page: 128, render()). The current function does not support a second variable. So i guess i have to rebuild it?
This is a solution for Laravel 5.5+. Here is what it does:
Shows the first and the last page.
Shows previous and next two pages from the current page.
Three dots only appear on the left after the current page is greater than 4.
Three dots only appear on the right after the current page is less than the 4 - (count of pages).
<!-- Pagination Elements -->
#foreach ($elements as $element)
<!-- Array Of Links -->
#foreach ($element as $page => $url)
<!-- Use three dots when current page is greater than 4. -->
#if ($paginator->currentPage() > 4 && $page === 2)
<li class="page-item disabled"><span class="page-link">...</span></li>
#endif
<!-- Show active page else show the first and last two pages from current page. -->
#if ($page == $paginator->currentPage())
<li class="page-item active"><span class="page-link">{{ $page }}</span></li>
#elseif ($page === $paginator->currentPage() + 1 || $page === $paginator->currentPage() + 2 || $page === $paginator->currentPage() - 1 || $page === $paginator->currentPage() - 2 || $page === $paginator->lastPage() || $page === 1)
<li class="page-item"><a class="page-link" href="{{ $url }}">{{ $page }}</a></li>
#endif
<!-- Use three dots when current page is away from end. -->
#if ($paginator->currentPage() < $paginator->lastPage() - 3 && $page === $paginator->lastPage() - 1)
<li class="page-item disabled"><span class="page-link">...</span></li>
#endif
#endforeach
#endforeach
Output:
Page 1 (first page)
Page 3
Page 10 (last page)
Option 1 :
You can customize default files but don't change vendor files directly. Publish them and then add modifications to that.
php artisan vendor:publish --tag=laravel-pagination
This command will automatically create the folder /resources/views/vendor/pagination and you have your files for modification.
You can get more information here : laravel pagination
Option 2:
Maybe you want to get rid of the files that are generated by default. Or, perhaps you want to assign another file to be responsible for your default pagination view.
All of this is possible, but you will need to inform the AppServiceProvider for this action by calling the new pagination views in the boot() method:
use Illuminate\Pagination\Paginator;
public function boot(){
Paginator::defaultView('your-pagination-view-file-name');
Paginator::defaultSimpleView('your-pagination-view-file-name');
}
Get information for defaultView and defaultSimpleView here :laravel pagination
I have created new file for pagination and added in AppServiceProvider.
#if ($paginator->hasPages())
<ul class="blog-pagenation">
{{-- Previous Page Link --}}
#if ($paginator->onFirstPage())
<li class="disabled"><a>«</a></li>
#else
<li>«</li>
#endif
#if($paginator->currentPage() > 3)
<li class="hidden-xs">1</li>
#endif
#if($paginator->currentPage() > 4)
<li><a>...</a></li>
#endif
#foreach(range(1, $paginator->lastPage()) as $i)
#if($i >= $paginator->currentPage() - 2 && $i <= $paginator->currentPage() + 2)
#if ($i == $paginator->currentPage())
<li class="active"><a class="active">{{ $i }}</a></li>
#else
<li>{{ $i }}</li>
#endif
#endif
#endforeach
#if($paginator->currentPage() < $paginator->lastPage() - 3)
<li><a>...</a></li>
#endif
#if($paginator->currentPage() < $paginator->lastPage() - 2)
<li class="hidden-xs">{{ $paginator->lastPage() }}</li>
#endif
{{-- Next Page Link --}}
#if ($paginator->hasMorePages())
<li>»</li>
#else
<li class="disabled"><a>»</a></li>
#endif
</ul>
#endif
By using this i am able to get 3 dots in starting and ending you have to customize classes based on your themes.
Adding to the previous response, once you generate the vendor view files with the artisan command php artisan vendor:publish you can create a new one in that folder and call it for example custom.blade.php and put the following code:
#if ($paginator->hasPages())
<ul class="custom-pagination">
{{-- Previous Page Link --}}
#if ($paginator->onFirstPage())
<li class="disabled pageNumber"><span>« Prev</span></li>
#else
<li><a class="pageNumber" href="{{ $paginator->previousPageUrl() }}" rel="prev">«</a></li>
#endif
{{-- Pagination Elements --}}
#foreach ($elements as $element)
{{-- Array Of Links --}}
#if (is_array($element))
#foreach ($element as $page => $url)
#if ($page === $paginator->currentPage())
<li class="active pageNumber"><span>{{ $page }}</span></li>
#elseif (($page === $paginator->currentPage() + 1 || $page === $paginator->currentPage() + 2)
|| $page === $paginator->lastPage())
<li><a class="pageNumber" href="{{ $url }}">{{ $page }}</a></li>
#elseif ($page === $paginator->lastPage()-1)
<li class="disabled"><span>...</span></li>
#endif
#endforeach
#endif
#endforeach
{{-- Next Page Link --}}
#if ($paginator->hasMorePages())
<li><a class="pageNumber" href="{{ $paginator->nextPageUrl() }}" rel="next">Next »</a></li>
#else
<li class="disabled pageNumber"><span>Next »</span></li>
#endif
</ul>#endif
The important part of the code for the three dots is in the {{-- Array Of Links --}} portion. I think this more or less does what you need but may require additional tweaking.
then you can use it in your view like this:
<div class="pagination">
#if ($users instanceof \Illuminate\Pagination\LengthAwarePaginator)
{{ $users->links('vendor.pagination.custom') }}
#endif
</div>