Package io.karatelabs.markup
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 TypeMethodDescriptionParse a JSON string into an object (Map or List).Returns the caller template name (for fragments/includes), or null if top-level.default ObjectReturns the session object if one exists, null otherwise.Returns the current template name (without leading slash).default ObjectDefault implementation of jsGet that exposes context methods to JavaScript.Read a resource as text.byte[]Read a resource as bytes.Serialize an object to JSON string.Methods inherited from interface io.karatelabs.js.ObjectLike
getPrototypeMethods 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
Read a resource as text.- Parameters:
path- resource path (relative to template root or absolute)- Returns:
- text content of the resource
-
readBytes
Read a resource as bytes.- Parameters:
path- resource path (relative to template root or absolute)- Returns:
- byte array content of the resource
-
toJson
Serialize an object to JSON string.- Parameters:
obj- object to serialize- Returns:
- JSON string
-
fromJson
Parse a JSON string into an object (Map or List).- Parameters:
json- JSON string- Returns:
- parsed 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). -
jsGet
Default implementation of jsGet that exposes context methods to JavaScript. Implementations can override to add more properties/methods.- Specified by:
jsGetin interfaceio.karatelabs.js.SimpleObject
-