I want to make an excerpt of an entity resume.
So I do
{{ randBook.resume|slice(0, 200) ~ '...'}}
It work but i'm french and all of my spécial caracteres are like this #%00 so how I want to convert into UTF-8 ?
And I Try it
{{ randBook.resume|striptags|convert_encoding('UTF-8', 'ISO-8859-1')|slice(0, 200) ~ '...'}}
But it's the same.
Thanks for your help
Related
I would like to format the numbers being displayed in a "Computed Twig" element in Drupal 8 Webform. I've tried Twig's "number_format" filter, but it does not work. I have this:
{{ (data.auswahl_oder_eingabe) * 202 / 1000000 * 25|number_format(2, ',', '.') }}
I would like to have only two decimals, the dot as a thousands separator and the comma as a decimal point. But I always get something like this:
53.025
So three decimals and the dot as a decimal separator. The format I would like to have in this case would be: 53,03 No matter what I write in that "number_format" definition, the result doesn't change. Does this filter work at all in Drupal 8.7.7? Or is there a mistake I made somewhere in the markup?
I believe that your filter is only applying to the 25 not the entire calculated value. Try something like this:
{% set calculated = data.auswahl_oder_eingabe * 202 / 1000000 * 25 %}
{{ calculated|number_format(2, ',', '.') }}
most likely a silly question, but I do not see how to do.
Within a controller, I need to use $myObj->getData()->format('d-M-Y'), and I wish to get a localized string too (in italian instead than in english).
Within a twig template, I get it by {{ myobj.data|localizeddate('long', 'none', app.request.locale ) }}, but I do not know a similar trick for the former case.
Yes, I found how to do (thanks to ccKep):
$cal = IntlCalendar::fromDateTime($oldObj->getData()->format('d-M-Y')." Europe/Rome");
$newObj->setField("my date is ". IntlDateFormatter::formatObject($cal, "d MMMM YYYY", 'it_IT')."");
In controller you may use Intl. For example:
$intl = new \IntlDateFormatter($request->getLocale(), \IntlDateFormatter::LONG, \IntlDateFormatter::NONE, null, null, 'd-LLL-y');
$date = $intl->format(new \DateTime('now'));
In my Twig project I use translation from .yaml files. The text which I need to translate is found in a .html.twig file and says, for example,
"My text for translation contains a {{parameter}} to translate."
I know that I can replace this whole string with a key word, for example - to_translate %parameter% to_translate2, and I can use the translation from my .yaml file like this
to_translate: "My text for translation contains a"
to_translate2: "to translate"
And the parameter will be passed. However, how can I do this without breaking the sentence in so many parts?
Maybe I didn't get your point, but you can add as many parameters as you want ?
Yaml file:
my_translation_key: Hello %firstname%, %lastname%, welcome here !
In Twig:
{{ 'my_translation_key' | trans({
'%firstname%': 'John',
'%lastname%': 'Doe'
}) }}
If what you want is to get nested blocks in your translations, you can probably try something like this:
Yaml file:
my_translation_key: Hello %firstname%, %lastname%, %welcome% !
welcome_block: welcome %where%
In Twig:
{% set welcome = 'welcome_block' | trans({'%where%': 'here'}) %}
{{ 'my_translation_key' | trans({
'%firstname%': 'John',
'%lastname%': 'Doe',
'%welcome%': welcome
}) }}
Maybe you should try this :
{{ ('My text for translation contains a '~parameter)|trans }}
In Documentation :
~: Converts all operands into strings and concatenates them. {{ "Hello " ~ name > ~ "!" }} would return (assuming name is 'John') Hello John!.
How can I multiple two decimal values in twig ?
Let's say var1 = "37.57" and var2 = "8" and I tried with
{{ var1 * var2 }}
But it multiplies only the integer value while ignoring the decimal it returns "296" which is 37 * 8.
Can you try with
{{ var1|number_format * var2|number_format }}
Maybe it doesn't work because of string format (even if string format should work).
Doc for number_format
The integer result may be produced if wrong decimal separator is used.
Okay I got it working.
For some reason the twig is considering it as a "string" instead of "interger" not sure why it is, since i simple return the data that i receive through external API connection.
here is the thing which worked for me:
{% set var_1 = 0 + var1|numberformat(2,'.',',') %}
{% set var_2 = 0 + var2|numberformat(2,'.',',') %}
then,
{{ (var_1 * var_2)|numberformat(2,'.',',')}}
I'm having a problem with the twig date filter. I'm trying to universally change any post time to PST. But if I were to post an item at 5:20 ET, the post will say 9:20pm (which is UTC) when it is supposed to say 2:20pm (which is PST). I just want to know how to change UTC to PST because the date filter is not doing it for me.
{{ post.published_at }}
will give 2013-12-08 21:20:46"
{{ post.published_at|date("F jS \\a\\t g:ia", "PST") }}
will give "August 12th at 9:20pm"
Try
{# using "PST" is fine too as I noticed #}
{{ post.published_at|date("F jS \\a\\t g:ia", "America/Los_Angeles") }}
The timezone parameter uses the accepted values from PHP. Los Angeles should be PST so it should work.
See the list of supported timezones.
It's also shown in the twig documentation (or at least there's a hint) where they use Europe/Paris as timezone.
EDIT
Example to change an existing date, assuming you have a DateTime object.
PHP:
$date = new \DateTime('2013-12-08 21:20:46');
$pst = new \DateTimeZone('PST');
$date->setTimezone($pst);
And in twig:
{{ date|date("F jS \\a\\t g:ia", "PST") }}
Will output December 8th at 12:20pm