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

In the laravel framework we can use blade to add PHP code in html file.
We are using both {{ }} and {{{ }}} syntax in blade files of Laravel.
What is the difference between them?
i have a code in a blade file like this
{{{ $errors->has('slider') ? 'has-error' : '' }}}

Before Laravel 5:
{{{ $var }}} escapes the contents of $var
{{ $var }} echoes $var unescaped
Since Laravel 5:
{{ $var }} AND {{{ $var }}} escape the contents of $var
{!! $var !!} echoes $var unescaped

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/

{{{ renders HTML data but {{ shows data and elements without rendering.
ex :
$str = "<h3>Hello world!</h3>";
{{ $str }} output : <h3>Hello world!</h3>
{{{ $str }}} output : Hello world!

In Laravel 4 {{{ $var }}} is used to render data by escaping HTML entities that uses PHP htmlentities function to prevent XSS attacks.
{{ $var }} is used to render data without escaping HTML entities.
In Laravel 5 it slightly different you can use either {{ $var }} or {{{ $var }}} to escape data and to render unescaped data you can use {!! $var !!}.

Something I don't see mentioned here but which can be handy to know. If you have a translation text with a parameter that should become a Vue variable in the resulting html then the triple curly braces come in handy.
E.g.:
{{{ __('This window will close in :timeout seconds', ['timeout' => '#{{timeoutSecs}}']) }}}
Attempting this with any other combination of curly braces will throw some error.

Related

How to write a multiline Ansible Jinja2 variable?

I have a Ansible line that fails linting:
tags: "{{ deployment_id | resource_tags('asg', base_resource_tags, deployment=deployment_id, deployment_env=deployment_env, deployment_name=deployment_name, purpose=deployment_purpose_tag, cpu_utilization=deployment_cpu_utilization_tag, disk_io_class=deployment_disk_io_tag, prom_exporters=deployment_prom_exporter_tags) | asg_tag_list }}"
How do I make this pass linting?
You need to use YAML Folding Scalars, > without quotes. Then add 'block chomping' 'strip'docs indicator to remove the trailing newline. The below example will work correctly, with each newline translating to a space. Adding quotes will break it e.g.
tags: >-
{{ deployment_id | resource_tags('asg', base_resource_tags, deployment=deployment_id,
deployment_env=deployment_env, deployment_name=deployment_name, Purpose=deployment_purpose_tag,
cpu_utilization=deployment_cpu_utilization_tag, disk_io_class=deployment_disk_io_tag,
prom_exporters=deployment_prom_exporter_tags) | asg_tag_list }}

How can I escape single quotes for Ansible/Jinja2 ternary operator?

I have the snippet below. Basically, for an included task I would like to provide a variable whose contents look like the below string:
--date='something'
or it should be empty if the original variable is an empty string. The thing is, I need the string to be in the form above, including the single quotes around the value.
If I wouldn't need the single quotes, everything works perfectly! However, as I need them, I am trying to escape them using the below snippet. Unfortunately, what I have doesn't seem to work, as \' doesn't apply as expected. How can I properly escape ' so that get them in my string?
tasks:
- include_tasks: ../tasks/get_current.yml
- include_tasks: ../tasks/failed_jobs_stats.yml
vars:
date_param: "{{ date_start != '' | ternary('--date=\''+date_start+'\'', '') }}"
This is not a quoting problem, this is an operator precedence problem.
In your example, you:
apply the ternary filter to the empty string ''
compare the above result to date_start
What you need to do is to enclose the condition in parens:
date_param: "{{ (date_start != '') | ternary('--date=\''+date_start+'\'', '') }}"

Liquid filter to strip HTML comments

Is there any way to remove HTML comments from a string using Liquid? Here is my current code:
{{ post.content | strip | xml_escape }}
Is there any way to do something like:
{{ post.content | strip | xml_escape | strip_comments }}
That removes all HTML comments? I could do this with RegEx and replace (/<!--.-->/g), but liquid doesn't support that.
It is for an RSS feed, so I cannot use any JavaScript.
There is no way to do this. You could, however, use liquid {% comment %} tags inside of your post instead.
To remove all HTML comments from a string using Liquid, you could try:
{{ post.content | strip_html | escape }}

Is there an efficient way to concatenate strings

For example, there is a function like that:
func TestFunc(str string) string {
return strings.Trim(str," ")
}
It runs in the example below:
{{ $var := printf "%s%s" "x" "y" }}
{{ TestFunc $var }}
Is there anyway to concatenate strings with operators in template ?
{{ $var := "y" }}
{{ TestFunc "x" + $var }}
or
{{ $var := "y" }}
{{ TestFunc "x" + {$var} }}
It gives unexpected "+" in operand error.
I couldnt find it in documentation (https://golang.org/pkg/text/template/)
There is not a way to concatenate strings with an operator because Go templates do not have operators.
Use the printf function as shown in the question or combine the calls in a single template expression:
{{ TestFunc (printf "%s%s" "x" "y") }}
If you always need to concatenate strings for the TestFunc argument, then write TestFunc to handle the concatenation:
func TestFunc(strs ...string) string {
return strings.Trim(strings.Join(strs, ""), " ")
}
{{ TestFunc "x" $var }}

How to avoid newlines caused by conditionals?

Given this Go text/template code:
Let's say:
{{ if eq .Foo "foo" }}
Hello, StackOverflow!
{{ else if eq .Foo "bar" }}
Hello, World!
{{ end }}
We get the following output in case Foo equals "foo":
Let's say:
Hello, StackOverflow!
(followed by a newline)
Is there a way to get rid of the extra newlines?
I would expect that this can be accomplished using the {{- and -}} syntax:
Let's say:
{{- if eq .Foo "foo" }}
Hello, StackOverflow!
{{- else if eq .Foo "bar" }}
Hello, World!
{{- end }}
However, that yields an illegal number syntax: "-" error.
In your first template, you have a newline after the static text "Let's say:", and the 2nd line contains only the {{if}} action, and it also contains a newline, and its body "Hello, StackOverflow!" starts in the 3rd line. If this is rendered, there will be 2 newlines between the 2 static texts, so you'll see an empty line (as you posted).
You may use {{- if... to get rid of the first newline, so when rendered, only 1 newline gets to the output, resulting in 2 different lines but no newlines between them:
Let's say:
{{- if eq .Foo "foo" }}
Hello, StackOverflow!
{{- else if eq .Foo "bar" }}
Hello, World!
{{- end }}
Output when Foo is "foo":
Let's say:
Hello, StackOverflow!
Output when Foo is "bar":
Let's say:
Hello, World!
Try it on the Go Playground.
Note that this was added in Go 1.6: Template, and is documented at text/template: Text and Spaces.
If you use the - sign at the closing of the actions -}}, you can even remove all the newlines:
Let's say:
{{- if eq .Foo "foo" -}}
Hello, StackOverflow!
{{- else if eq .Foo "bar" -}}
Hello, World!
{{- end -}}
Output when Foo is "foo" and Foo is "bar":
Let's say:Hello, StackOverflow!
Let's say:Hello, World!
Try this one on the Go Playground.
There is a new line because you're adding a new line after colons (:)
This works https://play.golang.org/p/k4lazGhE-r
Note I just start the first if right after the first colons

Resources