CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-dustjs-helpers

Context helpers that extend the core Dust.js templating system with conditional logic, mathematical operations, and iteration utilities.

Overview
Eval results
Files

conditional-logic.mddocs/

Conditional Logic

Conditional logic helpers that create complex template logic with type-safe comparisons and multiple test scenarios. The select/truth test system enables sophisticated conditional rendering patterns.

Capabilities

Select Helper

Groups truth tests and outputs the first one that passes, providing structured conditional logic for templates.

/**
 * Groups truth tests and outputs the first match
 * @param key - Value to use as left-hand side of comparisons (optional)
 * @param type - Coerce all comparison keys to this type (optional)
 */
{@select key="comparison_value" type="coercion_type"}
  <!-- Truth test helpers go here -->
{/select}

Usage Examples:

// Basic select with key
{@select key="{userStatus}"}
  {@eq value="active"}User is active{/eq}
  {@eq value="inactive"}User is inactive{/eq}
  {@eq value="pending"}User is pending approval{/eq}
{/select}

// Select with type coercion
{@select key="{userAge}" type="number"}
  {@lt value="18"}Minor{/lt}
  {@gte value="18"}Adult{/gte}
{/select}

// Select without global key (each truth test provides its own key)
{@select}
  {@eq key="{status}" value="published"}Content is published{/eq}
  {@eq key="{featured}" value="true"}Content is featured{/eq}
{/select}

Truth Test Helpers

Comparison helpers that test conditions and render content based on the results. All truth tests can be used standalone or within {@select} blocks.

/**
 * Equality test helper
 * @param key - Left-hand side value (optional if within @select)
 * @param value - Right-hand side value for comparison (required)
 * @param type - Type coercion override (optional)
 */
{@eq key="left_value" value="right_value" type="coercion_type"}
  Content when values are equal
{:else}
  Content when values are not equal
{/eq}

/**
 * Inequality test helper
 * @param key - Left-hand side value (optional if within @select)
 * @param value - Right-hand side value for comparison (required)
 * @param type - Type coercion override (optional)
 */
{@ne key="left_value" value="right_value" type="coercion_type"}
  Content when values are not equal
{:else}
  Content when values are equal
{/ne}

/**
 * Less than test helper
 * @param key - Left-hand side value (optional if within @select)
 * @param value - Right-hand side value for comparison (required)
 * @param type - Type coercion override (optional)
 */
{@lt key="left_value" value="right_value" type="coercion_type"}
  Content when left < right
{:else}
  Content when left >= right
{/lt}

/**
 * Less than or equal test helper
 * @param key - Left-hand side value (optional if within @select)
 * @param value - Right-hand side value for comparison (required)
 * @param type - Type coercion override (optional)
 */
{@lte key="left_value" value="right_value" type="coercion_type"}
  Content when left <= right
{:else}
  Content when left > right
{/lte}

/**
 * Greater than test helper
 * @param key - Left-hand side value (optional if within @select)
 * @param value - Right-hand side value for comparison (required)
 * @param type - Type coercion override (optional)
 */
{@gt key="left_value" value="right_value" type="coercion_type"}
  Content when left > right
{:else}
  Content when left <= right
{/gt}

/**
 * Greater than or equal test helper
 * @param key - Left-hand side value (optional if within @select)
 * @param value - Right-hand side value for comparison (required)
 * @param type - Type coercion override (optional)
 */
{@gte key="left_value" value="right_value" type="coercion_type"}
  Content when left >= right
{:else}
  Content when left < right
{/gte}

Usage Examples:

// Standalone truth tests
{@eq key="{user.role}" value="admin"}
  <div class="admin-panel">Admin Controls</div>
{/eq}

{@gt key="{score}" value="90"}
  <span class="grade-a">Excellent!</span>
{:else}
  <span class="grade-lower">Keep trying</span>
{/gt}

// Within select blocks
{@select key="{priority}" type="number"}
  {@eq value="1"}High Priority{/eq}
  {@eq value="2"}Medium Priority{/eq}
  {@eq value="3"}Low Priority{/eq}
{/select}

Conditional Flow Helpers

Special helpers that work within {@select} blocks to handle complex conditional scenarios.

/**
 * Any helper - renders if at least one truth test in the parent @select passes
 * Must be used inside a @select block
 */
{@any}
  Content to show if any condition matched
{/any}

/**
 * None helper - renders if no truth tests in the parent @select pass
 * Must be used inside a @select block
 */
{@none}
  Content to show if no conditions matched
{/none}

Usage Examples:

{@select key="{userType}"}
  {@eq value="premium"}Premium Features Available{/eq}
  {@eq value="pro"}Pro Features Available{/eq}
  {@any}
    <p>You have special account privileges</p>
  {/any}
  {@none}
    <p>Standard account features only</p>
  {/none}
{/select}

// Complex example with multiple conditions
{@select key="{user.status}"}
  {@eq value="active"}
    {@select key="{user.subscription}"}
      {@eq value="premium"}Premium Active User{/eq}
      {@eq value="basic"}Basic Active User{/eq}
      {@none}Active User - No Subscription{/none}
    {/select}
  {/eq}
  {@eq value="inactive"}Inactive User{/eq}
  {@none}Unknown User Status{/none}
{/select}

Type Coercion

All comparison helpers support automatic type coercion to ensure consistent comparisons:

  • number: Converts values using +value
  • string: Converts values using String(value)
  • boolean: Converts values to boolean ('false' string becomes false)
  • date: Converts values using new Date(value)

Type Coercion Examples:

// String comparison
{@select key="{userId}" type="string"}
  {@eq value="123"}User found{/eq}
{/select}

// Numeric comparison
{@select key="{age}" type="number"}
  {@gte value="21"}Can drink{/gte}
  {@lt value="21"}Too young{/lt}
{/select}

// Boolean comparison
{@select key="{isEnabled}" type="boolean"}
  {@eq value="true"}Feature is enabled{/eq}
  {@eq value="false"}Feature is disabled{/eq}
{/select}

Error Handling:

Truth Test Helpers (eq, ne, lt, lte, gt, gte):

  • Missing key: Logs warning "{@helperName}: No key specified" and skips rendering
  • Missing value parameter: No error, uses undefined for comparison
  • Type coercion errors: Values that cannot be coerced retain their original type

Select Helper:

  • Missing body block: Logs warning "{@select}: Missing body block"
  • Invalid key references: Resolves to undefined and continues processing
  • Type coercion failures: Uses original value type if coercion fails

Any/None Helpers:

  • Used outside @select: Logs error "{@any}: Must be used inside a {@select} block"
  • Nested any/none: Logs error "{@any}: Must not be nested inside {@any} or {@none} block" and skips rendering
  • Deferred rendering failures: Handled gracefully with chunk mapping

Logging:

All error and warning messages use the dust logging system:

  • dust.log(helperName, message, "WARN") for non-critical issues
  • dust.log(helperName, message, "ERROR") for critical violations
  • Helper names in log messages match the template helper name (e.g., "eq", "select", "any")

Install with Tessl CLI

npx tessl i tessl/npm-dustjs-helpers

docs

conditional-logic.md

index.md

iteration-control.md

math-operations.md

utility-helpers.md

tile.json