Freemarker: comparing against a default value - comparison

I have this condition:
<#if tag.level?? && tag.level == "IMPORTANT">
Is it possible to shorten it to something like this?
<#if tag.level!"" == "IMPORTANT">
If I try this
<#assign tag = {"bar": "AA"} >
${ ((tag.bar)!"x") = "x" }
I get
Can't convert boolean to string automatically, because the "boolean_format" setting was "true,false", which is the legacy default computer-language format, and hence isn't accepted.
See http://freemarker-online.kenshoo.com/

1) ${ ((tag.bar)!"x") = "x" } should be probably ${ ((tag.bar)!"x") == "x" }.
2) Use a built-in to display boolean, either ${ (((tag.bar)!"x") = "x")?c } (if your Freemarker is newer than 2.3.20), or ${ (((tag.bar)!"x") = "x")?string("true", "false") }

It's a precedence issue that you run into. This works:
<#if (tag.level!"") == "IMPORTANT">
but for this kind of default there's a shorter form:
<#if tag.level! == "IMPORTANT">

Related

I try to write a condition in thymeleaf with OR

I need to have a condition with logical operator OR. I try to do in this way but it seem doesn't work :
<div th:if="${(fingerprints.totalPages != 0) or (fingerprints.totalPages != 1)}"
How I suppose to do ? :)
Both or and || work. In this case, your logic is wrong. I think your expression should be:
th:if="${(fingerprints.totalPages != 0) and (fingerprints.totalPages != 1)}"
Your original expression is always true. (Since fingerprints.totalPages is always going to be either != 1 or != 0.

How to filter a lot of items in an array

I'm trying to filter an array in swift, it works great when I'm just trying to filter a few things but when I add to the list I get this error:
Expression was too complex to be solved in reasonable time; consider
breaking up the expression into distinct sub-expressions
Here is my code with the above error:
filteredArray = workArray.filter { $0.stateName.localizedCaseInsensitiveContainsString(searchString!) || $0.firstName.localizedCaseInsensitiveContainsString(searchString!) || $0.lastName.localizedCaseInsensitiveContainsString(searchString!) || $0.countyName.localizedCaseInsensitiveContainsString(searchString!) || $0.cityName.localizedCaseInsensitiveContainsString(searchString!) || $0.communityName.localizedCaseInsensitiveContainsString(searchString!) || $0.sectionName.localizedCaseInsensitiveContainsString(searchString!) || $0.notes.localizedCaseInsensitiveContainsString(searchString!) || $0.email1.localizedCaseInsensitiveContainsString(searchString!) || $0.email2.localizedCaseInsensitiveContainsString(searchString!) || $0.email3.localizedCaseInsensitiveContainsString(searchString!) || $0.title.localizedCaseInsensitiveContainsString(searchString!) || $0.jobsiteID.localizedCaseInsensitiveContainsString(searchString!)}
I have tried to split this process up like this
filteredArray = workArray.filter { $0.stateName.localizedCaseInsensitiveContainsString(searchString!) || $0.firstName.localizedCaseInsensitiveContainsString(searchString!) || $0.lastName.localizedCaseInsensitiveContainsString(searchString!) || $0.countyName.localizedCaseInsensitiveContainsString(searchString!) || $0.cityName.localizedCaseInsensitiveContainsString(searchString!) || $0.communityName.localizedCaseInsensitiveContainsString(searchString!) || $0.sectionName.localizedCaseInsensitiveContainsString(searchString!) || $0.notes.localizedCaseInsensitiveContainsString(searchString!) || $0.email1.localizedCaseInsensitiveContainsString(searchString!)}
and
filteredArray.appendContentsOf(workArray.filter { $0.email2.localizedCaseInsensitiveContainsString(searchString!) || $0.email3.localizedCaseInsensitiveContainsString(searchString!) || $0.title.localizedCaseInsensitiveContainsString(searchString!) || $0.jobsiteID.localizedCaseInsensitiveContainsString(searchString!)})
But I am getting duplicate objects in the array.
I could write something else that would then look for and delete duplicate objects but I would rather not. My question is how should I be filtering all this items.
Thank you for all the help
Factor that behemoth of an expression out to a method on your datatype.
extension MyDataThingy {
func anyFieldContains(searchTerm term: String) -> Bool {
let fieldValues = [self.stateName, self.firstName, /* etc. */]
for value in fieldValues {
if value.localizedCaseInsensitiveContainsString(term) {
return true
}
}
return false
}
}
Then:
filteredArray = workArray.filter { $0.anyFieldContains(searchTerm: searchTerm) }
This will fix the timeout error from the type inference engine. It is also more readable, more understandable, and more maintainable.
Extended syntax
Try the extended syntax
workArray.filter { elm -> Bool in
// put your conditions here
}
this way you are helping the compiler to understand that the closure receive an element of the type of your array and returns a Bool value.

add macro parameter of type Dropdownlist umbraco

I am using umbraco version 7.2.6 . I want to add macro parameter of type Dropdownlist .
How can I set source (data come from database ) of dropdownlist ??
thanks
Had the same situation, I am aware that the 'proper' way to do it would be like described in http://www.richardsoeteman.net/2010/01/04/createacustommacroparametertype.aspx, but for my purpose this would have been too much fuss. What I suggest here instead is not elegant, but it's easy to implement.
Create a macro parameter type Numeric and explain in the description which number stands for which result. In the Macro Partial View assign the number to the respective result.
Example
Description of the Macro parameter:
Alias: dimension
Description: 1: 300x225 2:400x300 3:600x450 4:800x600
Type: Numeric
Code in the Macro Partial View:
var defaultdim = "medium";
if (Model.MacroParameters["dimension"] != null)
{
var dim = Convert.ToInt32( Model.MacroParameters["dimension"] );
if(dim == 1) { defaultdim = "small"; }
else if(dim == 2) { defaultdim = "medium"; }
else if(dim == 3) { defaultdim = "large"; }
else if(dim == 4) { defaultdim = "xlarge"; }
}
"small", "medium" ... are crop names and stand for the dimensions shown in the parameter description.

Is is possible to have strings evaluate as variables in groovy?

I am playing with grails and groovy. I wondered if its possible to do something like this.
def inbuiltReqAttributes = ['actionName','actionUri','controllerName','controllerUri']
inbuiltReqAttributes.each() { print " ${it} = ? " };
what would i put in the ? to get groovy to evaluate the current iterator value as a variable e.g. to do it the long way
print " actionName = $actionName "
Thanks
I believe off the top of my head, this should work:
print " ${it} = ${this[ it ]}"
Or:
print " ${it} = ${getProperty( it )}"
But i'm not at a computer to 100% verify this atm...
Try this:
inbuiltReqAttributes.each() {
evaluate("value = ${it}")
print "$it = $value"
}

Why would you add "or false" to the end of your boolean expression?

I have seen code in lua like this
if (a==b or false) then
what is the purpose of the "or false"?
This is the original code:
Function = function(self, aura, auraID)
-- Store the version.
-- aura.Version = 50000;
-- Fix texmode/glow.
aura.texmode = (aura.texmode == 1 or aura.texmode == true or false);
-- Texture source.
if(aura.owntex or aura.SourceType == PowaAuras.SourceTypes.Icon) then
aura.SourceType = PowaAuras.SourceTypes.Icon;
elseif(aura.wowtex or aura.SourceType == PowaAuras.SourceTypes.WoW) then
aura.SourceType = PowaAuras.SourceTypes.WoW;
elseif(aura.customtex or aura.SourceType == PowaAuras.SourceTypes.Custom) then
aura.SourceType = PowaAuras.SourceTypes.Custom;
elseif(aura.textaura or aura.SourceType == PowaAuras.SourceTypes.Text) then
aura.SourceType = PowaAuras.SourceTypes.Text;
else
aura.SourceType = PowaAuras.SourceTypes.Default;
end
end
And here is another example
-- Iterate over element children.
visible, hidden = self:UpdateScrollList(key, level+1, visible, hidden,
(parentShowing == true and item:GetExpanded() or false));
This looks like a behavior that comes from the syntax:
a=b or false
to initialize a to the value of b or false if b isn't defined.
Within the if statement as you wrote it, I don't see any purpose since the == is evaluated before the or. If they used parenthesis to change the order of operations, then it could be used to validate that b has been defined, e.g.:
> a=nil
> b=nil
> if (a == (b or false)) then print("yikes") else print("aok") end
aok
> if (a == b or false) then print("yikes") else print("aok") end
yikes
It is used to temporarily disable a block of Code.
It's semantics are technically the same as:
#if 0
dead code goes here ...
#endif
It's to explicitly state the value assigned to the expression when none of its prior conditions are true.
In Lua, the and and or expressions return the values that determine their truth values, rather than the flat truth value itself, like this:
function And(A,B)
if A then return B --if A is true, then the agreement of both depends on B
else return A end --otherwise, they're not, because A wasn't
end
function Or(a,b)
if A then return A --if A is true, then it doesn't matter if B was true
else return B end --otherwise, the truth hinges on B's value
end
In Lua, all values other than false and nil evaluate to true in conditional constructions, so this behavior is commonly used as an idiom to describe default values:
local color = color or 'blue' -- if `color` is defined to any value other than
-- `false` (such as any string), it will evaluate
-- as true, and the value will be returned from
-- the `or` statement (so `color` will be assigned
-- its own value and no change will result) -
-- if `color` is not defined, the `or` statement
-- will evaluate its `nil` value as false and
-- return the second value, 'blue'
This idiom can be combined and extended in various ways:
local color = color or self.color
or (favorites and favorites.color)
or (type(favourites)=='table' and favourites.colour)
or get_defaults('color')
or {'red','blue','green','yellow'}[math.random(1,4)]
Traditionally, though, they stay fairly simple, with maybe a few chained or cases before the default value.
That's what we're seeing here: the cases where or false is being used are for the last-case value for the expression.
So, while this:
aura.texmode = (aura.texmode == 1 or aura.texmode == true or false);
could have been written as this with exactly the same effect (since the == comparisons will return either true or false):
aura.texmode = (aura.texmode == 1 or aura.texmode == true);
adding the final or false makes it more obvious when looking at that line that the value can be false.
The same goes for the second example, in which the value is being assigned to an argument.
There is no use, because if a~=b then it will try false, which is ... false and return false anyway ...

Resources