Search in sources :

Example 6 with TemplateModel

use of freemarker.template.TemplateModel in project entando-core by entando.

the class ControllerServlet method initFreemarker.

protected void initFreemarker(HttpServletRequest request, HttpServletResponse response, RequestContext reqCtx) throws TemplateModelException {
    Configuration config = new Configuration();
    DefaultObjectWrapper wrapper = new DefaultObjectWrapper();
    config.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
    config.setObjectWrapper(wrapper);
    config.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);
    TemplateModel templateModel = this.createModel(wrapper, this.getServletContext(), request, response);
    ExecutorBeanContainer ebc = new ExecutorBeanContainer(config, templateModel);
    reqCtx.addExtraParam(SystemConstants.EXTRAPAR_EXECUTOR_BEAN_CONTAINER, ebc);
}
Also used : Configuration(freemarker.template.Configuration) DefaultObjectWrapper(freemarker.template.DefaultObjectWrapper) TemplateModel(freemarker.template.TemplateModel) ExecutorBeanContainer(org.entando.entando.aps.system.services.controller.executor.ExecutorBeanContainer)

Example 7 with TemplateModel

use of freemarker.template.TemplateModel in project wombat by PLOS.

the class TemplateModelUtil method getAsMap.

static ImmutableMap<String, TemplateModel> getAsMap(TemplateModel value) throws TemplateModelException {
    if (value == null)
        return ImmutableMap.of();
    if (value instanceof TemplateHashModelEx) {
        TemplateHashModelEx ftlHash = (TemplateHashModelEx) value;
        ImmutableMap.Builder<String, TemplateModel> builder = ImmutableMap.builder();
        for (TemplateModelIterator iterator = ftlHash.keys().iterator(); iterator.hasNext(); ) {
            String key = iterator.next().toString();
            builder.put(key, ftlHash.get(key));
        }
        return builder.build();
    }
    throw new TemplateModelException("Hash type expected");
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateModelIterator(freemarker.template.TemplateModelIterator) TemplateHashModelEx(freemarker.template.TemplateHashModelEx) TemplateModel(freemarker.template.TemplateModel) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 8 with TemplateModel

use of freemarker.template.TemplateModel in project wombat by PLOS.

the class TemplateModelUtil method getAsMultimap.

static ImmutableListMultimap<String, TemplateModel> getAsMultimap(TemplateModel value) throws TemplateModelException {
    if (value == null)
        return ImmutableListMultimap.of();
    if (value instanceof TemplateHashModelEx) {
        TemplateHashModelEx ftlHash = (TemplateHashModelEx) value;
        ImmutableListMultimap.Builder<String, TemplateModel> builder = ImmutableListMultimap.builder();
        for (TemplateModelIterator iterator = ftlHash.keys().iterator(); iterator.hasNext(); ) {
            String key = iterator.next().toString();
            TemplateModel model = ftlHash.get(key);
            if (model instanceof TemplateSequenceModel) {
                TemplateSequenceModel sequenceModel = (TemplateSequenceModel) model;
                int size = sequenceModel.size();
                for (int i = 0; i < size; i++) {
                    builder.put(key, sequenceModel.get(i));
                }
            } else {
                builder.put(key, model);
            }
        }
        return builder.build();
    }
    throw new TemplateModelException("Hash type expected");
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateSequenceModel(freemarker.template.TemplateSequenceModel) TemplateModelIterator(freemarker.template.TemplateModelIterator) TemplateHashModelEx(freemarker.template.TemplateHashModelEx) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) TemplateModel(freemarker.template.TemplateModel)

Example 9 with TemplateModel

use of freemarker.template.TemplateModel in project wombat by PLOS.

the class TemplateModelUtil method getAsList.

static ImmutableList<TemplateModel> getAsList(TemplateModel value) throws TemplateModelException {
    if (value == null)
        return ImmutableList.of();
    if (value instanceof TemplateSequenceModel) {
        ImmutableList.Builder<TemplateModel> builder = ImmutableList.builder();
        TemplateSequenceModel sequenceModel = (TemplateSequenceModel) value;
        int size = sequenceModel.size();
        for (int i = 0; i < size; i++) {
            builder.add(sequenceModel.get(i));
        }
        return builder.build();
    } else {
        return ImmutableList.of(value);
    }
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) ImmutableList(com.google.common.collect.ImmutableList) TemplateModel(freemarker.template.TemplateModel)

Example 10 with TemplateModel

use of freemarker.template.TemplateModel in project freemarker by apache.

the class DynamicKeyName method _eval.

@Override
TemplateModel _eval(Environment env) throws TemplateException {
    TemplateModel targetModel = target.eval(env);
    if (targetModel == null) {
        if (env.isClassicCompatible()) {
            return null;
        } else {
            throw InvalidReferenceException.getInstance(target, env);
        }
    }
    TemplateModel keyModel = keyExpression.eval(env);
    if (keyModel == null) {
        if (env.isClassicCompatible()) {
            keyModel = TemplateScalarModel.EMPTY_STRING;
        } else {
            keyExpression.assertNonNull(null, env);
        }
    }
    if (keyModel instanceof TemplateNumberModel) {
        int index = keyExpression.modelToNumber(keyModel, env).intValue();
        return dealWithNumericalKey(targetModel, index, env);
    }
    if (keyModel instanceof TemplateScalarModel) {
        String key = EvalUtil.modelToString((TemplateScalarModel) keyModel, keyExpression, env);
        return dealWithStringKey(targetModel, key, env);
    }
    if (keyModel instanceof RangeModel) {
        return dealWithRangeKey(targetModel, (RangeModel) keyModel, env);
    }
    throw new UnexpectedTypeException(keyExpression, keyModel, "number, range, or string", new Class[] { TemplateNumberModel.class, TemplateScalarModel.class, Range.class }, env);
}
Also used : TemplateNumberModel(freemarker.template.TemplateNumberModel) TemplateScalarModel(freemarker.template.TemplateScalarModel) TemplateModel(freemarker.template.TemplateModel)

Aggregations

TemplateModel (freemarker.template.TemplateModel)108 TemplateModelException (freemarker.template.TemplateModelException)26 Map (java.util.Map)23 TemplateScalarModel (freemarker.template.TemplateScalarModel)18 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)15 TemplateException (freemarker.template.TemplateException)13 HashMap (java.util.HashMap)13 SimpleScalar (freemarker.template.SimpleScalar)11 IOException (java.io.IOException)11 List (java.util.List)11 Template (freemarker.template.Template)9 TemplateSequenceModel (freemarker.template.TemplateSequenceModel)8 TemplateTransformModel (freemarker.template.TemplateTransformModel)8 SimpleHash (freemarker.template.SimpleHash)7 SimpleSequence (freemarker.template.SimpleSequence)7 Writer (java.io.Writer)7 Iterator (java.util.Iterator)7 Environment (freemarker.core.Environment)6 TemplateHashModel (freemarker.template.TemplateHashModel)6