Or condition not working in FTL? - freemarker

I'm trying to put a condition in free marker template but it's not working . here is the condidtion
<#if "${Model.Order.Addresses.DeliveryAddress}" != "TRED" || "${Model.Order.Addresses.DeliveryAddress}" != "TREF">
Please note that it can take some time for the tracking.
</#if>
Is there any syntax issue in this ?
Thanks in advance

There's a logical problem there. Your condition says "the delivery address is not TRED or the delivery address is not TREF", which is true for all delivery addresses. I guess you want either "the delivery address is TRED or the delivery address is TREF", in which case use == instead of !=, or "the delivery address is not TRED and the delivery address is not TREF", in which case use && instead of ||.
As of the syntax, instead of "${Model.Order.Addresses.DeliveryAddress}" != "TRED" you should just write Model.Order.Addresses.DeliveryAddress != "TRED". It will give the same result as far as DeliveryAddress is a string, but is shorter.

Related

Freemarker not entering if/else if values are null

<#if myVar = "test">
A
<#else>
B
<#/if>
If myVar is null / not defined, neither A nor B will be output. This is fixed by adding ! after the variable:
<#if myVar! = "test">
A
<#else>
B
<#/if>
Is this intended? Because if so, it is extremely confusing and I can't imagine any user would expect it to behave this way. It is suggesting that null = "test" is somehow neither true nor false. It seems clear that null = "test" should always be considered false -- what am I missing?
Note: we have configured Freemarker to replace unknown variables with an empty string:
config.setTemplateExceptionHandler((ex, environment, out) ->
{
if (ex instanceof InvalidReferenceException)
{
try
{
out.write("");
}
catch (IOException e)
{
throw new TemplateException("Error while handling template exception.", e, environment);
}
}
else
{
throw ex;
}
});
So this would (at least I thought it would) end up being:
<#if "" = "test">
A
<#else>
B
<#/if>
In which case, I'd expect the else to be entered for sure -- but it is not.
Given this example:
<#assign myVar = null/>
<#if myVar == "test">
A
<#else>
B
</#if>
This throws the following error:
The following has evaluated to null or missing:
==> null
myVar has been evaluated to null or missing -- this seems like the source of my confusion; Freemarker does not differentiate between missing and existing-but-null values, I guess?
It's irrelevant if it's #if/#else or any other directive call, because when an error occurs (any kind of TemplateException, not just "missing value"), the whole statement is skipped. Not the variable resolution, not even the whole expression, but the whole statement. That's what the Manual and the Javadoc says too, like see: https://freemarker.apache.org/docs/pgui_config_errorhandling.html. Therefore, templateExceptionHandler setting is not for providing defaults for missing values. It's for handling situations that are genuinely errors. Like, someone did a mistake, something is not operational.
As of why not take the #else still. When an error occurs while evaluating the condition, the template engine just can't tell which branch would have been taken if the developers (or whoever) did not make a mistake. Picking either branch of the if/else is a gamble. (It's not even a 50-50 gamble, as the positive branch tends to be the right one considering the real world meaning of the output, yet, if anything, people expect the template engine to bet on the else branch.) So, hoping that you will print some error indicator at least, it rather doesn't pick either branch. But it's really automatic, as the whole #if/#elseif/#else thing is one big statement.
<#assign myVar = "test"/>
<#if myVar?? && myVar == "test">
A
<#else>
B
</#if>
Yes it is intended. When a variable is not defined or null, freemarker will raise an error.
It is explained in the FAQ: Why is FreeMarker so picky about null-s and missing variables, and what to do with it?
This is a design choice from the initial author of the framework. If you are not confortable with this choice, you can use the Default value operator (use myVar! instead of myVar), or use another templating framework.
The custom exception handler you added doesn't replace the expression with an empty String, it replaces the full if/else expression.

Laravel: How to check if route contains nothing after a slash

I have a condition to check what URL it is so I can set a proper tab class to be active.
I have code
`{{ Request::is('popular') || Request::is('/') || Request::is('category/*/popular') || Request::is('category/*') ? 'nav-active' : '' }}`
The last condition is wrong, for example when the URL is http://localhost:8000/category/goodwin/new that will still be correct, which I don't want. How can I write a condition to check if there is anything after category but only after one /
P.S. I have other conditions for new and top posts which work correctly
You can use url()->current() and then do string analysis on that. E.g. with preg_match and a regex, or by simply counting the number of slashes.
Example:
preg_match("/(.*)\/category\/(.*)\/(?!popular)(.*)/", url()->current());
The regex (.*)\/category\/(.*)\/(?!popular)(.*) checks whether the URL matches .../category/*/* except where the final * is popular.
That would allow you to do:
{{ (Request::is('popular') ||
Request::is('/') ||
Request::is('category/*/popular') ||
Request::is('category/*')) &&
preg_match("/(.*)\/category\/(.*)\/(?!popular)(.*)/", url()->current()) == 0
? 'nav-active' : '' }}
I would consider moving away from the ternary operator as this is getting quite bulky. You should probably also put this logic in the controller and just pass a variable to the view.

How do you evaluate and compare the value of item.istaxable in Netsuite?

I'm using the Advanced PDF/HTML Templates in Netsuite to create a custom output template. In this Template I want to evaluate an item to see if it is taxable.
NetSuite's Schema Defines a Sales Order with a Sub List Item that has a field .istaxable (source)
Field: istaxable
Type: checkbox
Label: Tax
Required: false
When I try to evaluate an expression such as:
<#if item.istaxable == true>
By printing the template I get the following error.
Left hand operand is a com.netledger.templates.model.StringModel
Right hand operand is a freemarker.template.TemplateBooleanModel$2
When I try to evaluate .istaxable as a String:
<#if item.istaxable == "true">
or
<#if item.istaxable == 'T'>
*EDIT: Updated in response to suggested answer
I am unable to save the template in the editor as it throws an error:
The only legal comparisons are between two numbers, two strings, or
two dates. Left hand operand is a
com.netledger.templates.model.BooleanModel Right hand operand is a
freemarker.template.SimpleScalar
So is item.istaxable a StringModel or a BooleanModel?
Netsuite is notoriously inconsistent with how it treats boolean values, and in fact I have come across cases where the same field is treated differently depending on the transaction (in my case it was the isclosed field). I ended up using the syntax below:
<#if (item.isclosed?is_boolean && item.isclosed) || (item.isclosed?is_string && item.isclosed == 'T')
Have you tried item.istaxable = 'T'? That is what I use in SuiteScript 1.0
<#if item.istaxable>.....
Should function as a true/false evaluation.
If Netsuite has set the boolean_format setting correctly, then the following should work:
<#if "${item.istaxable}" == "T">
If istaxable is a string, then its evaluation produces the Netsuite 'T'. If istaxable is a boolean, then the boolean_format setting is used to convert it to a string; if true maps to 'T' and false maps to 'F', then it produces the same result as the string version.
YMMV - If I remember, I'll try to update this answer.

Validate form input for domains and IPs - ajax

how to validate a form field that gets submitted through ajax (below). It needs to validate against a domain (.com, .net, .se etc.) and an IP address. Basically it has to look for at least one dot and at least two letters after last dot.
Now I only have an empty field validation:
var domain = $("input#domain").val();
if (domain == "") {
$("label#domain_error").show();
$("input#domain").focus();
return false;
}
Thank you!
You probably want to use regular expressions:
For the IP address:
^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$
For the hostname:
^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$
For an example how to use regular expressions in JavaScript, have a look at this site

Ruby email check (RFC 2822)

Does anyone know what the regular expression in Ruby is to verify an email address is in proper RFC 2822 email format?
What I want to do is:
string.match(RFC_2822_REGEX)
where "RFC_2822_REGEX" is the regular expression to verify if my string is in valid RFC 2882 form.
You can use the mail gem to parse any string according to RFC2822 like so:
def valid_email( value )
begin
return false if value == ''
parsed = Mail::Address.new( value )
return parsed.address == value && parsed.local != parsed.address
rescue Mail::Field::ParseError
return false
end
end
This checks if the email is provided, i.e. returns false for an empty address and also checks that the address contains a domain.
http://theshed.hezmatt.org/email-address-validator
Does regex validation based on RFC2822 rules (it's a monster of a regex, too), it can also check that the domain is valid in DNS (has MX or A records), and do a test delivery to validate that the MX for the domain will accept a message for the address given. These last two checks are optional.
Try this:

Resources