You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Geir Magnusson Jr." <ge...@optonline.net> on 2001/06/13 01:21:54 UTC

Re: inconsistencies between #include and #parse (?)

Simon Christian wrote:
> 
> Hi folks,
> 
> I was wanting to use the #include directive in a template for some
> static content, as a test really. I'm using a template manager tool to
> construct the paths to templates on a per-servlet basis which works fine
> for in #parse directives, but if I try using #include it brings up an
> error.
> 
> The templateTool.getTemplate method basically looks for the best match
> template for the current user and returns the path to that file. In this
> test there is only the one set of default templates. Example
> input/output below
> 
> *** input ***
> $templateTool.getTemplate("doctype.txt")
> #include( $templateTool.getTemplate("doctype.txt") )
> #parse( $templateTool.getTemplate("doctype.txt") )
> 
> *** output ***
> home/simon/myproject/templates/doctype.txt
> <!-- include error : error with arg 0 please see log. see error log -->
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
> "http://www.w3.org/TR/html4/loose.dtd">
> 
> (whitespace added for clarity)
> 
> The velocity.log file reports that the
> home/simon/myproject/templates/doctype.txt cannot be found..
> 
> In this case the file.resource.loader.path in the velocity.properties is
> set to /
> 
> Any hints as to why this might happen? How does the ResourceLoader
> discriminate between #include and #parse directives?

It doesn't.  Must be a bug.  I assume your template tool returns a
String?

geir
 
> tia,
> 
> - simon

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Re: String substitution & Date Formats

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Christoph Reck wrote:

> Geir, I can send you these tools as they are now (they are well javadoc'ed).
> As a donation to the ASF, I can put the ASF header into them. This action
> is pending in my mail queue, Geir shall I send these context tools
> for the apache.commons to you now?

That's great!

I'll be happy to accept.  Just so it's clear, and I know you know this -
you will be assigning ownership of copyright to the ASF to do that. (The
copyright of the whole Apache/Jakarta/XML/etc codebase is owned by the
ASF.)

I will make a space in the contrib area for them for now - when we get
enough, then we decide where they will live as a project.  I think
Jakarta Commons is a good candidate if we can make a nice little bundle,
but Jon also mentioned Turbine.

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Re: String substitution & Date Formats

Posted by Christoph Reck <Ch...@dlr.de>.
I've done this using ORO and VMs:

In my per request context (since it seemed that I read somewhere
that ORO is not threadsafe, otherwise it could be instantiated
once in a chained global context):

  context.put("Regexp", new org.apache.oro.text.perl.Perl5Util() );


And within my globalMacros.vm:

## ------------------------------------------------------------------------
## Encodes a text string into HTML
## ------------------------------------------------------------------------
#macro( encodeToHTML $text )
#set( $l_line = $Regexp.substitute('s/&/&amp;/g', $text) )
#set( $l_line = $Regexp.substitute('s/</&lt;/g', $l_line) )
#set( $l_line = $Regexp.substitute('s/  /&nbsp;&nbsp;/g', $l_line) )
#set( $l_line = $Regexp.substitute('s/%0A/<br>/g', $l_line) )
$l_line##
#end


If you have a high load server, the VM solution is not recommendable,
then you should go a context tool approach as jon said about the
anakia/Escape.java tool.


for the Date tool I've created an IsoDateTool, with convenient date
parsing and conversion methods. It also has a constructor to take
other date formatting strings. In my application I initialize a
root context with code in a init.vm template, which is then chained on every 
request (wich takes another per-request initialization template - for
request-dependant and thread-unsafe stuff). These special templates
have a class tool to do whacky suff. I use:
#set( $IsoDate = $Class.newInstance("de.dlr.dfd.naomi.util.IsoDateTool") )


Geir, I can send you these tools as they are now (they are well javadoc'ed).
As a donation to the ASF, I can put the ASF header into them. This action
is pending in my mail queue, Geir shall I send these context tools
for the apache.commons to you now?

Cheers, 
Christoph

Tim Colson wrote:
> 
> Two questions - apologies if they're simplistic!
> 
> 1) What's a clean way to do string substitutions?
>   Ex. Replace all newlines with <BR>, or replace "(bullet)" with "<LI>"
> 
>   Assuming our template designers are Regular Expression saavy, could I
> populate the context with a utility class that knows about Jakarta-Oro
> perhaps?
> 
>   #foreach ($item in $list_of_items)
>    $util.RegexReplace($item.InStringy, "\n", "<BR>")
>   #end
> 
>  Am I at least barking up the right tree?
> 
> 2) How do I make it easy for a Designer to pretty-print a Date object?
>   a) In a format of their choosing ("DD-MMM-YYYY")
>   b) In a locale-specific preferred format ("06/12/01" for US, "12/06/01"
> for UK)
> 
> Thanks!
> Timothy

Re: String substitution & Date Formats

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Keith Vance wrote:
> 
> I just wanted to chime in and say that having lots of neat tools would be
> cool, however I really hope that the Velocity scripting language stays as
> simple as it is now.

It will.  There are many of us here who believe that simplicity is
perfection.  Or something along those lines.

This is the beauty of the model : we can accumulate a large set of tools
for people to use, but they aren't part of Velocity, and have nothing to
do with the scripting language or the included core.

Further, I think that they would be useful for others, even if they are
using other java solutions...


> 
> I think as Velocity gets more and more popular more and more
> people who have been using PHP, Cold Fusion, or ASP will start using it.
> Then the mailing list will start seeing more and more posts asking for the
> Velocity project to support more and more functionality in the scripting
> language. The beauty of Velocity is it's simplicity, putting the control
> in Java. I love PHP and have used it for years, but I really enjoy working
> with Velocity. I feel that a lot of people who are coming from a PHP type
> scripting language will have a hard time dealing with Java at first, but
> will soon see the value.


And if a good toolset if available (like in Commons or from Turbine)
then it's even easier for the php-ers to make the transition.

geir
 
> K E I T H  V A N C E
> Software Engineer
> n-Link Corporation
> 
> On Tue, 12 Jun 2001, Geir Magnusson Jr. wrote:
> 
> > Tim Colson wrote:
> > >
> > > Geir / others -
> > >
> > > > > 1) What's a clean way to do string substitutions?
> > > > >   Ex. Replace all newlines with <BR>, or replace "(bullet)" with "<LI>"
> > >
> > > Geir wrote:
> > > > In Velocity? You mean in-template or post process?
> > >
> > > Yep - in template.
> > >
> > > If a database field has newlines in it, they need replaced with <BR> to
> > > achieve the effect in HTML, or perhaps the designer fancies newlines +
> > > "==>", or "<LI>".
> > >
> > > This happens quite a bit for "description" varchars/CLOB's of 3-5K in size.
> > > I don't want to store markup in the DB - and I want to let the designer do
> > > as they please. :-)
> > >
> > > > >    $util.RegexReplace($item.InStringy, "\n", "<BR>")
> > > > Yes, you could do that.
> > >
> > > Alrighty then - thanks. :-)
> > >
> > > > > 2) How do I make it easy for a Designer to pretty-print a Date object?
> > > > Drop a tool into the context...
> > >
> > > Both of these requests arise from fairly common tasks. Gasp, dare I mention
> > > it?  When I used to do Cold Fusion stuff, they had quite a bucket of handy
> > > tools for stuff like this.
> > >
> > > Any suggestions on where to look for pre-built java tools for this common
> > > stuff?
> >
> > This is a good subject.  I think we need to start collecting things like
> > this.  We can start holding them here until we get a critical mass, but
> > I think this would make a great commons project.
> >
> > geir
> >
> >

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

RE: String substitution & Date Formats

Posted by Tim Colson <tc...@cisco.com>.
Jon et. al -
> Yes, but making these tools part of Velocity itself is not our goal...
Right, sorry - I didn't mean to suggest integrated-packaging of the widgets
+ Velocity.

> JSP itself doesn't include taglibs. That is what the Jakarta
> Taglibs project is for.
Ayep, good example. So stashing useful beans perhaps in the contrib
area just until they could move to a more appropriate Commons
area seems like a good way to keep similar separation.


BTW - thanks to all who've replied - especially Christoph!
I look forward to seeing/using the code for the Date tool.  :-)


Cheers,
Timo





Re: String substitution & Date Formats

Posted by Jon Stevens <jo...@latchkey.com>.
on 6/13/01 2:32 PM, "Tim Colson" <tc...@cisco.com> wrote:

> The addition of JavaBean utility widgets doesn't add Velocity Directives.
> They simply provide context utilities which enhance Velocity template usage.
> 
> Cheers,
> Timothy

Yes, but making these tools part of Velocity itself is not our goal. You get
into larger issues about the scope of the beans within the Context that we
really don't want to get into. That is something that your app framework
should deal with.

:-)

A perfect example:

JSP itself doesn't include taglibs. That is what the Jakarta Taglibs project
is for.

-jon

-- 
"Open source is not available to commercial companies."
            -Steve Ballmer, CEO Microsoft
<http://www.suntimes.com/output/tech/cst-fin-micro01.html>


RE: String substitution & Date Formats

Posted by Tim Colson <tc...@cisco.com>.
Keith -

> I just wanted to chime in and say that having lots of neat tools would be
> cool, however I really hope that the Velocity scripting language stays as
> simple as it is now.

Amen. I agree completely. :-)

> Then the mailing list will start seeing more and more posts asking for the
> Velocity project to support more and more functionality in the scripting
> language.

The addition of JavaBean utility widgets doesn't add Velocity Directives.
They simply provide context utilities which enhance Velocity template usage.


Cheers,
Timothy




Re: String substitution & Date Formats

Posted by Keith Vance <kv...@n-link.net>.
I just wanted to chime in and say that having lots of neat tools would be
cool, however I really hope that the Velocity scripting language stays as
simple as it is now.

I think as Velocity gets more and more popular more and more
people who have been using PHP, Cold Fusion, or ASP will start using it.
Then the mailing list will start seeing more and more posts asking for the
Velocity project to support more and more functionality in the scripting
language. The beauty of Velocity is it's simplicity, putting the control
in Java. I love PHP and have used it for years, but I really enjoy working
with Velocity. I feel that a lot of people who are coming from a PHP type
scripting language will have a hard time dealing with Java at first, but
will soon see the value.


K E I T H  V A N C E
Software Engineer
n-Link Corporation

On Tue, 12 Jun 2001, Geir Magnusson Jr. wrote:

> Tim Colson wrote:
> >
> > Geir / others -
> >
> > > > 1) What's a clean way to do string substitutions?
> > > >   Ex. Replace all newlines with <BR>, or replace "(bullet)" with "<LI>"
> >
> > Geir wrote:
> > > In Velocity? You mean in-template or post process?
> >
> > Yep - in template.
> >
> > If a database field has newlines in it, they need replaced with <BR> to
> > achieve the effect in HTML, or perhaps the designer fancies newlines +
> > "==>", or "<LI>".
> >
> > This happens quite a bit for "description" varchars/CLOB's of 3-5K in size.
> > I don't want to store markup in the DB - and I want to let the designer do
> > as they please. :-)
> >
> > > >    $util.RegexReplace($item.InStringy, "\n", "<BR>")
> > > Yes, you could do that.
> >
> > Alrighty then - thanks. :-)
> >
> > > > 2) How do I make it easy for a Designer to pretty-print a Date object?
> > > Drop a tool into the context...
> >
> > Both of these requests arise from fairly common tasks. Gasp, dare I mention
> > it?  When I used to do Cold Fusion stuff, they had quite a bucket of handy
> > tools for stuff like this.
> >
> > Any suggestions on where to look for pre-built java tools for this common
> > stuff?
>
> This is a good subject.  I think we need to start collecting things like
> this.  We can start holding them here until we get a critical mass, but
> I think this would make a great commons project.
>
> geir
>
>


Re: String substitution & Date Formats

Posted by Jon Stevens <jo...@latchkey.com>.
on 6/12/01 6:52 PM, "Tim Colson" <tc...@cisco.com> wrote:

> Jon - how difficult/worthwhile/futile do you think it'd be to actually try?
> 
> Thanks gents,
> Timo

Well, I would have said that it was easy, but now I look at the code and the
people who implemented it made all sorts of silly dependencies on JSP! Those
bastards! :-) :-)

You might want to subscribe to the Jakarta Taglibs mailing list and try to
get them to remove the JSP dependencies...or just do it yourself and submit
a patch...you will have to re-implement their code anyway...

-jon


RE: String substitution & Date Formats

Posted by Tim Colson <tc...@cisco.com>.
Geir -

> > Any suggestions on where to look for pre-built java tools for
> this common stuff?
>
> This is a good subject.  I think we need to start collecting things like
> this.  We can start holding them here until we get a critical mass, but
> I think this would make a great commons project.

Heh heh - I looked there first, was hoping I could be lazy and find
ready-made widgets. ;-)

Jon wrote:
> What it comes down to is that these tools are really just JavaBean's...and
> if you get into more detail, you could probably use the code behind the
> Jakarta taglibs project in order to fulfill your needs. :-)

Yep - I was kinda figuring they smelled like Beans/Taglibs... maybe
stealing...err "repurposing" taglib code wouldn't be such a bad idea
actually. I'm all for sharing. Did I mention I was lazy?

Jon - how difficult/worthwhile/futile do you think it'd be to actually try?

Thanks gents,
Timo




Re: String substitution & Date Formats

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Tim Colson wrote:
> 
> Geir / others -
> 
> > > 1) What's a clean way to do string substitutions?
> > >   Ex. Replace all newlines with <BR>, or replace "(bullet)" with "<LI>"
> 
> Geir wrote:
> > In Velocity? You mean in-template or post process?
> 
> Yep - in template.
> 
> If a database field has newlines in it, they need replaced with <BR> to
> achieve the effect in HTML, or perhaps the designer fancies newlines +
> "==>", or "<LI>".
> 
> This happens quite a bit for "description" varchars/CLOB's of 3-5K in size.
> I don't want to store markup in the DB - and I want to let the designer do
> as they please. :-)
> 
> > >    $util.RegexReplace($item.InStringy, "\n", "<BR>")
> > Yes, you could do that.
> 
> Alrighty then - thanks. :-)
> 
> > > 2) How do I make it easy for a Designer to pretty-print a Date object?
> > Drop a tool into the context...
> 
> Both of these requests arise from fairly common tasks. Gasp, dare I mention
> it?  When I used to do Cold Fusion stuff, they had quite a bucket of handy
> tools for stuff like this.
> 
> Any suggestions on where to look for pre-built java tools for this common
> stuff?

This is a good subject.  I think we need to start collecting things like
this.  We can start holding them here until we get a critical mass, but
I think this would make a great commons project.

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Re: String substitution & Date Formats

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Jon Stevens wrote:

> What it comes down to is that these tools are really just JavaBean's...and
> if you get into more detail, you could probably use the code behind the
> Jakarta taglibs project in order to fulfill your needs. :-)

That's the ticket - lets hijack all the  taglibs code.  Of course, we
would need to find an environmentally friendly way of disposing all of
those pointy brackets...

Maybe they could be recycled into bitshift operators...

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Re: String substitution & Date Formats

Posted by Jon Stevens <jo...@latchkey.com>.
on 6/12/01 5:37 PM, "Tim Colson" <tc...@cisco.com> wrote:

> Gasp, dare I mention
> it?  When I used to do Cold Fusion stuff, they had quite a bucket of handy
> tools for stuff like this.
> 
> Any suggestions on where to look for pre-built java tools for this common
> stuff?

Right...we are keeping this out of Velocity on purpose. The idea is to put
regular "tools" either into Turbine (my preference) or start another sub
project/cvs for this...something like jakarta-velocity-tools

What it comes down to is that these tools are really just JavaBean's...and
if you get into more detail, you could probably use the code behind the
Jakarta taglibs project in order to fulfill your needs. :-)

-jon

-- 
"Open source is not available to commercial companies."
            -Steve Ballmer, CEO Microsoft
<http://www.suntimes.com/output/tech/cst-fin-micro01.html>


RE: String substitution & Date Formats

Posted by Tim Colson <tc...@cisco.com>.
Geir / others -

> > 1) What's a clean way to do string substitutions?
> >   Ex. Replace all newlines with <BR>, or replace "(bullet)" with "<LI>"

Geir wrote:
> In Velocity? You mean in-template or post process?

Yep - in template.

If a database field has newlines in it, they need replaced with <BR> to
achieve the effect in HTML, or perhaps the designer fancies newlines +
"==>", or "<LI>".

This happens quite a bit for "description" varchars/CLOB's of 3-5K in size.
I don't want to store markup in the DB - and I want to let the designer do
as they please. :-)


> >    $util.RegexReplace($item.InStringy, "\n", "<BR>")
> Yes, you could do that.

Alrighty then - thanks. :-)

> > 2) How do I make it easy for a Designer to pretty-print a Date object?
> Drop a tool into the context...

Both of these requests arise from fairly common tasks. Gasp, dare I mention
it?  When I used to do Cold Fusion stuff, they had quite a bucket of handy
tools for stuff like this.

Any suggestions on where to look for pre-built java tools for this common
stuff?


Thanks again!
Tim


Re: String substitution & Date Formats

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Tim Colson wrote:
> 
> Two questions - apologies if they're simplistic!
> 
> 1) What's a clean way to do string substitutions?
>   Ex. Replace all newlines with <BR>, or replace "(bullet)" with "<LI>"

In Velocity? You mean in-template or post process?  I would resort to
Oro or something as a post step.  The problem is that if HTML, then the
\n happens because of the way people like to lay out the VTL. 
Substituting <br> for any newline will most likely lead to problems.

>   Assuming our template designers are Regular Expression saavy, could I
> populate the context with a utility class that knows about Jakarta-Oro
> perhaps?
> 
>   #foreach ($item in $list_of_items)
>    $util.RegexReplace($item.InStringy, "\n", "<BR>")
>   #end
> 
>  Am I at least barking up the right tree?

Yes, you could do that.
 
> 2) How do I make it easy for a Designer to pretty-print a Date object?
>   a) In a format of their choosing ("DD-MMM-YYYY")
>   b) In a locale-specific preferred format ("06/12/01" for US, "12/06/01"
> for UK)

Drop a tool into the context...

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

String substitution & Date Formats

Posted by Tim Colson <tc...@cisco.com>.
Two questions - apologies if they're simplistic!


1) What's a clean way to do string substitutions?
  Ex. Replace all newlines with <BR>, or replace "(bullet)" with "<LI>"

  Assuming our template designers are Regular Expression saavy, could I
populate the context with a utility class that knows about Jakarta-Oro
perhaps?

  #foreach ($item in $list_of_items)
   $util.RegexReplace($item.InStringy, "\n", "<BR>")
  #end

 Am I at least barking up the right tree?


2) How do I make it easy for a Designer to pretty-print a Date object?
  a) In a format of their choosing ("DD-MMM-YYYY")
  b) In a locale-specific preferred format ("06/12/01" for US, "12/06/01"
for UK)




Thanks!
Timothy