How to check if a string is false or empty in dustjs? - dust.js

Consider these two JSON objects:
{
note: false
}
or
{
note: ""
}
And if the below code is used to check note value, it prints ccccc in both the cases.
{#select key=note}
{#eq value=false}aaaa{/eq}
{#eq value=true}bbbb{/eq}
{#default}ccccc{/default}
{/select}
Can anyone suggest if there is any other way available to get different output in case of false and empty?

Cast to strings. Also, if you're using an up-to-date dust-helpers, you should use {#none} instead of {#default}.
{#select key=note}
{#eq value="false" type="string"}note was false{/eq}
{#eq value=""}note was empty string{/eq}
{#eq value="true" type="string"}note was true{/eq}
{#none}note was something else{/none}
{/select}

Related

Is there a way to differentiate between false and undefined in a dust template?

Is there any way to differentiate between false and undefined in a dust template? I believe:
{^myVariable}
Hello
{/myVariable}
would print Hello if myVariable is undefined right?
As you've noticed, Dust doesn't compare on truthiness, but on "emptiness", so you can't specifically check for a variable's falsiness.
You can use the {#eq} comparator instead, but you have to be careful with what you compare. You can't blindly use the Javascript keyword undefined, because Dust will just read it as a reference to the variable undefined (which is probably safe, but could give you a bug). So this is OK:
{#eq key=myVariable value="undefined" type="string"}myVariable is undefined{/eq}
{#eq key=myVariable value=somethingImSureDoesntExist}myVariable is undefined{/eq}
But you can't test the reverse, since using type="boolean" will cast both key and value
{#eq key=myVariable value="false" type="boolean"}
myVariable might be 0, undefined, null, false, etc...
{/eq}
{#eq key=myVariable value=somethingInContextIKnowIsAlwaysFalse}
This is sloppy because you have to include a dummy "false" in your context, but it works!
{/eq}
So if you really need to test === false, you should write a quick helper:
dust.helpers.isFalse = function(chunk, context, bodies, params) {
return context.resolve(params.key) === false;
}
And use it like:
{#isFalse key=myVariable}
myVariable is exactly false!
{:else}
myVariable is something else
{/isFalse}
All this said, you will probably be much happier if you allow Dust to use its emptiness checks instead, by not requiring your templates to care about the difference between undefined and false.

sysproperty set to value "${value}" when no property is passed to target

I am executing a target with paramters as Dcolor=red.I am using this property value to set system property as
<sysproperty key="COLOR" value="${color}" />
If not passing any parameter named -Dcolor, when fetching key COLOR as System.getProperty("COLOR") is returning ${color}.
Is this correct behavior. How to handle such scenario ie. setting COLOR as null if no parameter names color is passed.
Please let me know if I am missing basics here. Resource link will be helpfull
That's crazy, the evaluation of the properties is done by the PropertyHelper class of the ant. But based on the source code, the returned value should be null, but it's not.
The documentation says the ant tries to evaluate it, but it doesn't state what happens if the evalutation is unsuccessful.
You can set a default value by predefining the property.
<property name="COLOR" value="defaultColor" />
But this will never set value null.
And finally after a long search, I found out that using syspropertyset instead of sysproperty, will return nulls.
This is a working example:
<propertyset id="java.props">
<propertyref name="test.property"/>
</propertyset>
<java>
<syspropertyset refid="java.props"/>
</java>
If you get the test.property, it will return null.

MVC3 edit for decimal fields and localization

My locale uses a comma ,, and not a dot . for decimal separator.
In MVC3, when I open an Edit view where the decimal values are shown with
#Html.EditorFor(model => model.MyDecimalVal)
the value is shown correctly.
When I enter value with comma, I get error "Value is not a number" and if I enter value with dot, I get no error, but actually no value is saved.
How to handle this situation?
This blog post recommends overriding the default jQuery validate number and range rules in order to enable client-side support of the comma decimal separator.
To fix these problems, we can take the default implementation from the
jquery.validate.js file for the range() and number() functions. We
then create another .js file (say jQueryFixes.js), in which we
override these default functions with ones that contain support for
the comma as a decimal separator. The contents of the file should be
something like this:
$.validator.methods.range = function (value, element, param) {
var globalizedValue = value.replace(",", ".");
return this.optional(element) || (globalizedValue >= param[0] && globalizedValue <= param[1]);
}
$.validator.methods.number = function (value, element) {
return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value);
}
Getting around with this by tweaking your validation logic has been already explained so here is a different approach.
Put the below code inside your web.config file under the <system.web> node if you would like to set the culture to en. It will take care of decimal delimiter:
<globalization culture="en-US" uiCulture="en" />
I am not sure but for DateTime, you are still bound to your server's locale.

Groovy GSP <g:if>

This has been driving me crazy for hours and its probably very obvious to someone ...
Can anyone see why this is printing out even though its reporting as being false?
<g:if test="${className == 'SRep'}">
${className == 'SRep'}
</g:if>
If classname==SRep then its correct. However if classname <> SRep it still prints out false? I don't understand how this can be.
If I use ?showSource=true, the if statement looks like this:
if(true && ("false")) {
printHtmlPart(29)
}
else {
printHtmlPart(30)
}
Anyone see anything obvious?
Thanks
John
your code looks fine but im not sure what 'className' is, perhaps it's not being returned or returning wrong type etc:
i would display the output in the GSP of className just to see what it is
e.g. add this anywhere in your GSP:
${className} //displays the value
you may also want to check the object type, in your case i think it should be string
so check what you have:
${className?.class} //displays the type of object

grails gsp test evaluates to false, but block is still rendered. Why?

I'm baffled with Grails test operator.
This expression:
<g:if test="${!(preferences.displayOption.equals('ANA') || preferences.displayOption.equals('FLOP'))} ">
${!(preferences.displayOption.equals('ANA') || preferences.displayOption.equals('FLOP'))}
</g:if>
prints
false
How can that be? I'm printing the exact same condition I'm testing for!
even though I'm certain the test condition evaluates to 'false' because it prints false in the very next line, the statements inside the g:if are being rendered.
Anu ideas as to what's going on.
I think the problem is the trailing space after the final } in your test. The string "false " evaluates to true, whereas "false" will appropriately interpreted as false by the if tag.
Try removing the extra space after the closing } in the test attribute. You can view the generated groovy source by adding "?showSource=true" to the url in development mode.
The extra space causes it to create a String "false " which evaluates to true.

Resources