/** * Does what the <code>#import</code> directive does, but with an already loaded template. * * @param loadedTemplate * The template to import. Note that it does <em>not</em> need to be a template returned by * {@link #getTemplateForImporting(String name)}. Not {@code null}. * @param targetNsVarName * The name of the FTL variable that will store the namespace. If {@code null}, the namespace * won't be stored in a variable (but it's still returned). * * @return The namespace of the imported template, already initialized. * * @see #getTemplateForImporting(String name) * @see #importLib(Template includedTemplate, String namespaceVarName) */ public Namespace importLib(Template loadedTemplate, String targetNsVarName) throws IOException, TemplateException { return importLib(null, loadedTemplate, targetNsVarName); }
/** * Like {@link #importLib(String, String)}, but you can specify if you want a * {@linkplain #setLazyImports(boolean) lazy import} or not. * * @return Not {@code null}. This is possibly a lazily self-initializing namespace, which mean that it will only try * to get and process the imported template when you access its content. * * @since 2.3.25 */ public Namespace importLib(String templateName, String targetNsVarName, boolean lazy) throws IOException, TemplateException { return lazy ? importLib(templateName, null, targetNsVarName) : importLib(null, getTemplateForImporting(templateName), targetNsVarName); }
/** * Emulates <code>import</code> directive, except that <code>templateName</code> must be template root relative. * * <p> * It's the same as <code>importLib(getTemplateForImporting(templateName), namespace)</code>. But, you may want to * separately call these two methods, so you can determine the source of exceptions more precisely, and thus achieve * more intelligent error handling. * * <p> * If it will be a lazy or an eager import is decided by the value of {@link #getLazyImports()}. You * can also directly control that aspect by using {@link #importLib(String, String, boolean)} instead. * * @return Not {@code null}. This is possibly a lazily self-initializing namespace, which means that it will only * try to get and process the imported template when you access its content. * * @see #getTemplateForImporting(String templateName) * @see #importLib(Template includedTemplate, String namespaceVarName) * @see #importLib(String, String, boolean) */ public Namespace importLib(String templateName, String targetNsVarName) throws IOException, TemplateException { return importLib(templateName, targetNsVarName, getLazyImports()); }
private void doAutoImports(Environment env, Template t) throws IOException, TemplateException { Map<String, String> envAutoImports = env.getAutoImportsWithoutFallback(); Map<String, String> tAutoImports = t.getAutoImportsWithoutFallback(); boolean lazyAutoImports = env.getLazyAutoImports() != null ? env.getLazyAutoImports().booleanValue() : env.getLazyImports(); for (Map.Entry<String, String> autoImport : getAutoImportsWithoutFallback().entrySet()) { String nsVarName = autoImport.getKey(); if ((tAutoImports == null || !tAutoImports.containsKey(nsVarName)) && (envAutoImports == null || !envAutoImports.containsKey(nsVarName))) { env.importLib(autoImport.getValue(), nsVarName, lazyAutoImports); } } if (tAutoImports != null) { for (Map.Entry<String, String> autoImport : tAutoImports.entrySet()) { String nsVarName = autoImport.getKey(); if (envAutoImports == null || !envAutoImports.containsKey(nsVarName)) { env.importLib(autoImport.getValue(), nsVarName, lazyAutoImports); } } } if (envAutoImports != null) { for (Map.Entry<String, String> autoImport : envAutoImports.entrySet()) { String nsVarName = autoImport.getKey(); env.importLib(autoImport.getValue(), nsVarName, lazyAutoImports); } } }
public Object exec(List args) throws TemplateModelException { if (!args.isEmpty()) { throw new TemplateModelException("This method supports no parameters."); } try { return env.importLib(template, null); } catch (IOException e) { throw new _TemplateModelException(e, "Failed to import loaded template; see cause exception"); } catch (TemplateException e) { throw new _TemplateModelException(e, "Failed to import loaded template; see cause exception"); } } });
TemplateSequenceModel evaluateStringsToNamespaces(Environment env) throws TemplateException { TemplateSequenceModel val = (TemplateSequenceModel) eval(env); SimpleSequence result = new SimpleSequence(val.size()); for (int i = 0; i < items.size(); i++) { Object itemExpr = items.get(i); if (itemExpr instanceof StringLiteral) { String s = ((StringLiteral) itemExpr).getAsString(); try { Environment.Namespace ns = env.importLib(s, null); result.add(ns); } catch (IOException ioe) { throw new _MiscTemplateException(((StringLiteral) itemExpr), "Couldn't import library ", new _DelayedJQuote(s), ": ", new _DelayedGetMessage(ioe)); } } else { result.add(val.get(i)); } } return result; }
@Override TemplateElement[] accept(Environment env) throws IOException, TemplateException { TemplateModel node = targetNode.eval(env); if (!(node instanceof TemplateNodeModel)) { throw new NonNodeException(targetNode, node, env); } TemplateModel nss = namespaces == null ? null : namespaces.eval(env); if (namespaces instanceof StringLiteral) { nss = env.importLib(((TemplateScalarModel) nss).getAsString(), null); } else if (namespaces instanceof ListLiteral) { nss = ((ListLiteral) namespaces).evaluateStringsToNamespaces(env); } if (nss != null) { if (nss instanceof Environment.Namespace) { SimpleSequence ss = new SimpleSequence(1); ss.add(nss); nss = ss; } else if (!(nss instanceof TemplateSequenceModel)) { if (namespaces != null) { throw new NonSequenceException(namespaces, nss, env); } else { // Should not occur throw new _MiscTemplateException(env, "Expecting a sequence of namespaces after \"using\""); } } } env.invokeNodeHandlerFor((TemplateNodeModel) node, (TemplateSequenceModel) nss); return null; }
nss = env.importLib(((TemplateScalarModel) nss).getAsString(), null); } else if (namespaces instanceof ListLiteral) { nss = ((ListLiteral) namespaces).evaluateStringsToNamespaces(env);
@Override TemplateElement[] accept(Environment env) throws TemplateException, IOException { final String importedTemplateName = importedTemplateNameExp.evalAndCoerceToPlainText(env); final String fullImportedTemplateName; try { fullImportedTemplateName = env.toFullTemplateName(getTemplate().getName(), importedTemplateName); } catch (MalformedTemplateNameException e) { throw new _MiscTemplateException(e, env, "Malformed template name ", new _DelayedJQuote(e.getTemplateName()), ":\n", e.getMalformednessDescription()); } try { env.importLib(fullImportedTemplateName, targetNsVarName); } catch (IOException e) { throw new _MiscTemplateException(e, env, "Template importing failed (for parameter value ", new _DelayedJQuote(importedTemplateName), "):\n", new _DelayedGetMessage(e)); } return null; }
@Override public void execute(Environment environment, @SuppressWarnings("rawtypes") Map parameters, TemplateModel[] loopVars, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException { @SuppressWarnings("unchecked") String path = TemplateModelUtils.converString(((Map<String, TemplateModel>) parameters).get("path")); @SuppressWarnings("unchecked") String namespace = TemplateModelUtils.converString(((Map<String, TemplateModel>) parameters).get("namespace")); if (CommonUtils.notEmpty(path) && CommonUtils.notEmpty(namespace) && null != environment) { environment.importLib(SiteComponent.getFullTemplatePath(site, path), namespace); } } }
@Override public void execute(Environment environment, @SuppressWarnings("rawtypes") Map parameters, TemplateModel[] loopVars, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException { @SuppressWarnings("unchecked") String path = TemplateModelUtils.converString(((Map<String, TemplateModel>) parameters).get("path")); @SuppressWarnings("unchecked") String namespace = TemplateModelUtils.converString(((Map<String, TemplateModel>) parameters).get("namespace")); if (CommonUtils.notEmpty(path) && CommonUtils.notEmpty(namespace) && null != environment) { environment.importLib(SiteComponent.getFullTemplatePath(site, path), namespace); } } }
/** * Does what the <code>#import</code> directive does, but with an already loaded template. * * @param loadedTemplate * The template to import. Note that it does <em>not</em> need to be a template returned by * {@link #getTemplateForImporting(String name)}. Not {@code null}. * @param targetNsVarName * The name of the FTL variable that will store the namespace. If {@code null}, the namespace * won't be stored in a variable (but it's still returned). * * @return The namespace of the imported template, already initialized. * * @see #getTemplateForImporting(String name) * @see #importLib(Template includedTemplate, String namespaceVarName) */ public Namespace importLib(Template loadedTemplate, String targetNsVarName) throws IOException, TemplateException { return importLib(null, loadedTemplate, targetNsVarName); }
/** * Like {@link #importLib(String, String)}, but you can specify if you want a * {@linkplain #setLazyImports(boolean) lazy import} or not. * * @return Not {@code null}. This is possibly a lazily self-initializing namespace, which mean that it will only try * to get and process the imported template when you access its content. * * @since 2.3.25 */ public Namespace importLib(String templateName, String targetNsVarName, boolean lazy) throws IOException, TemplateException { return lazy ? importLib(templateName, null, targetNsVarName) : importLib(null, getTemplateForImporting(templateName), targetNsVarName); }
/** * Like {@link #importLib(String, String)}, but you can specify if you want a * {@linkplain #setLazyImports(boolean) lazy import} or not. * * @return Not {@code null}. This is possibly a lazily self-initializing namespace, which mean that it will only try * to get and process the imported template when you access its content. * * @since 2.3.25 */ public Namespace importLib(String templateName, String targetNsVarName, boolean lazy) throws IOException, TemplateException { return lazy ? importLib(templateName, null, targetNsVarName) : importLib(null, getTemplateForImporting(templateName), targetNsVarName); }
/** * Emulates <code>import</code> directive, except that <code>name</code> must be tempate * root relative. * * <p>It's the same as <code>importLib(getTemplateForImporting(name), namespace)</code>. * But, you may want to separately call these two methods, so you can determine the source of * exceptions more precisely, and thus achieve more intelligent error handling. * * @see #getTemplateForImporting(String name) * @see #importLib(Template includedTemplate, String namespace) */ public Namespace importLib(String name, String namespace) throws IOException, TemplateException { return importLib(getTemplateForImporting(name), namespace); }
protected void doAutoImportsAndIncludes(Environment env) throws TemplateException, IOException { for (int i=0; i<autoImports.size(); i++) { String namespace = (String) autoImports.get(i); String templateName = (String) autoImportMap.get(namespace); env.importLib(templateName, namespace); } for (int i = 0; i < autoIncludes.size(); i++) { String templateName = (String) autoIncludes.get(i); Template template = getTemplate(templateName, env.getLocale()); env.include(template); } }
public Object exec(List args) throws TemplateModelException { if (!args.isEmpty()) { throw new TemplateModelException("This method supports no parameters."); } try { return env.importLib(template, null); } catch (IOException e) { throw new _TemplateModelException(e, "Failed to import loaded template; see cause exception"); } catch (TemplateException e) { throw new _TemplateModelException(e, "Failed to import loaded template; see cause exception"); } } });
public Object exec(List args) throws TemplateModelException { if (!args.isEmpty()) { throw new TemplateModelException("This method supports no parameters."); } try { return env.importLib(template, null); } catch (IOException e) { throw new _TemplateModelException(e, "Failed to import loaded template; see cause exception"); } catch (TemplateException e) { throw new _TemplateModelException(e, "Failed to import loaded template; see cause exception"); } } });
String macrosSource = getMacros(); // Needed to create the fictional template, has no real purpose Configuration wrapper = new Configuration(Configuration.VERSION_2_3_21); wrapper.setObjectWrapper(new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_21).build()); // Fictional template which has the only purpose to hold your macroSource Template macrosFictionalTemplate = new Template(null, macroSource, wrapper); // Get template as you normally would Template mainTemplate = configuration.getTemplate("main_template"); // Create a processing environment. This is an object on which you can call // .process just as you can on a template. The model and out are the same as // Template#process(model, out) Environment mainTemplateEnvironment = mainTemplate.createProcessingEnvironment(model, out); // Either include -> macros will be accessible as top-level mainTemplateEnvironment.include(macrosFictionalTemplate); // Or import -> macros will be accessible using macros.macroName mainTemplateEnvironment.importLib(macrosFictionalTemplate, "macros"); // Process everything mainTemplateEnvironment.process();
TemplateSequenceModel evaluateStringsToNamespaces(Environment env) throws TemplateException { TemplateSequenceModel val = (TemplateSequenceModel) eval(env); SimpleSequence result = new SimpleSequence(val.size()); for (int i = 0; i < items.size(); i++) { Object itemExpr = items.get(i); if (itemExpr instanceof StringLiteral) { String s = ((StringLiteral) itemExpr).getAsString(); try { Environment.Namespace ns = env.importLib(s, null); result.add(ns); } catch (IOException ioe) { throw new _MiscTemplateException(((StringLiteral) itemExpr), "Couldn't import library ", new _DelayedJQuote(s), ": ", new _DelayedGetMessage(ioe)); } } else { result.add(val.get(i)); } } return result; }