Re: loading plain files

Attila Szegedi <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Another thing you can do is to just try Configuration.getTemplate(),  
and if you get a FileNotFoundException, fall back to retrieving a  
classpath resource yourself.
Or, if you're worried of having an exception as part of your normal  
control flow, you can also try first looking up the classpath resource  
with Class.getResource() and if it returns null, only then fall back  
to FreeMarker.

Another advantage of this approach instead of having FreeMarker serve  
static content is that you can return Last-Modified headers in HTTP  
responses (i.e. with the timestamp of the JAR file itself), and  
handling If-Modified-Since request headers, or even returning Expires  
headers with some sane values. That way you can take advantage of HTTP  
clients' caches (if they have any, i.e. if they're regular web  
browsers, then they obviously do) and reduce traffic on your app.

Attila.

On 2008.11.08., at 18:11, Luther Baker wrote:

> That is the answer I was assuming ... but wanted to confirm ...  
> sounds good.
>
> Thanks for the response. Love the library.
>
> -Luther
>
>
>
> On Sat, Nov 8, 2008 at 10:39 AM, Attila Szegedi <[email protected]>  
> wrote:
> There's no way to do it, and I don't think it'd be FreeMarker's
> responsibility to provide such capabilities.
>
> Except there IS of course a way to do it, which would be to write a
> custom TemplateLoader that wraps another loader, and makes sure that
> it edits the text stream of the resource so that anything that
> FreeMarker would recognize as its own markup (which would be "<#",
> "<@", (or "[#", "[@") and "${") gets escaped.
>
> OTOH, if you can be certain that the static files you have don't have
> anything in them that FreeMarker would recognize as its own markup,
> you can just put them in there -- FM will treat each of them as a
> template with single chunk of static text and render them very
> efficiently :-)
>
> Attila.
>
> On 2008.11.08., at 16:32, Luther Baker wrote:
>
> > For simplicity's sake, I would like to use freemarker to load both
> > *.ftl files and plain html/js/css files.
> >
> > IE: I am distributing a library (JAR) and include a few text files
> > in the classpath (src/main/resources per maven). When a request
> > comes into my framework, I modify the URL path and look for a *.ftl
> > file. If I find it, great, if I don't - I would like to look up the
> > RAW file and simply return it via freemarker unprocessed.
> >
> > a) Is that safe to do?
> > b) Is there a way I can tell freemarker 'load these .css and .js
> > files from the special FTL paths" but do not waste your time trying
> > to interpolate them. Just render them as is?
> >
> > Thanks,
> >
> > -Luther





-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.