Differencebetween {{ }} and {!! !!} in Laravel Blade Files

What is the difference between {{ }} and {!! !!} in laravel blade files?

Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks.

If you pass data from your Controller to a View with some HTML styling like:

$first = "<b>Narendra Sisodia</b>";

And it is accessed, within Blade, with {{ $first }} then the output'll be:

<b>Narendra Sisodia</b>

But if it is accessed with {!! $first !!} then the output'll be:

Narendra Sisodia

What is the difference between {{ }} and {{{ }}} in laravel blade files?

There is no difference between {{ }} and {{{ }}} in Laravel 5 at all.

In version 4 it included the following two styles: “{{” and “{{{“. The double
curly bracket was a raw echo and the triple curly bracket escaped.

Currently in 5.0 both the double and triple curly brackets escape the
variable and a new “{!! $var !!}” is for raw.

https://laravel-news.com/2014/09/laravel-5-0-blade-changes/

Laravel blade {{ ... }} vs ?= ?

from Laravel docs:

Blade {{ }} statements are automatically sent through PHP's htmlspecialchars function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:

Hello, {!! $name !!}.

Be very careful when echoing content that is supplied by users of your application. Always use the escaped, double curly brace syntax to prevent XSS attacks when displaying user supplied data.

Additionally if you are using blade template engine, it's like convention to use {{ }}

https://laravel.com/docs/5.7/blade#displaying-data

How different echo ({!! ... !!} and {{...}}) in laravel 5.3?

From Laravel 5.3 doc

Displaying Unescaped Data

By default, Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:

Hello, {!! $name !!}.

Laravel 5 What is the difference between simple text in a link and link with blade syntax?

Using __ utilizes the localization system in Laravel and can display the text in another language if you have it set up.

You can read more about it in the documentation:
https://laravel.com/docs/5.6/localization#retrieving-translation-strings

If you want your website in different languages you should use it, otherwise your better of with just typing it as plain text.

What is the difference between Section and Stack in Blade?

I might be mistaken, but the difference is not only semantically, but in behaviour as well.
With @push you append as many times as needed to a stack, while (by default) you may fill @section only once in your views.
In some situations this comes in handy when you need to add content from different locations across your template files or in loops:

index.blade.php:

@extends('master')

...

@for ($i = 0; $i < 3; $i++)

@push('test-push')
<script type="text/javascript">
// Push {{ $i }}
</script>
@endpush

@section('test-section')
<script type="text/javascript">
// Section {{ $i }}
</script>
@endsection

@endfor

master.blade.php

    @stack('test-push')
@yield('test-section')
</body>

result:

    <script type="text/javascript">
// Push 0
</script>
<script type="text/javascript">
// Push 1
</script>
<script type="text/javascript">
// Push 2
</script>
<script type="text/javascript">
// Section 0
</script>
</body>

Laravel: display difference between two dates in blade

You've got two separate problems here:

First: how do you diff two dates. You can go high-tech or low-tech here. If you don't want to use Carbon, I suggest going low-tech:

<?php
// Note, this gives you a timestamp, i.e. seconds since the Epoch.
$ticketTime = strtotime($ticket->start_date);

// This difference is in seconds.
$difference = $ticketTime - time();

At this point, you've got to decide how you want to output the difference. In seconds? In hours?

Difference: {{ $difference }} seconds

Difference: {{ round($difference / 3600) }} hours

Difference: {{ round($difference / 86400) }} days

You'll have to do extra engineering if you want something as pretty as Carbon::diffForHumans().

Second: This now becomes a question for you whether this is too much code for your front-end. Obviously you could reduce all of the above to a one-liner in PHP, but your co-workers may not appreciate how unreadable it becomes:

{{ round((strtotime($ticket->start_date) - time()) / 3600) }} hours

Caveats

Using timestamps ducks the issue of dealing with timezones. For a bunch of use cases, this is sufficient; for others this is woefully inadequate. But if you want to deal with timezones, you're definitely better off using Carbon, which is better than using PHP's DateTime, but up to you.



Related Topics



Leave a reply



Submit