Re: loading plain files

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Sunday, November 16, 2008, 8:50:40 PM, Luther Baker wrote:

> I ended up simply looking up the file myself as a resource. I really  
> like the header suggestion ... I cache the contents in memory but your
> http header suggestion even helps with the bandwidth as well.
>
> A quick question on using Freemarker again ... I could easily start  
> and end the file with a <#noparse> block. For instance - I want to  
> load JQuery.js. I've no idea what they include in their distros ...  
> but I can easily add the noparse directive to the top and bottom of  
> the files they distribute.
>
> Would that be ok?
>
> Would I be safe in assuming that Freemarker would only read that file
> one time?

Not if you are so unlucky that the original JQuery.js happens to
contain </#noparse>... ;)

> And that for the most part, the noparse directive would
> speed up how it is served everytime?

All this complication is unnecessary. I mean, only the
not-exactly-perfect design of the FreeMarker caching/storage API-s
make it necessary... But we have this:

  http://freemarker.org/docs/api/freemarker/template/Template.html#getPlainTextTemplate(java.lang.String,%20java.lang.String,%20freemarker.template.Configuration)

so, you *can* safely parse "non-template templates" without escaping
tricks and like, only you can't explain to the TemplateCache to use
this method for parsing if the the template name doesn't end with ftl.
Or at least I don't see how could I achieve that now. So maybe we
should add:

  public class TemplateCache {

      ...
 
      protected boolean isTreatedAsTemplate(String name) {
          return true;
      }

      ...

  }

or, maybe even better:

  // A new interface!
  public interface RichTemplateLoader extends TemplateLoader {

      /**
       * @return true, except if the resource has to be treated as
       * static text (i.e., even parts that look like FreeMarker statements
       * will be treated as plain text).
       */
      boolean isTemplate(java.lang.Object templateSource) throws java.io.IOException;

      // Not to mention things like:

      /**
       * @returns -1 if not supported, 0 for disabling caching for
       * this template.
       */
      long expires(java.lang.Object templateSource) throws java.io.IOException;

      /**
       * Returns the custom attributes that will be associated with
       * the Template. Frameworks my use this to associate an
       * output-MIMEType to the template, and like.
       */
      Map customTemplateAttributes(java.lang.Object templateSource) throws java.io.IOException;

      /**
       * @returns for example {@code "x as x?html"}.
       * But it has no tradition in FTL, so maybe it would be too
       * confusing...
       */
      String autoEscaping(java.lang.Object templateSource) throws java.io.IOException;
      
      // etc.
      
  }

  
> I not worried about blinding speed - I just want this to scale  
> predictably/well.
>
> Thanks,
>
> -Luther
>
>
>
> On Nov 10, 2008, at 5:19 AM, Attila Szegedi wrote:
>
>> 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=/
>> _______________________________________________
>> FreeMarker-user mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>
>
> -------------------------------------------------------------------------
> 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=/
> _______________________________________________
> FreeMarker-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user

-- 
Best regards,
 Daniel Dekany


-------------------------------------------------------------------------
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.