i want to know what this symbol | mean and when we use it
{{ entity.date|date('d-m-Y')}
Could someone explain it for me?
This is called a filter. It applies a filter to a variable or expression to the left. For example
{{ name|striptags }}
will apply a striptags filter to a name variable.
In your case a date formatting filter is applied, to make the date look according to a certain format.
The full list of builtin filters can be found here.
Filters can be chained, for example
{{ name|striptags|title }}
will apply a striptags filter to a name variable and then apply a title filter to the resulting value.
Related
For instance, you can see {{{body}}} and in the templates, you are able to do something like {{data.page.hero.text}}
Is there any significant difference we should be aware of?
Handlebars HTML-escapes values returned by a {{expression}}. If you don't want Handlebars to escape a value, use the "triple-stash", {{{.
Reference: https://handlebarsjs.com/guide/expressions.html#html-escaping
Depending on your use case and logic, you may add an option "noEscape" set to true when compiling the template if you want to use {{ }} when {{{ }}} are required to successfully replace the templates.
For example: replacing with JSON values.
From the documentation:
var template = Handlebars.compile('{{foo}}', { noEscape: true });
noEscape: Set to true to not HTML escape any content.
I have product name, which contains only one number.
Product.name="Pack of apples (12 pcs).";
Product.price=6;
What I'm trying to do - is to put 12 to variable, to do a little math later (ex: show how many costs 1 apple).
I was tried to do following:
{{ set apple = attribute(product, 'title'~lang)|replace('/[^0-9]/', '') }}
but with no luck.
I only have access to modifying .twig files, so any workaround would be appreciated
I would not reccommend using this way, however in this circumstance, the required data could be retrieved using simple split commands.
{% set bar = "Pack of apples (12 pcs)." %}
{% set foo = bar|split('(')[1]|split(' p')[0] %}
{{ foo }}
This would lead to foo containing 12.
However again, this would not be the best way to approach this and the use of an amount variable would be more approproate.
Is it possible in Twig (used with Symfony2) to round number max to 8 decimal places OR less?
E.g.
number is 10.0001 so I would like to display it as 10.0001 (not 10.00010000)
number is 10.0000000000001 - display 10.0 (or 10)
number is 10.00100000001 -> display 10.001
Function number_format allows only to set static number of decimal places (e.g. always 8).
You might be able to get the result using number_format and trim like
{{ your_number | number_format(8) | trim (0) }}
That being said a custom twig extension like #bosam has recommended would probably be the easiest to use/remember in the future.
Best way for you to do this is a Custom Twig Extension implementing your logic.
That way you could use {{ myNumber | round_number }} and have the right rounded number to display.
My goal is to mask one digit from a 4-digit string. Instead of having 2451, I'd like to have 24*1.
I tried {{ my_var|replace(slice(2, 1): '*') }} but this raises the following error: The function "slice" does not exist in My:Bundle:file.html.twig.
The weirdest thing being that {{ my_var|slice(2, 1) }} works perfectly. So the function does exists.
How can I do what I want to achieve?
Many thanks.
Create Your own Twig extension - filter:
SymfonyCookbook
IMHO it would be cleanest way to do this.
slice is a filter not a function, you can try to pipe them but in your case i do not see something achievable without creating your custom twig function or filter to mask your needs:
This is a question related to getting Drupal CCK fields (just in case that happens to change anything).
I have several Drupal CCK fields with similar names. They have the same name with a number at the end. that I'd like to pull values from these fields (ten fields total). This is the syntax for accessing the fields values:
$node->cck_field_1[0]['value']
$node->cck_field_2[0]['value']
$node->cck_field_3[0]['value']
…etc.
Since they're all separate fields, but they're numbered, I'd like to just loop through incrementally to write out what I need (there's a lot more to what I'm writing than just accessing these fields' data, but they're the determining factors of the rest), but I can't figure out how to insert a variable into that part of the code.
e.g., (if $i were the incremental number variable), I'd like to be able to write the following string as a variable:
'$node->cck_field_' . $i . '[0]["value"]'
I understand about using the curly brackets to create a variable name from a string, but the part I need the variable in needs to be outside of the string. e.g. this works:
${node}->cck_field_1[0]['value']
but this doesn't:
${node->cck_field_1}[0]['value']
(so I can't write ${'node->cck_field'.$i}[0]['value'] )
So how can write this so that I can use $i in place of the number?
This should work:
$node->{'cck_field_' . $i}[0]['value']