Conditional #parse

2008-12-29 Thread Erik Hatcher

Hi,

I'd like to have some construct like this in a template:

  #if(templateExists($template))
#parse($template)
  #end

Is there a canonical/approved way of doing this?   I've put the  
VelocityEngine into the context and tried:


  #if($engine.resourceExists($template))
...
  #end

But that still throws an exception when $template does not exist.  I  
want to avoid the exception and simply skip #parse'ing if a template  
does not exist


Thanks,
Erik


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



Re: Conditional #parse

2008-12-29 Thread Nathan Bubna
On Mon, Dec 29, 2008 at 8:27 PM, Erik Hatcher
e...@ehatchersolutions.com wrote:
 Thanks, good pointers!IncludeNotFound looks like it'd do the trick, but
 for every #parse, not just the one I want to make conditional.

Yeah, a tool approach would probably better fit your use case.  It
could be pretty simple:

public class IncludeTool {
  private VelocityEngine engine;
  public void setVelocityEngine(VelocityEngine engine) {
this.engine = engine;
  }
  public Boolean exists(String name) {
if (engine == null) {
  return null;
}
try {
  return engine.resourceExists(name);
} catch (ResourceNotFoundException rnfe) {
  return false;
}
  }
}

Of course, if you're using VelocityTools, you could even have this
automatically configured and available in all your templates (for
Tools 1.x it'd take a little refactoring).

 Why is it that $engine.resourceExists($template) throws an exception rather
 than returning false?

I'm not really sure.  That suprises me and seems wrong.  If you would
open a JIRA issue for it, i'd try to look into it this week.

Erik



 On Dec 29, 2008, at 3:22 PM, Nathan Bubna wrote:

 There's no canonical way of doing this within a template yet, but
 there is the IncludeEventHandler which was added in Velocity 1.5.
 There is even an IncludeNotFound implementation provided.  You may be
 able to set up a simpler, exception free solution using those.
 Alternately, you could pretty easily create a VelocityTool that did
 this.

 On Mon, Dec 29, 2008 at 4:10 AM, Erik Hatcher
 e...@ehatchersolutions.com wrote:

 Hi,

 I'd like to have some construct like this in a template:

 #if(templateExists($template))
  #parse($template)
 #end

 Is there a canonical/approved way of doing this?   I've put the
 VelocityEngine into the context and tried:

 #if($engine.resourceExists($template))
  ...
 #end

 But that still throws an exception when $template does not exist.  I want
 to
 avoid the exception and simply skip #parse'ing if a template does not
 exist

 Thanks,
  Erik


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



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


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



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