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.default MarkupScopeCompanion accessor forsetMarkupScope(MarkupScope).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.default voidsetMarkupScope(MarkupScope scope) 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, setSealedMethods 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). -
setMarkupScope
Set the current-eval scope accessor. Called byMarkupTemplateContextat engine-bind time socontext.getcan resolve names from_and the wrapped Thymeleaf scope. Default no-op for implementations that don't carry scope state. -
getMarkupScope
Companion accessor forsetMarkupScope(MarkupScope). Default returns null; implementations override to surface the scope to thecontext.getdispatch path. -
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
-