Convert h:m:s to hh:mm:ss in twig - 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

Related

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!

Airflow override 'ds' within macro function

I'm trying to pass a dynamic date value into a macro function ds_format.
DEFAULT_DATE= '{{ ds }}'
__init__(self, exec_date=DEFAULT_DATE): self.exec_date = exec_date
Now, I've some macros within the functions like
{{macros.ds_format(ds, "%Y-%m-%d", "%Y%m%d")}}
But, I want to pass the exec_date into the macro like
"""{{ macros.ds_format(""" + self.exec_date + """, "%Y-%m-%d", "%d%m%Y") }}"""
Is this the right way to pass a value to to a micro in Airflow (jinja2) ? or. Is there a different way to pass the dynamic value (exec_date) ?.
You need to just pass as follow:
"""{{ macros.ds_format(ds, "%Y-%m-%d", "%d%m%Y") }}"""
An example is https://stackoverflow.com/a/52137676/5691525
Edited:
Why don't you use the following code if you just want to use exectiuon date:
EXEC_DATE = "{{ execution_date.strftime('"%d%m%Y"') }}"
If you want to just use variable why even use jinja at all? Just use normal python datetime that can be seen in this post: How do I turn a python datetime into a string, with readable format date?
Isn't it simpler to just use the {{ ds_nodash }} instead?
https://airflow.apache.org/code.html#macros

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

Does Twig 'date' support localised dates?

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') %}

Setting a key to array from a variable in Twig

I want to substitute a value from a variable as a key to an array. I have that code:
{{ blogPost.getContent()|replace({truncationMark: ''})|raw }}
I've tried the way, but I've encountered with a problem: truncationMark is interpretated as string! It even replaces "truncationMark" substring in blogpost.getContent().
So what I have do to solve the problem?
I'd be thankful for any help.

Resources