Resources

Resources in Jtwig are modeled as references. It uses the ResourceReference class to do so. A resource reference is a pair of type and path. For example:

  • file:/tmp/file.twig represents a reference with type file and path /tmp/file.twig
  • classpath:template.twig refers to type classpath and path template.twig.

Reference Type

To represent the type one use a raw string, allowing for custom types to be introduced at configuration time. Each type must have an associated ResourceLoader, allowing one to, given a reference, perform different operations, like:

  • Load reading the reference as an InputStream.
  • Exists check if the given reference exists.
  • URL return the URL representation of the reference, if it exists.
  • Charset return the charset of such reference, if possible.

Types defined in Core

The Jtwig Core default configuration comes with three reference types, namely:

  • file
  • classpath
  • string

Absolute Type vs Relative Type

In Jtwig a reference type can either be relative or absolute, meaning that, relative path calculation is possible or not. For example, file and classpath types are relative types, while string is absolute.

Relative Resource Resolver

For relative reference types only the concept of relative resource resolver exists so that, using a given reference as base path, calculate the path to another reference.

The string reference

The string reference type is a special kind of reference where as path, one can provide, actually, a template definition. For example, the resource reference string:{{ "hello world!" }} defines a resource with content {{ "hello world!" }} which when rendered will produce hello world!.

The any reference type

The resource resolution in Jtwig also allows one to refer to a given resource without specifying the type, as such, the defined type will be any, meaning a best effort approach will be used to load/resolve the given resource. For example to load the reference /tmp/template.twig (without the type) Jtwig will iterate over the list of ResourceLoader and the first one finding the resource will be used. For this is important the ordering defined during configuration. As default the ordering is as such:

  • file with base directory as current working directory
  • classpath using jtwig-core ClassLoader

Note that, the string type is ignored for the purpose of resource lookup.