001package io.jstach.jstachio;
002
003import java.io.IOException;
004import java.util.function.Function;
005
006import org.eclipse.jdt.annotation.Nullable;
007
008/**
009 * A JStachio Template is a renderer that has template meta data.
010 * <p>
011 * Generated code implements this interface.
012 *
013 * @author agentgt
014 * @param <T> the model type
015 */
016public interface Template<T> extends Renderer<T>, TemplateInfo {
017
018        /**
019         * Renders the passed in model.
020         * @param model a model assumed never to be <code>null</code>.
021         * @param appendable the appendable to write to.
022         * @throws IOException if there is an error writing to the appendable
023         */
024        default void execute(T model, Appendable appendable) throws IOException {
025                execute(model, appendable, Formatter.of(templateFormatter()), Escaper.of(templateEscaper()));
026        }
027
028        /**
029         * Renders the passed in model.
030         * @param model a model assumed never to be <code>null</code>.
031         * @param a appendable to write to.
032         * @param formatter formats variables before they are passed to the escaper
033         * @param escaper used to write escaped variables
034         * @throws IOException if an error occurs while writing to the appendable
035         */
036        public void execute(T model, //
037                        Appendable a, //
038                        Formatter formatter, //
039                        Escaper escaper) throws IOException;
040
041        /**
042         * Renders the passed in model.
043         * @param model a model assumed never to be <code>null</code>.
044         * @param a appendable to write to.
045         * @param formatter formats variables before they are passed to the escaper
046         * @param escaper used to write escaped variables
047         * @throws IOException if an error occurs while writing to the appendable
048         */
049        default void execute(T model, //
050                        Appendable a, //
051                        Function<@Nullable Object, String> formatter, //
052                        Function<String, String> escaper) throws IOException {
053                execute(model, a, Formatter.of(formatter), Escaper.of(escaper));
054        }
055
056        /**
057         * Return the model class (root context class annotated with JStache) that generated
058         * this template.
059         * @return model class
060         */
061        public Class<?> modelClass();
062
063}