TemplateModel _getAsTemplateModel(Environment env) throws TemplateException {
TemplateModel model = target.getAsTemplateModel(env);
Expression sourceExpr = null;
String id = "anonymous_interpreted";
if(model instanceof TemplateSequenceModel)
{
sourceExpr = ((Expression)new DynamicKeyName(target, new NumberLiteral(new Integer(0))).copyLocationFrom(target));
if(((TemplateSequenceModel)model).size() > 1)
{
id = ((Expression)new DynamicKeyName(target, new NumberLiteral(new Integer(1))).copyLocationFrom(target)).getStringValue(env);
}
}
else if (model instanceof TemplateScalarModel)
{
sourceExpr = target;
}
else
{
throw invalidTypeException(model, target, env, "sequence or string");
}
String templateSource = sourceExpr.getStringValue(env);
Template parentTemplate = env.getTemplate();
try
{
Template template = new Template(parentTemplate.getName() + "$" + id, new StringReader(templateSource), parentTemplate.getConfiguration());
template.setLocale(env.getLocale());
return new TemplateProcessorModel(template);
}
catch(IOException e)
{
throw new TemplateException("", e, env);
}
}
Constructs a template on-the-fly and returns it embedded in a
TemplateTransformModel .
The built-in has two arguments:
the arguments passed to the method. It can receive at
least one and at most two arguments, both must evaluate to a scalar.
The first scalar is interpreted as a template source code and a template
is built from it. The second (optional) is used to give the generated
template a name. |