I know it's possible to join an array in twig, like so:
['name_first', 'name_second', 'name_third']|join(', ')
{# name_first, name_second, name_third #}
I also know I can use [5:] to remove the 'name_' part of each element. But, is it possible combine both, without looping over the whole thing. So, the final result will be:
first, second, third
With inspiration from DarkBee's comment, it's pretty easy to achieve.
{{ ['name_first', 'name_second', 'name_third']|join(',')[5:] }}
This removes the first 5 characters of each member of the array then concatenates them with a comma separating them.
Related
There is one condition where I have to split my string in the manner that all the alphabetic characters should stay as one unit and everything else should be separated like the example shown below.
Example:
Some_var='12/1/20 Balance Brought Forward 150,585.80'
output_var=['12/1/20','Balance Brought Forward','150,585.80']
Yes, you could use some regex to get over this.
Some_var = '12/1/20 Balance Brought Forward 150,585.80'
match = re.split(r"([0-9\s\\\/\.,-]+|[a-zA-Z\s\\\/\.,-]+)", Some_var)
print(match)
You will get some extra spaces but you can trim that and you are good to go.
split isn't gonna cut it. You might wanna look into Regular Expressions (abbreviated regex) to accomplish this.
Here's a link to the Python docs: re module
As for a pattern, you could try using something like this:
([0-9\s\\\/\.,-]+|[a-zA-Z\s\\\/\.,-]+)
then trim each part of the output.
I'm trying to write an if statement in twig that doesn't seem to be working.
{% if content.thumbnail_uri != '' and '/podcasts/' not in content.thumbnail_uri %}
The first statement before the and is working properly.
For the second statement I want to ensure content.thumbnail_url (which is a string/URI that is extracted from a JSON string) doesn't contain the sub-string /podcasts/.
How do i write this if-statement correctly in a twig template?
You can use braces to group your statements correctly.
The following syntax will work:
{%
if (content.thumbnail_uri is not empty)
and ("/podcasts/" not in content.thumbnail_uri)
%}
Is it ok to use a (semicolon) ; separator instead of (underscore) _
and adding my own format for easier parsing in the aggregation?
It looks like at the end it just needs to be a string so probably doesn't
matter but would like confirmation.
This is an example aggregation I would like to do.
object_type={{object_type}};verb={{verb.infinitive}};target={{target }};actor={{actor}};time={{time.strftime("%Y-%m-%d-%H-%M-%S-%f")}}
The example shows something like this:
{{ actor }}_{{ verb.id }}_{{ time.strftime('%H') }}
Yes it's just a string. This shouldn't be a problem. Also we're going to be releasing a new interface which will make it easier to test different aggregation formats.
I am using redis storing data, datas are combinated sequences of values, a sequece is combined with separator : and several string values, for example:
value1:value2:value3
the problem is those values may contain : in them, my first thought is escaping : to :: in the values, and then combine them, and I can split them by a solo :.
but this is not perfect, because {'abc', 'aaa:', 'bbb'} will be escaped to {'abc', 'aaa::', 'bbb'} and combined to abc:aaa:::bbb, it's unresolveable. this is probably a stupid question, I'm stuck, how would you solve the problem, or any better suggestion ?
I would instead suggest enclosing the values while inserting using a special identifier towards the beginning and end of string each and then combining them. e.g :
{'%abc%', '%aaa:%', '%bbb%'}
So whenever you want to split them again you can split them using your separator and then replace the prepended and appended value as per you convention to get the original string.
Hope that Helps!
Pretty self explanatory. For example, I would want both test and Test to be replaced:
{{ var|replace({"test": 'replaced'}, ignore) }}
The replace filter is defined as a wrapper for strtr, so there's no built-in way to do that.
Extending twig is easy! Simply create a new filter called ireplace (or something else you find appropriate) that uses str_ireplace in the background.
I think this function is used to replace keywords, not to do a search and replace