Use two twig extensions on the same variable - twig

I need to print the content of a blogPost (which contains HTML content) with twig, so I have use the raw extension.
I also have to replace a part of the content by another, but how can I use the replace extension if there already is the raw one?
I have tried:
{{ blogPost.getPost()| raw | replace('[[video]]': '(your video here)') }}
{{ {{blogPost.getPost()| raw}} | replace('[[video]]': '(your video here)') }}
{{ blogPost.getPost()| raw, replace('[[video]]': '(your video here)') }}
but none of them works.

replace('[[video]]': '(your video here)')
should be
replace({'[[video]]': '(your video here)'})
That's because you need to pass an array to replace function and not a scalar value
Edit
Try with
{{ blogPost.getPost()|replace({'[[video]]': '(your video here)'})|raw }}

Related

How to show/print same content twice or more using drupal_view in a template?

I have a page where I need to show/print the same content twice. For this, I am using drupal_view.
{{ drupal_view('product_content', 'page_1', node.id) }}
This code works only for the first position and for the second position it doesn't work. What can be done so that the content shows up in both places?
Got the solution from the Drupal community:
When something is rendered, the render system remembers what has been
rendered and won't re-render that. Assign the render output to a twig
variable and print the variable twice:
> {% set product_content = drupal_view( ... )|render %}
> {{ product_content }}
> {{ product_content }}

Displaying malformed html with twig

Assume we have a string
$mystring = "<b> Hello"
How can I display this string using twig while preventing leaking html tags? Or in other word how can I make twig to close tags automatically if they are still open?
I guess {{ mystring | raw }} just prints raw text without verifying / purifying.
sw_sanitize does this already.
{{ '<b> hello' | sw_sanitize }}
Produces:
<b> hello</b
Internally \HTMLPurifier::purify is used, which
Filters an HTML snippet/document to be XSS-free and standards-compliant.

Twig equivalent of lcfirst

I'm not able to find a equivalent of lcfirst php function in Twig, my need is to lower only the first letter of a word ?
If such function doesn't exist, what is the best way to do it ?
As discussed in this issue on Github, you could use:
{{ foo[:1]|lower ~ foo[1:] }}
See this working example.
Just add the function into twig by chaining it with a filter, e.g.
$twig->addFilter(new \Twig\TwigFilter('lcfirst', 'lcfirst'));
Then use it inside any twig template like
{{ string | lcfirst }}
You can use a capitalize filter:
{{ 'my first car'|capitalize }}
{# outputs 'My first car' #}
https://twig.symfony.com/doc/1.x/filters/capitalize.html

Empty variable output using Twig

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

Twig - using variable

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 }}

Resources