@ScriptVariable in AEM




In Apache Sling, @ScriptVariable is an annotation used in Sling Models to inject objects from the HTL (HTML Templating Language) script context into the model class. It is part of the Sling Models API and helps bridge the gap between the templating layer and backend logic by allowing developers to access various predefined or custom variables.

Key Features of @ScriptVariable:

  1. Source Context:

    • It pulls variables that are typically available in the HTL script context, such as currentPage, resource, or request.
  2. Type-Safe Access:

    • The variable injected by @ScriptVariable is strongly typed, meaning the developer defines the type of the object to be injected.
  3. Simplifies Code:

    • Reduces the need to manually retrieve these variables from the script context, leading to cleaner and more maintainable code.

How It Works:

  1. HTL Context:

    • Variables like currentPage are typically defined automatically in the HTL context when rendering a component or page.
    • Example in HTL:
      <h1>${currentPage.title}</h1>
  2. Sling Model Binding:

    • With @ScriptVariable, you bind these HTL-provided variables to backend logic.
    • The framework automatically resolves the variable name (currentPage, etc.) and injects it into the model.

Common Script Variables in AEM:

  • currentPage: Represents the current page being rendered as a Page object.
  • resource: The current Sling Resource being processed.
  • currentDesign: Represents the current design object used in the context of the page.
  • wcmmode: Provides the current WCM mode (e.g., edit, preview, disabled).
  • request: The current HTTP request object (SlingHttpServletRequest).
  • response: The current HTTP response object (SlingHttpServletResponse).
  • slingRequest: Alias for request, specific to Sling context.
  • slingResponse: Alias for response, specific to Sling context.
  • component: The current component object.
  • currentStyle: Represents the current design style (used for editable templates and policies).
  • properties: Accesses the properties of the current resource.
  • log: Provides a logging utility for debugging.
  • componentContext: Context information for the component being processed.
  • editContext: Contains information for the current edit session in the authoring environment.
  • resourceResolver: The ResourceResolver associated with the current request.

Key Points to Remember:

  • @ScriptVariable can only inject variables that are available in the rendering context.
  • It works in conjunction with the Sling Models API and requires the proper Sling Models dependencies in your project.
  • If the variable is not available in the script context, it will inject null, unless default values or optional strategies are configured.

This feature is particularly powerful in AEM to simplify development by avoiding redundant lookups for objects that are already available in the rendering layer.

Comments

Popular posts from this blog

Debugging Javascript Memory Leaks

Memory Leaks in Javascripts

Apache Jackrabbit FileVault (VLT)