Interface ITemplateContext

  • All Superinterfaces:
    IContext, IExpressionContext
    All Known Subinterfaces:
    IEngineContext
    All Known Implementing Classes:
    AbstractEngineContext, EngineContext, WebEngineContext

    public interface ITemplateContext
    extends IExpressionContext

    Interface implemented by all classes containing the context required for template processing.

    This interface extends IExpressionContext and IContext by adding the required information needed to process templates.

    Note that, in order for the template engine to use a custom-made implementation of this interface for template processing instead of cloning its data, such implementation should also implement the IEngineContext interface.

    Also note these implementations do not have to be thread-safe, and in fact should not be shared by different threads or template executions. They are meant to be local to a specific template engine execution.

    Since:
    3.0.0
    Author:
    Daniel Fernández
    • Method Detail

      • getTemplateData

        TemplateData getTemplateData()

        Returns the template data object containing metadata about the template currently being processed.

        Note that the TemplateData returned here corresponds with the origin of the elements or nodes being currently processed. This is, if a processor is being executed for an element inserted from an external template (via a th:insert, for example), then this method will return the template data for the template in which the inserted fragment lives, not the one it was inserted into.

        Returns:
        the template data corresponding to the elements or nodes being currently processed.
      • getTemplateMode

        TemplateMode getTemplateMode()

        Returns the template mode of the template currently being processed.

        Note that the TemplateMode returned here corresponds with origin of the elements or nodes being currently processed. This is, if a processor is being executed for an element inserted from an external template (via a th:insert, for example), then this method will return the template mode for the template in which the inserted fragment lives, not the one it was inserted into.

        Returns:
        the template mode of the elements or nodes being currently processed.
      • getTemplateStack

        List<TemplateData> getTemplateStack()

        Returns the list of all the TemplateData objects corresponding to all the templates that have been nested in order to reach the current execution point.

        This is a way in which processors can know the complete execution route that lead to the execution of a specific event (e.g. Template A inserted fragment B, which inserted fragment C).

        The first element in this list will always be the top-level template (the one called at the ITemplateEngine process(...) methods).

        Returns:
        the stack of templates (list of TemplateData).
      • getElementStack

        List<IProcessableElementTag> getElementStack()

        Returns the list of all the IProcessableElementTag objects corresponding to the hierarchy of elements (open or standalone elements) that had to be processed in order to reach the current point in execution.

        If the element being processed is a tag (open/standalone), it will appear at the end of the list.

        Note this hierarchy does not correspond with the tag hierarchy at the original template, but with the hierarchy of processing (many tags could appear during processing itself and not be present at the original template).

        Also note that, because of this being the processing-time hierarchy, this information is set at the processor level, so it should not be considered to be available and/or valid at the pre-processor layer.

        Returns:
        the stack of elements (list of IProcessableElementTag).
      • getTemplateResolutionAttributes

        Map<String,​Object> getTemplateResolutionAttributes()

        Returns the map of configuration items that have been specified at the process(...) methods of ITemplateEngine, aimed at further configuring the template being used and its resolution by means of the ITemplateResolvers.

        Returns:
        the template resolution attributes map
      • hasSelectionTarget

        boolean hasSelectionTarget()

        Returns whether the current template has set a selection target for the current point of execution or not.

        Selection targets are objects on which all *{...} expression will be executed (instead of on the root context). They are normally set by means of th:objects.

        Returns:
        true if there is a selection target, false if not.
      • getSelectionTarget

        Object getSelectionTarget()

        Returns the selection target set for the current point of execution (or null if there isn't any).

        Selection targets are objects on which all *{...} expression will be executed (instead of on the root context). They are normally set by means of th:objects.

        Returns:
        the selection target, or null if there isn't any.
      • getInliner

        IInliner getInliner()

        Returns the inliner (implementation of IInliner) set to be used at the current point of execution.

        Returns:
        the inliner to be used.
      • getMessage

        String getMessage​(Class<?> origin,
                          String key,
                          Object[] messageParameters,
                          boolean useAbsentMessageRepresentation)

        Computes an externalized (internationalized, i18n) message to be used on a template.

        This method is meant to be called mainly by processors that need to output externalized messages.

        Parameters:
        origin - the origin class to be used for message resolution. When calling from a processor, this is normally the processor class itself. See IMessageResolver.
        key - the key of the message to be retrieved.
        messageParameters - the parameters to be applied to the requested message.
        useAbsentMessageRepresentation - whether an absent message representation should be returned in the case that the message does not exist (see IMessageResolver).
        Returns:
        the requested message, correctly formatted. Or an absent message representation, or null if no absent message representations are allowed.
      • buildLink

        String buildLink​(String base,
                         Map<String,​Object> parameters)

        Computes link to be used on a template.

        This method is meant to be called mainly by processors or expressions that need to output links.

        Parameters:
        base - the base of the link URL to be built, i.e. its path. Can be null.
        parameters - the (optional) URL parameters.
        Returns:
        the built URL, or an exception if no link builders are able to build this link.
      • getIdentifierSequences

        IdentifierSequences getIdentifierSequences()

        Returns the IdentifierSequences object set to be used at the current point of execution.

        Returns:
        the identifier sequences object.