Interface MarkupContext

All Superinterfaces:
io.karatelabs.js.ObjectLike, io.karatelabs.js.SimpleObject
All Known Implementing Classes:
ServerMarkupContext

public interface MarkupContext extends io.karatelabs.js.SimpleObject
Context interface exposed to templates and server-side JavaScript.

This interface defines the core APIs available in both plain templating mode and server mode. Server-specific methods (like redirect, init, close) are added by ServerMarkupContext which extends this interface.

In templates, access via the 'context' variable:

 <script ka:scope="global">
     var data = context.read('data.json');
     var json = context.toJson({ name: 'test' });
     console.log('Template: ' + context.template);
 </script>
 
  • Field Summary

    Fields inherited from interface io.karatelabs.js.SimpleObject

    logger, TO_STRING
  • Method Summary

    Modifier and Type
    Method
    Description
    Parse a JSON string into an object (Map or List).
    Returns the caller template name (for fragments/includes), or null if top-level.
    default Object
    Returns the session object if one exists, null otherwise.
    default MarkupScope
    Companion accessor for setMarkupScope(MarkupScope).
    Returns the current template name (without leading slash).
    default Object
    Default implementation of jsGet that exposes context methods to JavaScript.
    read(String path)
    Read a resource as text.
    byte[]
    Read a resource as bytes.
    default void
    Set the current-eval scope accessor.
    Serialize an object to JSON string.

    Methods inherited from interface io.karatelabs.js.ObjectLike

    getMember, getPrototype, isExtensible, isFrozen, isOwnProperty, isSealed, putMember, removeMember, setExtensible, setFrozen, setSealed

    Methods inherited from interface io.karatelabs.js.SimpleObject

    getMember, jsKeys, jsToString, putMember, removeMember, toMap
  • Method Details

    • getTemplateName

      String getTemplateName()
      Returns the current template name (without leading slash).
    • getCallerTemplateName

      String getCallerTemplateName()
      Returns the caller template name (for fragments/includes), or null if top-level.
    • read

      String read(String path)
      Read a resource as text.
      Parameters:
      path - resource path (relative to template root or absolute)
      Returns:
      text content of the resource
    • readBytes

      byte[] readBytes(String path)
      Read a resource as bytes.
      Parameters:
      path - resource path (relative to template root or absolute)
      Returns:
      byte array content of the resource
    • toJson

      String toJson(Object obj)
      Serialize an object to JSON string.
      Parameters:
      obj - object to serialize
      Returns:
      JSON string
    • fromJson

      Object fromJson(String json)
      Parse a JSON string into an object (Map or List).
      Parameters:
      json - JSON string
      Returns:
      parsed object
    • getContextSession

      default Object getContextSession()
      Returns the session object if one exists, null otherwise. Used by template engine to sync session variable after context.init() is called. Default returns null (no session in plain templating mode).
    • setMarkupScope

      default void setMarkupScope(MarkupScope scope)
      Set the current-eval scope accessor. Called by MarkupTemplateContext at engine-bind time so context.get can resolve names from _ and the wrapped Thymeleaf scope. Default no-op for implementations that don't carry scope state.
    • getMarkupScope

      default MarkupScope getMarkupScope()
      Companion accessor for setMarkupScope(MarkupScope). Default returns null; implementations override to surface the scope to the context.get dispatch path.
    • jsGet

      default Object jsGet(String key)
      Default implementation of jsGet that exposes context methods to JavaScript. Implementations can override to add more properties/methods.
      Specified by:
      jsGet in interface io.karatelabs.js.SimpleObject