velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nathan Bubna" <nbu...@gmail.com>
Subject Re: Finding Templates
Date Thu, 06 Apr 2006 15:18:12 GMT
Comments inline...

On 4/6/06, Piyush Purang <ppurang@gmail.com> wrote:
> Hi,
>
> Either I can't understand how velocity finds its templates (file
> resources) or it is very limited in its capabilities.

We have multiple resource loaders available and you can always write
your own.  There is no limit to its capabilities for a well-informed
user.

> I thought if I were to build the latest sources (I picked up the
> sources from http://svn.apache.org/snapshots/jakarta-velocity/jakarta-velocity_20060405104817.tar.gz.)
> and use that jar I could enhance the behaviour.
>
> Ideally I'd like to give an absolute  path to velocity  engine and it
> should be able to find it; if that is too much then a common parent
> folder of all templates should be enough. Neither worked!
>
> If velocity expects all my file-templates to lie in a single folder
> then I have to say this is very very limiting especially when I want
> to categorize my templates through folders.
>
> Examples,
>
> I am trying to access the template 'pgsql.vtl' lying under
> 'D:/netbeans/project/template/sql/'
>
> These attempts don't work:
>
> 1.
> velocityEngine.mergeTemplate(getFile().getAbsolutePath(), CHARSET,
> context, resultStringWriter);

in the latest version, you can now use absolute paths, but *ONLY* if
you configure the FileResourceLoader to search the empty path. 
Absolute paths are not valid by default; that would be a huge security
risk.  This is all discussed in the Bug 17379.

> 2. velocityEngine.setProperty("file.resource.loader.path",
> "D:/netbeans/project/template/");
>
> velocityEngine.mergeTemplate(getFile().getName(), CHARSET, context,
> resultStringWriter);
>
> (NOTE: It isn't D:/netbeans/project/template/sql/ but
> D:/netbeans/project/template/)

if the template is in the D:/netbeans/project/template/sql/pgsql.vtl
and you are asking for pgsql.vtl in the D:/netbeans/project/template
directory, it will not work.  Velocity does not automatically search
all subfolders.  That would be highly irregular and unwise IMO.

If file.resource.loader.path = D:/netbeans/project/template, then you
should be asking for "/sql/pgsql.vtl" instead of just "pgsql.vtl". 
Pretty easy.

If you want a resource loader that will automatically search through
subfolders, then you are free to write your own.  They're not very
difficult if you start with the code of an existing one that is
similar.

> Only the following works:
>
> 3. velocityEngine.setProperty("file.resource.loader.path",
> "D:/netbeans/project/template/sql/");
>
> velocityEngine.mergeTemplate(getFile().getName(), CHARSET, context,
> resultStringWriter);

As it should.

>
> Questions:
>
> I thought http://issues.apache.org/bugzilla/show_bug.cgi?id=17379 was
> fixed and this will allow file-templates defined by absolute paths. Is
> my assumption false?
>
>
> Or am I missing a trick/config option that will help solve my problems?

it is the latter.  you must setProperty("file.resource.loader.path",
"") for absolute paths to work in the latest version.

> Thanks.
>
> Cheers
> Piyush
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>

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


Mime
View raw message