Remove the '\' from a string in twig - symfony

I have this text :
Ch d\`Arnb
Now I want to remove all \ from string if exist. I try a lot of solutions, one of this : {{ item|replace({'\/': ''}) }}. But without success. What can I try next?

There is an error in the replace parameter. You are presently trying to remove the / char. Try this:
{% set item = 'Foo\Bar' %}
{{ item|replace({'\\': ''}) }}
Output: FooBar

Related

How to extract the first letter of a word with TWIG?

In a Drupal 8 views, I have a field with a token.
How to extract the first letter of a word with TWIG ?
I just want to display the first letter of the token in this field.
enter image description here
I tested the following code, but it doesn't work :
{{ '{{ name }}'|first }}
Try out :
{{ name|first }}
or
{{ name|slice(0, 1) }}
Twig: How to get the first character in a string
for a text field you can use :
content.field_lastname.0['#context'].value[0:1]

translation strings in twig inside an object

I'm trying to put translation strings inside an object within Twig. So far I haven't got it right and can't figure out how to properly do it. I didn't think this would work, but it was my best effort so far.
{% set options = {
0 : {{ 'user.first_name'|trans }},
1 : {{ 'user.surname'|trans }},
2 : {{ 'user.initials'|trans }}
} %}
I get the error:
A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{".
Any one any ideas? Thanks in advance.
The syntax {{ ... }} is used to output content. You don't need to interpolate the variable in order to add it to the object
{% set options = {
0 : user.first_name|trans,
1 : user.surname|trans,
2 : user.initials|trans,
} %}
Some extra notes. As you are using numeric indices you could use the following snippet
{% set options = [
user.first_name|trans,
user.surname|trans,
user.initials|trans,
] %}
demo

Can I pass a dynamic value from craft CMS to another template as well as a string value?

Can I pass a dynamic value from craft CMS using twig to another template as well as a string value?
Using the following gives me a syntax error
"Twig_Error_Syntax: A hash key must be followed by a colon (:).
Unexpected token "punctuation" of value "," ("punctuation" expected with value ":") "
{% set myEntry= craft.entries.slug('myEntry').first %}
{% embed '_layouts/_hero' with {
'extraClasses': "{{ myEntry.featuredBgPosition }} hero--medium",
'heroImage': myEntry.featuredImage.first
} %}
Other answers say to not use curly braces but this doesn't work either
'extraClasses': myEntry.featuredBgPosition "hero--medium",
and when using it within the quotations I get the string myEntry.featuredBgPosition as a class
'extraClasses': myEntry.featuredBgPosition "hero--medium",
The answers that suggest removing the curly braces are correct, but you need to concatentate strings with variables.
{% set myEntry= craft.entries.slug('myEntry').first %}
{% embed '_layouts/_hero' with {
'extraClasses': myEntry.featuredBgPosition ~ " hero--medium",
'heroImage': myEntry.featuredImage.first
} %}

Keep leading 0 in Twig

In my database, I have a field containing the following data : 000010 (the type is integer)
In my twig tpl, I want to display it, then I do : {{ spending.documentName }}
But the browser displays "10". As if Twig was automaticcaly performing a trim on my data.
I tried {{ spending.documentName|raw }} but it doesn't work. I dont find anything on Google about how to keep leading 0 with Twig.
Does anyone know how to proceed ?
I think you must force the format (as your type is integer).
You can use the format filter :
{{ "%06d"|format(spending.documentName) }}
Or better create a twig extension (http://symfony.com/doc/current/cookbook/templating/twig_extension.html):
public function strpad($number, $pad_length, $pad_string) {
return str_pad($number, $pad_length, $pad_string, STR_PAD_LEFT);
}
It's clearer in your template :
{{ spending.documentName | strpad(6,'0') }}
You need to use the format filter.
Since placeholders follows the sprintf() notation, you should be able to convert sprintf('%06d', $integer); in PHP to {{ '%06d'|format($integer) }} in Twig.
Kinda late here, but ran into the same.
With twig 3.x you can use:
{{your.number | format_number({min_integer_digit:'2'})}}

Symfony2 : Auto htmlentities using Twig

I'm displaying some variable retrieved in my database using Twig :
<p>{{ my_variable }}</p>
The thing is this variable may contain html tags, such as "<br />".
Twig seems to automatically call some htmlentities-like function when displaying variables.
Is there any way to disable it so that when I display a variable containing "Hello<br />world !" I get :
Hello
world !
rather than :
Hello<br />world !
Thanks
Use {{ my_variable|raw }} to prevent my_variable from being automatically escaped.
See Twig documentation: http://twig.sensiolabs.org/doc/filters/raw.html
Try using this
{% autoescape false %}{{ my_variable}}{% endautoescape %}
even better: {{ '<br />|raw('html') }} to avoid unescaping other sensible stuff.
If you just want to use linebreaks in the text stored in your database but don't care to use html , you can also use the nl2br filter as in {{ var|nl2br }}. Allows you to use string linebreak character \n in your text. Filter converts it to <br/>

Resources