how to use ternary operator in mustache template - node.js

I have a scenario where I am getting {{isdone}} value with Boolean data.
I want to be printed as "pending" for false value and "Done" for true.
I'm using below code, Which isn't working.
{{isdone}} == false ? "pending" : "Done"

Use the ^ block for else.
You can (now) use the ^ block for an else or false condition. Something like this should work:
{{#isdone}}Done{{/isdone}}{{^isdone}}pending{{/isdone}}
Or as a more readable multi-line block of code:
{{#isdone}}
Done
{{/isdone}}
{{^isdone}}
pending
{{/isdone}}

As long as you have control of your context data, correct way is to pass another variable, that will already contain pending or Done beforehand.
If you don't have control over the data, then maybe moustache isn't good for you as you may need template engine that can have some more logic in it to transform data a bit.

You might want to register a ternary helper for that
Handlebars.registerHelper("ternary", function (condition, trueValue, falseValue, options) {
return condition ? trueValue : falseValue;
});
and then in your templates use it like
{{ternary isdone "Done" "pending"}}

Related

Not able to use for loop in ternary operator in arangodb

How do we write conditions in arango, that includes for loops. I can elaborate the requirement below.
My requirement is if a particular attribute(array type) exists in the arango collection, i would read data from the collection(that requires a loop) or else, might do the following :
return null
return empty string ""
do nothing.
Is this possible to achieve in arango?
The helping methods could be -->
-- has(collectionname, attributename)
-- The ternary operator ?:
let attribute1 = has(doc,"attribute1") ?(
for name in doc.attribute1.names
filter name.language == "xyz"
return name.name
) : ""
But this dosent work. Seems like arango compiler first attempts to compile the for loop, finds nulls and reports error as below. Instead, it should have compiled "has" function first for the ternary operator being used.
collection or array expected as operand to FOR loop; you provided a value of type 'null' (while executing)
If there is a better way of doing it, would appreciate the advice!!
Thanks in advance!
Nilotpal
Fakhrany here from ArangoDB.
Regarding your question, this is a known limitation.
From https://www.arangodb.com/docs/3.8/aql/fundamentals-limitations.html:
The following other limitations are known for AQL queries:
Subqueries that are used inside expressions are pulled out of these
expressions and executed beforehand. That means that subqueries do not
participate in lazy evaluation of operands, for example in the ternary
operator. Also see evaluation of subqueries.
Also noted here for the ternary operator:
https://www.arangodb.com/docs/3.8/aql/operators.html#ternary-operator.
An answer to the question what to do may be to use a FILTER before enumerating over the attributes:
FOR doc IN collection
/* the following filter will only let those documents passed in which "attribute1.names" is an array */
FILTER IS_ARRAY(doc.attribute1.names)
FOR name IN doc.attribute1.names
FILTER name.language == "xyz"
RETURN name.name
Other solutions are also possible. Depends a bit on the use case.

Azure Logic App condition - Property contains in object within an array

value is an array with objects that have a property called skuPartNumber (string). How do I make a condition that is true when there are any objects where the skuPartNumber is equal to "X" in the array.
For your requirement, you can use contains function to implement it easily. As your screenshot shows, but need to make some changes.
First, you need to know the expression of value. It seems the value comes from "Parse JSON" in your logic app. So the expression of value should be like body('Parse_JSON')?['value']. Then use a string() function to convert it to string, then judge if it contains "skuPartNumber":"x".
The expression is string(body('Parse_JSON')?['value']).
I think the solution above is easy enough, but if you don't want to think of it as a string to judge if it contains "skuPartNumber":"x". You can also loop the value array, get each item and judge if the field skuPartNumber equals to x. Do it like below screenshot:
After the "For each" loop, use a "If" condition to judge if the variable result equals true or false.

How can I reference an unnamed argument of a when expression?

I have a when expression that looks something like this:
when(foo.toString()){
"" ->'A'
"HELLO" ->'B'
"GOODBYE"->'C'
else ->foo.toString()[0]//problematic method call duplication
}
Now, I don't want to call foo.toString() twice, but I also want this to remain a single expression. Is there a convenient way for me to access the value I passed into the when expression in its else block, such as the it or this# syntax found elsewhere in the language?
I'm currently using the following work-around:
with(foo.toString()){
when(this){
"" ->'A'
"HELLO" ->'B'
"GOODBYE"->'C'
else ->this[0]
}
}
But this introduces another block, and is less readable than I'd like. Is there a better solution?
For the when block there's no variable specified, but you can use the let() function for a similar behavior which might be a little better than your workaround, but behaving the same.
foo.toString().let{
when(it){
"" ->'A'
"HELLO" ->'B'
"GOODBYE"->'C'
else ->it[0]
}
}

How to check the value of a boolean (set by user) with a variable string?

The user sets a boolean to true or false.
That does (exemple)
ElementNameone = true
ElementNametwo = false
ElementNamethree = true
Etc.
Now I have a string that is loaded from a file. The string called name can have values that are Nameone, Nametwo, Namethree, etc. Anyone of them at a time.
Now I would like to be able to do this
if Element .. name == true then
do something
Except I don't know how to do this properly.
I've tried to do
if not not ("Element" .. name) then
But it does not work.
Can anyone help ?
Thanks
Try this:
if _G["Element" .. name] == true then
-- do something
end
Note that this will work only if the variables set by the user (ElementNameone, .. etc.) are globals.
It's very likely you're solving the wrong problem.
You say "the user" sets these variables. How? An end user normally isn't going to be interacting directly with variables inside your program.
Can you use a table instead, with ElementNameone as the key and true or false as the associated value? If so, that would be a lot cleaner.

how to retrieve selected item from ListBox and convert it to string?

I tried
if( ListBox.SelectedItem.ToString().Equals("test")
{
//do something
}
and
if( ListBox.SelectedValue.ToString().Equals("test")
{
//do something
}
none of them gets the selected value or item into string
if that is actual looking of your code there are mistakes. you should add one brace at the end of if statement.
if not i don't see any mistakes at there. But beware if you use SelectedItem.toString(). That is not giving you the value. That returns the object value. example it will give you something like this "System.Data.DataRowView". So that condition never comes true.
that's all i can see from your code.

Resources