Does Twig 'date' support localised dates? - twig

Using this twig code in Craft CMS I'm getting the error shown below. The makers of Craft CMS tell me that the Twig 'date' does not support localized month names like "janvier 2016", but it's fine with English "January 2016".
Is this true?
My Twig:
{% set queryStartDate = date([month, year]|join(' ')) %}
Error:
DateTime::__construct(): Failed to parse time string (janvier 2016) at position 0 (j): The timezone could not be found in the database

Twig's date function is just a wrapper for PHP's DateTime class.
Instead of passing in localized month names into the method, convert them to their numeric equivalents (January/Janvier = 01, etc.) and pass them into yyyy-mm-dd format.
Something like:
{% set queryStartDate = date(year ~ '-' ~ month ~ '01') %}

Related

Convert h:m:s to hh:mm:ss in twig

I have a little problem that I can't seem to solve and ask for your help please:
I have a branch variable which only contains hour, minute and second with no date. The problem is that its format is h: m: s is I would like to convert it to hh: mm: ss
My variable is : {{hours}} which contains for example: 1:5:7 is i would like to get a result 01:05:07.Can you help me please.
Note that I do not have access to the symfony4 controller because I am a designer thank you in advance.
You can do it using date anyways:
{% set hours = "1:2:3" %}
{{ hours|date('H:i:s') }}
#01:02:03

Parametrized twig date

I need how to check if a custom date is inside a range.
The problem is that the API returns a calendar but year and month are sent separately and I can't create a date with twig.
This part of the code seems to return my variables as expected
<script>console.log("Year"+{{CurrYear| json_encode()| raw}});</script>
<script>console.log("Month: "+{{CurrMonth| json_encode()| raw}});</script>
while I can retrieve the day within the loop
<script>console.log("Day: "+{{key| json_encode()| raw}});</script>
I'm trying to create a data item so I can use the native comparisons. I know I can split the other date and compare it one by one, but I'm trying to avoid this
<script>console.log({{ annoForm."-".mesForm."-".key |date('Y-m-d')}}) ;</script>
So I can use it afterwards like
{% set auxDate = annoForm."-".mesForm."-".key |date('Y-m-d') %}
BUT, I can't seem to construct a valid date here so I could use it in:
{% datestart < auxDate and dateend > auxDate %}
{# condition met #}
{% endif %}
Main problem here was concatenating correctly the string when instantiating date():
{% set auxDate = ("#{annoForm}-#{mesForm}-#{key} " | date('Y-m-d') ) %}
I want to add a couple of issues i found while working on this.
First the importance of using date('Y-m-d') instead of "date()" because it will consider July before June (due alphabetic order).
Take nulls or open fields into account (in my case datestart is mandatory)
{% if datestart < auxDate and ( dateend is null or dateend > auxDate ) %}
Hope it helps!

Liquid filter date in Logic App is not working as expected

I have a very simple map written in Liquid and executed in a Logic App.
The map contains only this :
{
"myDate": "{{ "now" }}"
}
The ouput of the map is
{
"myDate": "Now"
}
According to the Liquid documentation it is the right way to generate the current datetime. I tried to put the N in uppercase but it does not work.
Maybe there is a list of supported Liquid features somewhere?
I also tried to apply filters like the date filter in order to change the format of a datetime, but the map is not working as expected.
You have to format the output as this example
{
"Date" : "{{ "now" | Date: "MM/dd/yyyy" }}"
}

Twig RAW filter on literal string not working

{{ (vendorData.description) ? vendorData.description : "<em>No Description Entered</em>"|raw }}
When the value is not present I see:
<em>No Description Entered</em>
Printed literally on the screen in the web browser.
Raw should force the characters to be literal, not > < etc.
Why does this not work on a "created string" but if I do it on a string variable it works?
You need to place brackets around the whole statement like so:
{{ ((vendorData)
? vendorData
: "<em>No Description Entered</em>")|raw }}
Here is a working twigfiddle to show it working:
https://twigfiddle.com/fs2oc2
You can use twigfiddle to experiment with your code.
From feedback in comments section:
here is a twig example to show what you need: https://twigfiddle.com/hjyslr

How not to escape path() in Twig Template

My objective is to render a Twig Template and send the resulting HTML via API to Mailchimp to be sent out.
My current process:
1) create a Twig-Template email.html.twig.
2) $html = $this->renderView('MyBundle:email.html.twig');
3) sendHtmlViaApi($html);
The issue:
I need a URL to contain a Mailchimp Merge Tag String, which has to be *|VARIABLE|*. I do that with {{ path('my_route', {variable : '*|VARIABLE|*'}) }}. The desired result: /myroute/*|VARIABLE|*. The result I get: /myroute/*%7CVARIABLE%7C*.
Already tried and failed methods:
1) using {% autoescape %}
2) |raw
3) Twig Extension with new url_decode Filter from Symfony2 Twig stop escaping path
So you want Twig to stop the automatic URL encoding.
You can pass a placeholder with only letters and underscore to path(), so that it won't be escaped. Then you can replace the placeholder with the string Mailchimp expect:
{{ path('my_route', {variable : 'MAILCHIMP_VARIABLE'})|replace({
'MAILCHIMP_VARIABLE': '*|VARIABLE|*'
}) }}
Thanks for your suggestions!
In the end it was all my own fault... One of the merge tags was missing on the mailchimp-side setup, so it couldn't replace it with the desired value.
Silly me!

Resources