Class RenderContext

java.lang.Object
dev.jcputney.mjml.context.RenderContext

public class RenderContext extends Object
Per-component render context that carries the current container width as the rendering pipeline descends the component tree. Width narrows: body(600) -> section -> column -> content component.
  • Constructor Details

    • RenderContext

      public RenderContext(double containerWidth)
      Creates a new root render context with the given container width. The context starts with default values: index 0, first and last both true, not inside a wrapper or group, and a fresh ID counter.
      Parameters:
      containerWidth - the initial container width in pixels (e.g. 600)
  • Method Details

    • getContainerWidth

      public double getContainerWidth()
      Returns the current container width in pixels.
      Returns:
      the container width in pixels
    • getColumnWidthSpec

      public String getColumnWidthSpec()
      Returns the column width specification as computed by the parent section. For percentage widths: "33.33" (just the number) For pixel widths: "150px" (with px suffix) For auto columns: "100" or "33.333333333333336" (full precision)
      Returns:
      the column width specification string, or null if not set
    • getIndex

      public int getIndex()
      Returns the zero-based index of this component among its siblings.
      Returns:
      the sibling index
    • isFirst

      public boolean isFirst()
      Returns whether this component is the first among its siblings.
      Returns:
      true if this is the first sibling
    • isLast

      public boolean isLast()
      Returns whether this component is the last among its siblings.
      Returns:
      true if this is the last sibling
    • isInsideWrapper

      public boolean isInsideWrapper()
      Returns whether this component is nested inside an mj-wrapper.
      Returns:
      true if inside a wrapper
    • isInsideGroup

      public boolean isInsideGroup()
      Returns whether this component is nested inside an mj-group.
      Returns:
      true if inside a group
    • nextUniqueId

      public String nextUniqueId(String prefix)
      Returns a deterministic unique ID with the given prefix. The counter is shared across all child contexts derived from the same root.
      Parameters:
      prefix - the prefix for the generated ID
      Returns:
      a unique ID in the form "{prefix}-{counter}"
    • withWidth

      public RenderContext withWidth(double width)
      Creates a child context with a narrower container width.
      Parameters:
      width - the new container width in pixels
      Returns:
      a new render context with the updated width
    • withColumnWidth

      public RenderContext withColumnWidth(double width, String columnWidthSpec)
      Creates a child context with container width and column width specification.
      Parameters:
      width - the new container width in pixels
      columnWidthSpec - the column width specification string (e.g. "33.33" or "150px")
      Returns:
      a new render context with the updated width and column width spec
    • withPosition

      public RenderContext withPosition(int index, boolean first, boolean last)
      Creates a child context with positioning info for column rendering.
      Parameters:
      index - the zero-based index of the column among its siblings
      first - whether this is the first sibling
      last - whether this is the last sibling
      Returns:
      a new render context with the updated position info
    • withInsideWrapper

      public RenderContext withInsideWrapper(boolean insideWrapper)
      Creates a child context indicating the component is inside a wrapper.
      Parameters:
      insideWrapper - whether the component is inside an mj-wrapper
      Returns:
      a new render context with the updated wrapper flag
    • withInsideGroup

      public RenderContext withInsideGroup(boolean insideGroup)
      Creates a child context indicating the component is inside a group.
      Parameters:
      insideGroup - whether the component is inside an mj-group
      Returns:
      a new render context with the updated group flag