checking if all variables are present

View: New views
5 Messages — Rating Filter:   Alert me  

checking if all variables are present

by Captain Cid :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have a template containing some variables to be rendered from context.
Is there way by which i can know if all the variables are present in the context, otherwise throw an error.

Re: checking if all variables are present

by Alexander-129 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Maybe this will help you  This way I always test if variable is not null

.............

The #if directive in Velocity allows for text to be included when the web page is generated, on the conditional that the if statement is true. For example:


#if( $foo )
   <strong>Velocity!</strong>
#end

The variable $foo is evaluated to determine whether it is true, which will happen under one of two circumstances: (i) $foo is a boolean (true/false) which has a true value, or (ii) the value is not null. Remember that the Velocity context only contains Objects, so when we say 'boolean', it will be represented as a Boolean (the class). This is true even for methods that return boolean - the introspection infrastructure will return a Boolean of the same logical value.

............


2009/7/28 Captain Cid <captain_cid@...>

I have a template containing some variables to be rendered from context.
Is there way by which i can know if all the variables are present in the
context, otherwise throw an error.
--
View this message in context: http://www.nabble.com/checking-if-all-variables-are-present-tp24696207p24696207.html
Sent from the Velocity - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@...
For additional commands, e-mail: user-help@...



Re: checking if all variables are present

by apache-10 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

several possibilities:

a) You can use the experimental TemplateTool to programmatically get a list of references to check against the context before rendering:
  http://svn.apache.org/repos/asf/velocity/engine/trunk/experimental/templatetool/TemplateTool.java

b) Use an buffering writer and a ReferenceInsertionEventHandler to stop rendering if something is missing

c) Check with #if($!myvar != null) before using reference content (as in previous answer to this thread).

Hope this helps somehow,
Christoph

Captain Cid wrote:
> I have a template containing some variables to be rendered from context.
> Is there way by which i can know if all the variables are present in the
> context, otherwise throw an error.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@...
For additional commands, e-mail: user-help@...


Re: checking if all variables are present

by Captain Cid :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The scenario for me is an email webservice

It has many templates each having certain variables. Email service picks us template and adds data provided to webservice. So it's kind of generic. I dont know how many variables are there in template. All variables given to web service will be used. But email should not be sent if any data is missing. So how to tackle such situation !

Alexander-129 wrote:
Maybe this will help you [?] This way I always test if variable is not null

http://velocity.apache.org/engine/devel/user-guide.html#Conditionals
.............

The *#if* directive in Velocity allows for text to be included when the web
page is generated, on the conditional that the if statement is true. For
example:


#if( $foo )
   <strong>Velocity!</strong>
#end

The variable *$foo* is evaluated to determine whether it is true, which will
happen under one of two circumstances: (i) *$foo* is a boolean (true/false)
which has a true value, or (ii)* the value is not null*. Remember that the
Velocity context only contains Objects, so when we say 'boolean', it will be
represented as a Boolean (the class). This is true even for methods that
return boolean - the introspection infrastructure will return a Boolean of
the same logical value.

............

2009/7/28 Captain Cid <captain_cid@indiatimes.com>

>
> I have a template containing some variables to be rendered from context.
> Is there way by which i can know if all the variables are present in the
> context, otherwise throw an error.
> --
> View this message in context:
> http://www.nabble.com/checking-if-all-variables-are-present-tp24696207p24696207.html
> Sent from the Velocity - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

Re: checking if all variables are present

by Captain Cid :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks
a) part worked well for me !

apache-10 wrote:
several possibilities:

a) You can use the experimental TemplateTool to programmatically get a list of references to check against the context before rendering:
  http://svn.apache.org/repos/asf/velocity/engine/trunk/experimental/templatetool/TemplateTool.java

b) Use an buffering writer and a ReferenceInsertionEventHandler to stop rendering if something is missing

c) Check with #if($!myvar != null) before using reference content (as in previous answer to this thread).

Hope this helps somehow,
Christoph

Captain Cid wrote:
> I have a template containing some variables to be rendered from context.
> Is there way by which i can know if all the variables are present in the
> context, otherwise throw an error.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org