problem with elseif in velocity - velocity

I have this velocity template. It works fine in one case, but doesn't in another. If I remove the elseif it works fine. Has anyone encountered this?
#if (some condition)
## Do something
#elseif
## Do something else
#end

I don't know Velocity, but normally elseif is used with a second condition.
else seems to be what you need.
#if (some condition)
## Do something
#else
## Do something else
#end

If you are using #elseif statement then you should make sure that you are checking some condition inside that #elseif statement.
Eg :-
#if (some condition)
## Do something
#elseif (some condition)
## Do something
#else
## Do something else
#end

You can refer to this:
Apache Velocity User Guide
Basically it has to go according to one of the cases below:
#if ( condition )
##do something
#end
#if ( condition )
##do something
#else
##do something
#end
#if ( condition )
##do something
#elseif ( condition )
##do something
#else
##do something
#end
(The if-elseif-....-else ladder length can be long)

Related

Multiple #if statements in Apache Velocity

I want to write the following if - else logic in Velocity
If $var1 == NONE
( If $subvar1 != 'null'
return True
else
return Failed_Sub1)
Else
If $subvar2 != 'null'
return True
else
return Failed_Sub2
So basically $subvar2 is only evaluated if $var != NONE and $subvar1 is only evaluated if $var == NONE
I tried something like
#if($var1 != 'NONE')
#if($subvar2 != 'null')True
#{else}Failed_Sub2
#end
#else
#if($subvar1 != 'null')True
#{else}Failed_Sub1
#end
#end
But its returning nothing to me. What am I doing wrong?
Do you want to avoid null values or strings containing 'null'?
In velocity, you can check for nulls using any non assigned reference, for instance $null :
#if($var1 == $null)
...
Otherwise than that, your code looks fine and nested #if statements are definitely possible.
Here's the relevant documentation.

How to test if defined macro is empty at compile time (Cbjective-C / c)?

Is it possible to do check if I've got an empty define? IS_EMPTY_OR_UNDEFINED is a fictive macro I just came up with.
#define constantA 0
#define constantB 1
#define constantC null
#define constantF ""
#define constantH
#if IS_EMPTY_OR_UNDEFINED(constantA)
# error constantA is defined 0 and the above if should not be true - this line should not run
#endif
#if IS_EMPTY_OR_UNDEFINED(constantB)
# error constantB is defined 1 and the above if should not be true - this line should not run
#endif
#if IS_EMPTY_OR_UNDEFINED(constantC)
# error constantC is defined null and the above if should not be true - this line should not run
#endif
#if IS_EMPTY_OR_UNDEFINED(constantF)
# error constantF is defined "" and the above if should not be true - this line should not run
#endif
#if ! IS_EMPTY_OR_UNDEFINED(constantH)
# error constantH is defined empty and the above if should not be true - this line should not run
#endif
#if defined(undefinedConstant) && ! IS_EMPTY_OR_UNDEFINED(undefinedConstant)
# error undefinedConstant is not defined and the above if should not be true - this line should not run
#endif
Checking if an expression is empty can be done (modulo some really exotic border case) but the technique is somewhat complicated. P99 has a macro that does this and which you could be using as
#if !defined(constantA) || P99_IS_EMPTY(constantA)
...
#endif
Combining this in one single macro is not allowed by the C standard. AS of C11 6.10.1 p4, the token defined is not allowed inside macros or other expressions.
You can use #ifdef MACRO or #if defined(MACRO) to test if macros are defined at all. You can also compare them, although only against integers:
#if MACRO > 5
etc. Maybe this helps you?
Edit: Although I don't know how you would check if a definition evaluates to "empty", or if this is possible.

In Objective-C, how to test against macro definition value?

I'd like to use
#define IS_APP_FULL_VERSION NO
and the code
isAppFullVersion = IS_APP_FULL_VERSION;
to set instance variable. But is there a way to also do
#if IS_APP_FULL_VERSION == "NO"
// add some methods here
#endif
but it would give a compile error, and so is #if (IS_APP_FULL_VERSION == "NO"). Is there a way to check it against YES and NO? (check against the substitution value)
Update: it seems like > and == 0 is allowed, such as
#if IS_APP_FULL_VERSION == 0
so we can actually use 0 and 1 for false and true, and use == 1 to test, but it will be better if YES and NO can be used.
Update 2:
One possible solution turns out to be:
#define IS_APP_FULL_VERSION 1
#if IS_APP_FULL_VERSION
// add some methods here
#endif
isAppFullVersion = IS_APP_FULL_VERSION;
will all work, and we can just change 1 to 0 to toggle the code.
Your basic problem is that the macro processor #if statement only has one data type -- integer. All #if expressions are evaluated in integer, with undefined symbols being replaced with zero. String expressions (including comparisons) cannot be evaluated.
Actually, normally, I would just use a flag macro rather than a value one, since there's only two cases here:
#define APP_IS_FULL_VERSION
Either define or don't define that macro. Then, if you later wanted to include/exclude code based on that, you can use:
#ifdef APP_IS_FULL_VERSION
// Do something
#endif
(or #ifndef for the opposite case, obviously).
For the code segment, it's a simple matter of using that to select the correct code:
#ifdef APP_IS_FULL_VERSION
isAppFullVersion = YES;
#else
isAppFullVersion = NO;
#endif
I think, you're some confused comparison MACRO.
#define format is following it.
#define MACRO_NAME VALUE (VALUE is blank spaces are also fine. MACRO_NAME need is.)
if you want comparison compile-time following like it. this is comparison mean only defined vs no defined. not YES vs NO
#define IS_APP_FULL_VERSION 1
#if defined IS_APP_FULL_VERSION
NSLog(#"full-verison");
#endif
#define IS_APP_FULL_VERSION blahblah
#if defined IS_APP_FULL_VERSION
NSLog(#"full-verison");
#endif
If you want to build separate versions of. refer a following like it.
#define IS_APP_FREE_VERSION
#if defined IS_APP_FREE_VERSION
NSLog(#"free-version");
#endif
#define IS_APP_FULL_VERSION
#if defined IS_APP_FULL_VERISON
NSLog(#"full-version");
#endif
if you want comparison runtime following like it. this is comparsion normally.
#define IS_APP_FULL_VERSION 1
if(IS_APP_FULL_VERSION)
{
NSLog(#"full-verison");
}
#define IS_APP_FULL_VERSION 0
if(!IS_APP_FULL_VERSION)
{
NSLog(#"no full-version");
}

Checking if it is equal, Normal int and #define macro. but it doesn't work

I have an iVar named,
int DATA_IN_TRANSIT;
and I have defined several macros, e.g.
#define PLACES 0;
When I do something like the following,
if(DATA_IN_TRANSIT == PLACES)
{
NSLog(#"Make LLVM Dance!");
}
I get a compiler error (expression expected) in the line if(DATA_IN_TRANSIT == PLACES)
I don't know why it's giving me an error? Am I doing something naive?
#define PLACES 0
but without ';'
otherwise you'll get
if(DATA_IN_TRANSIT == 0;)
{
NSLog(#"Make LLVM Dance!");
}

conditional operator in Velocity

Is there a way to do ternary operators in Velocity?
This is what I'd like to do:
#set ($name = ($args.get(0) == "") ? "default" : $args.get(0))
Instead of chunky if-else
#if ($args.get(0) == "")
#set ($name = "default")
#else
#set ($name = $args.get(0))
#end
Any ideas?
From experience and reading the VTL Reference there is no way to do this. If you had lots of assignments like this maybe you could look at defining your own velocimacro to try and avoid repeating the if else.
For example, if the macro only prints a single string you could do the following:
#set ($name = "#condOpt($args.get(0), "default")")
The double quotes around the macro call are important as that means the RHS of the #set is parsed.
I ended up doing as you said, Mark:
#macro(condOp $check, $default)
#if ($check == "")
$default
#else
$check
#end
#end
And then I can call it like so:
#set ($name = "#condOp($args.get(0), 'default')")
For the record, with Velocity 2.1+, you can provide alternate reference values:
${args[0]|'default'}