How to create a multiline string variable in swig template? - node.js

I'm trying to create a multiline string variable in swig :
{% set myVariable = "
multi
line
string
" %}
Then I log it :
{% logMyVariable(myVariable) %}
But I don't understand why my variable is not displayed on several lines :
multi line string
Whereas I was expecting :
multi
line
string
Do you know how I could do that?

In HTML, new lines in text aren’t rendered when the text is displayed. For example, this HTML:
<p>Let’s split this sentence up
onto
multiple
lines.</p>
Will render like this:
Let’s split this sentence up onto multiple lines.
You might want to wrap your log in a <pre> tag, as that will preserve new lines (and multiple spaces between words, which are also ignored in rendered HTML):
<pre>{% logMyVariable(myVariable) %}</pre>

Related

How to define "ends with any two letters after underscore" in Twig

In the following example, this condition includes all type elements of an array that do not include _.
{% for type in array %}
{% if '_' not in type) %}
Instead, I would like to include all elements that do not end with _any2letters, where "any2letters" is actual any 2 letters. I examined Twig documentation and wasn't able to find the required syntax.
It was solved using:
{% if not (type matches '/_[a-z]{2}$/') %}
This solution takes advantage of PHP regex syntax.
"/" are used in Twig to define borders of regular expressions.
"_" is my underscore as is; [a-z]{2} means "2 lowercase letters".
And finally $ means that the preceding symbols are in the end of the
string.

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!

Long strings within variables in jade

I am writing a web application that supports multiple languages. What I am doing is passing strings to a translator class and it will perform the translation. Now I need to add some long text in my templates and I would like to have it on multiple lines for readability.
- var longText = 'Some really long text ... ';
// ...
p #{i18n.tr(longText)}
I would like to do something like
- var longText = 'Some text ' +
- 'some other text'
// ...
p #{i18n.tr(longText)}
Unfortunatly jade does not like that.
Is it possible to have strings on multiple lines in jade?
According to this forum, it is undocumented but ending a line in a backslash allows a string to continue onto the next line.
https://github.com/jadejs/jade/issues/1447

embedded spaces in a computed field don't show

I have some code in a computed field where I want to embed some spaces between two values:
var rtn:String = doc.getItemValue("RefNo")[0] + " " + doc.getItemValue("Company")[0];
the computed field Display Type = text and the content type is String but the display strips out all the extra spaces. Is there a function like insertSpaces(5) that would insert 5 hard spaces?
Figured it out insert "&#160" + "&#160" and display as HTML. fairly simple but really ackward.
It will print them out as whitespace in HTML output. It will not be rendered.
You can add instead (5 times in your case).
An alternative way is to set style="white-space: pre;" (works IE8+ and other browsers)

Resources