Search in sources :

Example 16 with TemplateModel

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

the class CollectionAndSequence method initSequence.

private void initSequence() throws TemplateModelException {
    if (data == null) {
        data = new ArrayList<TemplateModel>();
        TemplateModelIterator it = collection.iterator();
        while (it.hasNext()) {
            data.add(it.next());
        }
    }
}
Also used : TemplateModelIterator(freemarker.template.TemplateModelIterator) TemplateModel(freemarker.template.TemplateModel)

Example 17 with TemplateModel

use of freemarker.template.TemplateModel in project be5 by DevelopmentOnTheEdge.

the class Environment method getNodeProcessor.

TemplateModel getNodeProcessor(TemplateNodeModel node) throws TemplateException {
    String nodeName = node.getNodeName();
    if (nodeName == null) {
        throw new _MiscTemplateException(this, "Node name is null.");
    }
    TemplateModel result = getNodeProcessor(nodeName, node.getNodeNamespace(), 0);
    if (result == null) {
        String type = node.getNodeType();
        /* DD: Original version: */
        if (type == null) {
            type = "default";
        }
        result = getNodeProcessor("@" + type, null, 0);
    /* DD: Jonathan's non-BC version and IMHO otherwise wrong version:
            if (type != null) {
                result = getNodeProcessor("@" + type, null, 0);
            }
            if (result == null) {
                result = getNodeProcessor("@default", null, 0);
            }
            */
    }
    return result;
}
Also used : TemplateModel(freemarker.template.TemplateModel)

Example 18 with TemplateModel

use of freemarker.template.TemplateModel in project be5 by DevelopmentOnTheEdge.

the class Environment method getLocalVariable.

/**
 * Returns the loop or macro local variable corresponding to this
 * variable name. Possibly null.
 * (Note that the misnomer is kept for backward compatibility: loop variables
 * are not local variables according to our terminology.)
 */
public TemplateModel getLocalVariable(String name) throws TemplateModelException {
    if (localContextStack != null) {
        for (int i = localContextStack.size() - 1; i >= 0; i--) {
            LocalContext lc = (LocalContext) localContextStack.get(i);
            TemplateModel tm = lc.getLocalVariable(name);
            if (tm != null) {
                return tm;
            }
        }
    }
    return currentMacroContext == null ? null : currentMacroContext.getLocalVariable(name);
}
Also used : TemplateModel(freemarker.template.TemplateModel)

Example 19 with TemplateModel

use of freemarker.template.TemplateModel in project be5 by DevelopmentOnTheEdge.

the class Environment method getTransform.

TemplateTransformModel getTransform(Expression exp) throws TemplateException {
    TemplateTransformModel ttm = null;
    TemplateModel tm = exp.eval(this);
    if (tm instanceof TemplateTransformModel) {
        ttm = (TemplateTransformModel) tm;
    } else if (exp instanceof Identifier) {
        tm = getConfiguration().getSharedVariable(exp.toString());
        if (tm instanceof TemplateTransformModel) {
            ttm = (TemplateTransformModel) tm;
        }
    }
    return ttm;
}
Also used : TemplateTransformModel(freemarker.template.TemplateTransformModel) TemplateModel(freemarker.template.TemplateModel)

Example 20 with TemplateModel

use of freemarker.template.TemplateModel in project be5 by DevelopmentOnTheEdge.

the class Environment method visit.

public void visit(final TemplateElement element, TemplateDirectiveModel directiveModel, Map args, final List bodyParameterNames) throws TemplateException, IOException {
    TemplateDirectiveBody nested;
    if (element == null) {
        nested = null;
    } else {
        nested = new TemplateDirectiveBody() {

            public void render(Writer newOut) throws TemplateException, IOException {
                Writer prevOut = out;
                out = newOut;
                try {
                    Environment.this.visit(element);
                } finally {
                    out = prevOut;
                }
            }
        };
    }
    final TemplateModel[] outArgs;
    if (bodyParameterNames == null || bodyParameterNames.isEmpty()) {
        outArgs = NO_OUT_ARGS;
    } else {
        outArgs = new TemplateModel[bodyParameterNames.size()];
    }
    if (outArgs.length > 0) {
        pushLocalContext(new LocalContext() {

            public TemplateModel getLocalVariable(String name) {
                int index = bodyParameterNames.indexOf(name);
                return index != -1 ? outArgs[index] : null;
            }

            public Collection getLocalVariableNames() {
                return bodyParameterNames;
            }
        });
    }
    try {
        directiveModel.execute(this, args, outArgs, nested);
    } finally {
        if (outArgs.length > 0) {
            popLocalContext();
        }
    }
}
Also used : TemplateException(freemarker.template.TemplateException) Collection(java.util.Collection) IOException(java.io.IOException) TemplateModel(freemarker.template.TemplateModel) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) NullWriter(freemarker.template.utility.NullWriter) StringWriter(java.io.StringWriter) TemplateDirectiveBody(freemarker.template.TemplateDirectiveBody)

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