[stringtemplate-interest] Dynamic binding of templates?

Sam Barnett-Cormack s.barnett-cormack at lancaster.ac.uk
Sun Jun 5 04:55:20 PDT 2011


Hi all,

So, I've been away from StringTemplate use for a while, but I'm planning 
a new project using ST, and want to do something that I'm not sure is 
possible (even after a glance over current documentation).

I want to use templates that invoke other templates; no problem. I want 
to allow simple configuration to determine which specific templates to 
use to satisfy interfaces. I want extensions to my software to define 
new interfaces (generally with default implementations). So, in an 
installation of the software, there will be a whole group of interfaces 
(one or more for the core, and zero or more for each extension), and 
there may be several implementations of each interface. I want to be 
able to decide, at run-time, which implementations to use. That much I 
think is straightforwardly possible, except for this:

I want the extension templates to actually be satisfying some abstract 
interface and be called by the core templates. So a core template might 
only know that it's going to invoke 'some item template', and then it 
will invoke a specific template that was not even conceived off at the 
time the core template was written. The immediate idea that occurs to me 
is if it were possible to pass a template reference as a parameter and 
invoke it, but that would be kind of awkward and still hard to use, as 
it would have to be passed down through any external templates (although 
it could be deeper in a data structure, so it's possible - say that the 
core template just receives a list of items, and the template reference 
is a member of the item data structure, and so forth - maybe this could 
be done through invoking a 'getOutput' method on the item that invokes 
its own template, which may then invoke core templates perhaps, and then 
is just used as a string for output). Is anything like this possible?

The other point part of this, in terms of working out how I'll do it, is 
to ask whether it's possible/easy for the extensions to have their own 
template groups that are automatically able to use the templates in the 
core group, even though the core group is determined at runtime, 
possibly from multiple sources. I can see one way of doing it with 
dynamically generating strings that are used to build the groups, and 
using the same components for multiple groups, but that would lead to 
duplication; I suppose it could be done using the same dynamic building, 
but grouping everything in one group, but then I'm not sure about 
verifying that the conceptual groups satisfy their interfaces. In the 
end, at runtime, I envisage having a load of well-defined, in files, 
interfaces, dynamically building the implementation of each interface 
from fragments that come from files, and verifying that they satisfy 
their interfaces, but ending up with them being able to invoke templates 
from other groups.

So, for the bits that are weird, I'm hoping for ideas; for bits that are 
quite straightforward, I'm hoping for pointers - either in replies, or 
via pointers to resources online that will help me see what to do.

Thanks all,

Sam


More information about the stringtemplate-interest mailing list