Dotliquid: value from string - string

Is there a way to get the value from a string?
For example:
"SomeString" has the value "Edward".
Input:
{% assign variable = 'SomeString' %}
{{ variable }}
Output:
SomeString
Note: SomeString is a constructed string during runtime, so I in fact need to get the value from a string --> I can't remove the quotes in the assignment.

There is nothing in DotLiquid which allows to do that, however it is always possible to create your own Tag or to build the template at run time.
public sealed class RuntimeAssign : Tag
{
...
}
Template.RegisterTag<RuntimeAssign>("runtimeassign");

Related

How to create global variable as html

$value = sha1("anything");
$secure = '<input type = "hidden" value = "'.$value.'"/>';
$this->twig->addGlobal('anything',$secure);
After this I access this variable like this on template
{{ anything }}
The big surprise print this variable don't escaped like this
<input type = "hidden" value = "8867c88b56e0bfb82cffaf15a66bc8d107d6754a"/>
I want to print as html tag. But it worked when I used {{ anything|raw }}, I wish it worked without using raw filter
You can mark the variable as safe, without the filter raw, if you wrap it in a Twig\Markup instance, e.g.
$value = sha1("anything");
$secure = new \Twig\Markup('<input type = "hidden" value = "'.$value.'"/>', 'UTF-8');
$this->twig->addGlobal('anything',$secure);

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

Use Less to expand variable to string

Is there a way in Less to expand the contents of a variable to a string?
My specific use case:
#thecolor: #3B98D5
.theclass {
...
}
.theclass:before {
content: "#3B98D5"; // How do I use #thecolor here instead?
}
Then in HTML:
<div class="theclass">
#3B98D5 <!-- What I want in the end -->
</div>
The use case is to create a static page that shows a set of colors, where I would like the color code in the HTML, but only ever specify it in the Less.
So in the end I want it to be a HTML text node. Is this possible at all?
Just enclose the variable within double quotes and use variable interpolation (Format: #{var-name}).
#thecolor: #3B98D5;
.theclass:before {
content: "#{thecolor}";
}
The variable needs to be enclosed within quotes because otherwise the content property will not take it as a string and display. When you use "#theccolor", Less compiler just treats it as a normal string (and not as a variable which is present within quotes needing to be evaluated). The "#{theclass}" is the format for using variable interpolation which lets the compiler know that the value inside quotes is a variable which needs to be evaluated.
.theclass:before {
content: "#3B98D5";
}
.theclasswrong:before {
content: #3B98D5;
}
<div class='theclass'>- Correct Output</div>
<div class='theclasswrong'> - Will not give output without quotes</div>

Check if string variable is null or empty, or full of white spaces

How can I check if a string variable is null or empty, or full with space characters in Twig? (Shortest possible, maybe an equivalent to CSharp's String.IsNullOrWhiteSpace() method)
{% if your_variable is null or your_variable is empty %}
should check whether the variable is null or empty.
If you want to see if it's not null or empty just use the notoperator.
{% if foo is not null and foo is not empty %}
See the docs:
empty
null
"is" operator
logical operators like "not"
Perhaps you might be interested in tests in twig generally.
There are already good answers, but I give my 2 cents too:
{% if foo|length %}
I was inspired by #GuillermoGutiƩrrez's filter trick.
But I think |length is safer as the "0"|trim expression will evaluates to false.
References :
length
codepad
boolval
I'd rather use just trim and empty:
{% if foo|trim is empty %}
{% if foo|trim is not empty %}
empty evaluates to true if the foo variable is:
null
false
empty array
empty string
{% if foo|trim %} seems to be enough (assuming that foo is the variable to check). If foo is not null, trim removes whitespaces. Also, if handles empty string or null as false, and true otherwise, so no more is required.
References:
trim

Coding Implode In Twig

How can one use the equivalent of the implode function in a Twig environment?
For example, the contents of the variable $data1 = "input your name" and the variable $data2 = "input your address".
How to create a variable $result = "input your name, input your address" in Twig?
I'm guessing you're looking for the join filter. It works exactly like implode does in php. Quoting from the manual page:
The join filter returns a string which is the concatenation of the items of a sequence:
{{ [1, 2, 3]|join }}
{# returns 123 #}
The separator between elements is an empty string per default, but you can define it with the optional first parameter:
{{ [1, 2, 3]|join('|') }}
{# returns 1|2|3 #}

Resources