My Python application is using Jinja for the front end. I am trying to truncate the variable that is a string, however it is not working.
I can truncate a string but not a variable.
This fails to truncate:
{{ pagetitle | truncate(9,True,'') }}
This truncates to foo bar b:
{{ "foo bar baz qux"|truncate(9,True,'') }}
I think I have figured this out.
It appears to only trim phrases? {{ "foo bar baz qux"|truncate(9,True,'') }} will truncate, however {{ "foobarbazqux"|truncate(9,True,'') }} will not truncate.
There is a fourth parameter to truncate, and this is the one allowing you to achieve what you are looking for.
Strings that only exceed the length by the tolerance margin given in the fourth parameter will not be truncated.
So, given:
{{ 'foobarbazqux' | truncate(9, True, '', 0) }}
This yields:
foobarbaz
So, in your case:
{{ pagetitle | truncate(9, True, '', 0) }}
This said, since you are using truncate without an ellipsis and want to cut in word (the second parameter is True), you could also consider going for a simpler slice:
{{ 'foobarbazqux'[0:9] }}
So, in your case:
{{ pagetitle[0:9] }}
I had to add an optional leeway value. Not sure what the leeway value is doing. But it's truncating correctly.
pagetitle='whateverwhatever'
{{ pagetitle | truncate(9,True,'',0) }}
Related
I have an array of integers I am rendering with join in a twig template. If the value is zero, however, it is not displaying. Wondering if there is a way to resolve this easily?
Thanks
<b>Snow Last 24 Hours:</b> {{ report.snowLast24Hours|join(' - ') }} in
You can try this (with join and replace):
{{ (snowLast24Hours|join()|replace({'0':''}) is empty) ? '' : snowLast24Hours|join(' - ') }}
Or with join and trim:
{{ (snowLast24Hours|join()|trim('0') is empty) ? '' : snowLast24Hours|join(' - ') }}
Neither of the suggested solutions seemed to work and the '0' values kept getting stripped. I worked around this by printing each individual value rather than using join.
ie.
{{ snow.min }} - {{ snow.max }}
When using Twig's ternary operator, everything goes as expected EXCEPT the |raw filter is not being applied when the condition is false:
{{ thing.description|length > 255 ? thing.description|striptags|slice(0,255) ~ '...' : thing.description|raw }}
When true, it IS applying the |striptags|slice(0,255) filters.
The |raw filter does get applied when coded like this (and false):
{% if thing.description|length > 255 %}
{{ thing.description|striptags|slice(0,255) ~ '...' }}
{% else %}
{{ thing.description|raw }}
{% endif %}
For the life of me I can't figure out why |raw isn't applied when using ternary syntax.
In fact |raw do nothing. Really. Look at its code here.
When you do operation, value is marked as unsafe. |raw mark it as safe. If you try to print unsafe value with {{ ... }}, it will be escaped. It's how Twig is designed.
Let's analyse that simple code: {{ (a|raw) ~ (b|raw) }}
a is marked as safe.
b is marked as safe.
a and b are glued together. It's marked as unsafe, because ~ is a operation.
Value a ~ b is printed ESCAPED. Wow.
You must use {{ (a ~ b)|raw }}.
In your problem: first you calculate thing.description|raw and then it's passed to ternary. Results are marked as unsafe, because ternary is operation. You must use |raw after all operations, so {% if %} is the only solution.
I have a slim/paris/twig application and I have a strange problem with the output.
This is my code:
The title: {{ share.title }}
The output is nothing.
But if I use:
The title {{ share.title | lower }}
The output is:
The title first title
{{ share.title | raw }} works too, but I don't want to use it because it shows accented characteres as weird characters.
Any idea how to solve this ?
Thanks
I want to achive this is twig:
{{ form_widget(form.orderItems.0.enabled) }}
{{ form_widget(form.orderItems.1.enabled) }}
{{ form_widget(form.orderItems.2.enabled) }}
....
but the number to be a variable.
I tried this:
{% set index = 0 %}
{{ form_widget(form.orderItems.index.enabled) }}
Error: Method "index" for object "Symfony\Component\Form\FormView" does not exist
and this:
{{ form_widget(form.orderItems.{{index}}.enabled) }}
Error: Expected name or number
and this:
{{ form_widget(form.orderItems.~index~.enabled) }}
Error: Expected name or number
It is possible to achieve this :(
Some digging suggests you use the 'attribute' function - see Accessing array values using array key from Twig.
I suppose that would be something like
form_widget(attribute(form.orderItems, index).enabled)
Unfortunately I can't easily test that at the moment, but it should get you on the right track.
I am solving the same problem now and getting "expected name or number error" when I want to access variable dynamically.
I can not find simple answer, how to dynamically replace some part of variable in twig.
But It works without first dot as was first comment here.
{{ form[othervariable value].vars.label }}
and NOT
{{ form.[value].name }}
I want to trim a string using twig. The documentation for trim is located here.
{{ 'I like Twig!'|trim('!') }}
{# outputs 'I like Twig' #}
The above example trims exclamation marks from the string.
Consider the following:
{{ 'ROLE_USER'|trim('ROLE_') }}
One would think this would trim ROLE_ and return USER. That's not how it works:
{# outputs 'US' #}
This is because the letters E and R are also in ROLE_, hence they are also removed.
How can I circumvent this, perhaps with a regular expression, or replacing only exactly the string I want?
What about the replace filter?
{{ 'ROLE_USER'|replace({'ROLE_': ''}) }}
//outputs
USER