use of freemarker.template.TemplateException in project stanbol by apache.
the class LdRenderer method render.
/**
* Renders a GraphNode with a template located in the templates
* folder of any active bundle
*
* @param node the GraphNode to be rendered
* @param templatePath the freemarker path to the template
* @param out where the result is written to
*/
public void render(GraphNode node, final String templatePath, Writer out) {
//A GraphNode backend could be graph unspecific, so the same engine could be
//reused, possibly being signifantly more performant (caching, etc.)
RDFBackend<RDFTerm> backend = new ClerezzaBackend(node.getGraph());
RDFTerm context = node.getNode();
TemplateEngine<RDFTerm> engine = new TemplateEngine<RDFTerm>(backend);
engine.setTemplateLoader(templateLoader);
try {
engine.processFileTemplate(context, templatePath, null, out);
out.flush();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (TemplateException e) {
throw new RuntimeException(e);
}
}
use of freemarker.template.TemplateException in project engine by craftercms.
the class RenderComponentDirective method executeScripts.
protected Map<String, Object> executeScripts(SiteItem component, Map<String, Object> additionalModel, Environment env) throws TemplateException {
List<String> scriptUrls = scriptResolver.getScriptUrls(component);
if (CollectionUtils.isNotEmpty(scriptUrls)) {
if (logger.isDebugEnabled()) {
logger.debug("Scripts associated to component " + component.getStoreUrl() + ": " + scriptUrls);
}
Map<String, Object> templateModel = new HashMap<>();
Map<String, Object> scriptVariables = createScriptVariables(component, templateModel, additionalModel);
SiteContext siteContext = SiteContext.getCurrent();
if (siteContext != null) {
ScriptFactory scriptFactory = siteContext.getScriptFactory();
if (scriptFactory == null) {
throw new IllegalStateException("No script factory associate to current site context '" + siteContext.getSiteName() + "'");
}
for (String scriptUrl : scriptUrls) {
Script script;
try {
script = scriptFactory.getScript(scriptUrl);
} catch (Exception e) {
throw new TemplateException("Unable to load script at '" + scriptUrl + "'", e, env);
}
executeScript(script, scriptVariables, env);
}
} else {
throw new IllegalStateException("No current site context found");
}
return templateModel;
} else {
return null;
}
}
use of freemarker.template.TemplateException in project engine by craftercms.
the class RenderComponentDirective method execute.
@SuppressWarnings("unchecked")
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException {
TemplateModel componentParam = (TemplateModel) params.get(COMPONENT_PARAM_NAME);
TemplateModel componentPathParam = (TemplateModel) params.get(COMPONENT_PATH_PARAM_NAME);
TemplateModel additionalModelParam = (TemplateModel) params.get(ADDITIONAL_MODEL_PARAM_NAME);
Map<String, Object> additionalModel = null;
SiteItem component;
if (componentParam == null && componentPathParam == null) {
throw new TemplateException("No '" + COMPONENT_PARAM_NAME + "' or '" + COMPONENT_PATH_PARAM_NAME + "' param specified", env);
} else if (componentParam != null) {
component = getComponentFromNode(componentParam, env);
} else {
component = getComponentFromPath(componentPathParam, env);
}
if (additionalModelParam != null) {
additionalModel = unwrap(ADDITIONAL_MODEL_PARAM_NAME, additionalModelParam, Map.class, env);
}
Map<String, Object> templateModel = executeScripts(component, additionalModel, env);
SimpleHash model = getFullModel(component, templateModel, additionalModel);
Template template = getTemplate(component, env);
Writer output = env.getOut();
processComponentTemplate(template, model, output, env);
}
use of freemarker.template.TemplateException in project engine by craftercms.
the class CrafterTemplateExceptionHandler method handleTemplateException.
@Override
public void handleTemplateException(TemplateException te, Environment env, Writer out) throws TemplateException {
if (displayTemplateExceptionsInView) {
String error = ERROR_FORMAT.replace("{errorId}", createErrorId());
error = error.replace("{error}", getExceptionStackTrace(te));
try {
out.write(error);
} catch (IOException e) {
throw new TemplateException("Failed to print error. Cause: " + e, env);
}
}
}
Aggregations