Setting a key to array from a variable in Twig - 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.

Related

How to reference a Twig expression inside an expression

I'm trying to reference a Twig expression inside another expression:
{{ dbquery('name', 'product_manufacturer_translation', {'product_manufacturer_id =' lineItem.payload.manufacturerId} ) }}
Here, lineItem.payload.manufacturerId is the Twig expression I'm trying to put into the dbquery - but this does not work. Thanks for sharing your thoughts!
Edit: this query does not yield a result. What I would expect is a product manufacturer name, based on the product manufacturer ID. The manufacturerId experssion itself, when used on its own, does yield a result. I know there is a database record that should match this query, too.

Formatting string in relplot subplot title

Please note this is an edited version of my previous post.
I am trying to format the title of subplots generated using relplot in Seaborn. I am using the set_titles option and am including the {col_name} element. I would like to precede {col_name} with a dynamic string from the array of formatted strings "plotvarnames" (e.g., "I_{bs}=") before {col_name}. The following lines of code results in the error message "name 'col_name' is not defined":
plotvars=['nis','Ds','Ibs','kVssH','kVssL','kHL']
plotvarstrings=["r'$\nu_s$'","r'$D_s$'","r'$I_{bs}$'","r'$k_{vSSH}$'","r'$k_{vSSL}$'","r'$k_{HL}$'"]
for i,vname in enumerate (plotvars): g=sns.relplot(x="xvar",y="yvar",col=vname,hue="huevar",col_wrap=3,kind="scatter",palette=["b","r"],data=df,legend="full").set_titles(f"plotvarstrings[i]={col_name}")
How can I obtain the composed, formatted string?
Aha, I figured it out: you need to escape the brackets. Change plotvarstrings to the below (note the double {{ and }}:
plotvarstrings=['r[$\nu_s$]','r[$D_s$]','r'$I_{{bs}}$]','r[$k_{{vSSH}}$]',
'r[$k_{{vSSL}}$]','r[$k_{{HL}}$]']
Then you can do this:
.set_titles(plotvarstrings[i] + "={col_name}")

Inserting braces with format in python

I am trying to print this string
str = "container.insert({{ {0} ,{{ {{ {1}, {{ {2} ,{3} ,{4} , {5} }} }} }} }});"
str.format(prim_ident,country_ident,final[0],final[1],final[2],final[3])
fileWrite.write(str)
However the output of above that I get is
container.insert({{ {0} ,{{ {{ {1}, {{ {2} ,{3} ,{4} , {5} }} }} }} }});
Two problems the first problem is that i am getting double curly braces. I only want to show a single curly brace. But i read that you have to use double curly braces when u would like the curly brace to be present in the string the other problem is my format is not working (i.e) {0},{1} etc are not being replaced by their equivalent values. Can anyone please tell me what I am doing wrong ?
str.format() does modify the string in-place, you still need to save it to a variable, try adding str = :
str = str.format(prim_ident,country_ident,final[0],final[1],final[2],final[3])

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

How not to escape path() in Twig Template

My objective is to render a Twig Template and send the resulting HTML via API to Mailchimp to be sent out.
My current process:
1) create a Twig-Template email.html.twig.
2) $html = $this->renderView('MyBundle:email.html.twig');
3) sendHtmlViaApi($html);
The issue:
I need a URL to contain a Mailchimp Merge Tag String, which has to be *|VARIABLE|*. I do that with {{ path('my_route', {variable : '*|VARIABLE|*'}) }}. The desired result: /myroute/*|VARIABLE|*. The result I get: /myroute/*%7CVARIABLE%7C*.
Already tried and failed methods:
1) using {% autoescape %}
2) |raw
3) Twig Extension with new url_decode Filter from Symfony2 Twig stop escaping path
So you want Twig to stop the automatic URL encoding.
You can pass a placeholder with only letters and underscore to path(), so that it won't be escaped. Then you can replace the placeholder with the string Mailchimp expect:
{{ path('my_route', {variable : 'MAILCHIMP_VARIABLE'})|replace({
'MAILCHIMP_VARIABLE': '*|VARIABLE|*'
}) }}
Thanks for your suggestions!
In the end it was all my own fault... One of the merge tags was missing on the mailchimp-side setup, so it couldn't replace it with the desired value.
Silly me!

Resources