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:
Related
I want to use values from gradle.properties that should go into a template string.
A naive first:
println("${project.properties[somekey]}")
doesn't work: Unresolved reference: somekey
So, quotes required?
println("${project.properties[\"somekey\"]}")
is completely broken syntax: Expecting an expression for the first .
I couldn't find any example how to do this, yet the official documentation says expressions.
Question: is it possible to access a map in string template, and if so, how?
Yes and as follows:
"${project.properties["someKey"]}"
assuming the Map has the following signature: Map<String, Any?> (or Map<Any...)
Alternatives:
"${project.properties.getValue("someKey")}"
"${project.properties.getOrElse("someKey") { "lazy-evaluation-default-value" }}"
"${project.properties.getOrDefault("someKey", "someFixedDefaultValue")}"
Basically all the code you put in the ${} is just plain Kotlin code... no further quoting/escaping required, except for the dollar sign $ itself, e.g. use "\$test" if you do not want it to be substituted with a variable named test or """${"$"}test""" if you use a raw string
Note that in this println case the following would have sufficed as well (which also goes for all the shown alternatives above. You may omit the outer surrounding quotes and ${} altogether):
println(project.properties["someKey"])
See also Basic types - String templates
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.
I'm using objects in my view that I recover from a iCalendar file, these objects have string attributes, named description, and some of these strings contain "\n", and for some multiple times and I can't seem to find a way to trim them out.
I've tried to trim them using the php function in the controller, however they still are there after the function.
Same for the trim function in Twig.
I am maybe using wrong parameters in these functions.
{{ coursCourant.description|split("(")[0]|split(".")[0] }} <BR>
This displays something like "\n\nPromo S4\n" when the expected result is the same thing but without the multiple "\n".
I'm using splits on a ( and on a . because some strings contains them and I don't need to display the parts after those.
trim will only remove trailing and ending whitespaces.
Use str_replace in your controller (recommended)
$content = str_replace("\n", '', $content);
Use replace in your template
{{ foo|replace({"\n":'',}) }}
hence the double quotes to remove an actual newline character
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.
There are two Airflow macros available currently: ds_add and ds_format.
I would like to know the syntax for using them both.
For example, I currently can use one of them like this to add 7 days to a date:
dt7 = '{{ macros.ds_add(ds, 7) }}'
However, I actually need to do something like this and get back a YYYYMMDD format without using datetime or any other python package, since I need to feed this into an Operator:
dt7_fixed = '{{ macros.ds_add(ds_nodash, 7) }}'
But ds_add does not support 'YYYYMMDD' format, only 'YYYY-MM-DD'.
A workaround is to use ds_format in that one-liner too, but I can't grok the right syntax.
I believe this will do what you want:
{{ macros.ds_format(macros.ds_add(ds, 7), '%Y-%m-%d', '%Y%m%d') }}
You should think of these macros the same way you would think of functions.
As per the link you included above ds_format takes in 3 params, date string and two strings representing desired input and output format.
The output of the originally used ds_add macro is a string which you can use as the first argument here.
Note in the source that this uses datetime under the hood.