1

Say I have a template in either pebble or freemarker (The two engines I am considering using). It might look something like this:

<Node>
    <Element attribute="{{ VAR_A }}"/>
    <Element attribute="{{ VAR_F }}"/>
    <Element attribute="{{ VAR_N }}"/>
</Node>

In my Java code, I will need to provide an object containing these variables, which I believe can just be a map of variable name to object in both engines.

However to generate the data model, I need to know which variables to calculate values for. I have a lot of data. I don't want to calculate all of VAR_A, VAR_B, VAR_C, VAR_D, VAR_E, etc if they aren't going to be used.

Is there a way in either/both of these engines for getting a list of the required variables before executing the template? A way to do lazy evaluation of each attribute value

Edit: I have no idea what the template will look like when I have to evaluate it as it will be defined by the user. Even the variables they are providing will be user defined strings - which is why I need to get (and analyse) them before I can provide the data.

AdamBourke
  • 53
  • 1
  • 9

1 Answers1

0

Each kind of document you generate using Freemarker will have its own template. Each template will have a set if variables it uses. In your Java code you provide a Map that maps variable names to values. If you have a different Java method corresponding to each template, those methods can set up the smallest needed map for that template. That map need not be a HashMap; in theory you could write your own implementation of the Map interface that did lazy evaluation in its get and values (etc) methods. But that is almost certainly more trouble than it is worth. The values in map you provide need not be String objects; Freemarker will use the toString method of each object, which provides a kind of lazy evaluation.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • In this case, I won't know what the template looks like in advance, and they are likely to be different for each user. The variables in the template will be user-defined, so I will need to look each of them up anyway. I can override HashMap, if I need to - but I was hoping there was a way to pre-evaluate the template to get the variable list. – AdamBourke Mar 22 '16 at 10:34
  • That makes no sense. If the tempkates are user defined, the users are programming the system, not you. You do not want to do that. – Raedwald Mar 22 '16 at 11:45
  • The software provides a large amount of customisability for users. The idea is that they can write their own templates, and we will populate with the data in our system. The variables they use will be defined elsewhere in the system. It is very much what we want to do. – AdamBourke Mar 22 '16 at 12:22
  • @AdamBourke http://thedailywtf.com/articles/The_Inner-Platform_Effect https://en.m.wikipedia.org/wiki/Inner-platform_effect – Raedwald Mar 22 '16 at 15:54
  • It's an interesting article. But the wikipedia article points out that it's not wrong in all cases. Although, if you can suggest a better solution, I'll consider it. The goal of using the template engine, is to allow our users to create output files. Primarily in xml, but potentially in other formats as well. each of our users require multiple outputs, in multiple formats often unique to the customer. So they will write their own templates to reflect this. We let them use their own variables so that they don't need intimate knowledge of our java structures to use it. – AdamBourke Mar 22 '16 at 17:03
  • I would also add that, we're not intending to provide java-like functionality. So it's not really an inner platform effect. It's more like a website builder that lets you add javascript snippets to make something look pretty. – AdamBourke Mar 22 '16 at 17:24