Modules@robinpath/template
template

@robinpath/template

0.1.1Node.jsPublic

Mustache-like template engine with variable substitution, conditional sections, loops, and simple string interpolation

Template

Mustache-like template engine with variable substitution, conditional sections, loops, and simple string interpolation

Package: @robinpath/template | Category: Utility | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the template module when you need to:

  • Render a Mustache-like template string with variable substitution, sections, and loops -- Use template.render to perform this operation
  • Read a template from a file and render it with the provided data object -- Use template.renderFile to perform this operation
  • HTML-escape a string, converting &, <, >, ", and ' to their HTML entity equivalents -- Use template.escape to perform this operation
  • Validate template syntax, checking for unclosed tags, mismatched sections, and other structural errors -- Use template.compile to perform this operation
  • Extract all unique variable and section names used in a template -- Use template.extractVariables to perform this operation

Quick Reference

FunctionDescriptionReturns
renderRender a Mustache-like template string with variable substitution, sections, and loopsThe rendered template string with all tags resolved
renderFileRead a template from a file and render it with the provided data objectThe rendered template string with all tags resolved
escapeHTML-escape a string, converting &, <, >, ", and ' to their HTML entity equivalentsThe HTML-escaped string
compileValidate template syntax, checking for unclosed tags, mismatched sections, and other structural errorsTrue if the template syntax is valid. Throws an error with details if invalid.
extractVariablesExtract all unique variable and section names used in a templateDeduplicated array of all variable and section names found in the template
renderStringSimple string interpolation using ${key} placeholders (no sections or loops)The string with all ${key} placeholders replaced by their values

Functions

render

Render a Mustache-like template string with variable substitution, sections, and loops

Module: template | Returns: string -- The rendered template string with all tags resolved

template.render "Hello, {{name}}!" { "name": "World" }
ParameterTypeRequiredDescription
templatestringYesThe template string containing {{variable}}, {{{unescaped}}}, {{#section}}...{{/section}}, {{^inverted}}...{{/inverted}}, and {{! comment }} tags
dataobjectYesKey-value data object for variable substitution. Supports nested objects via dot notation.

renderFile

Read a template from a file and render it with the provided data object

Module: template | Returns: string -- The rendered template string with all tags resolved

template.renderFile "/tmp/greeting.mustache" { "name": "World" }
ParameterTypeRequiredDescription
templateFilePathstringYesAbsolute or relative path to the template file
dataobjectYesKey-value data object for variable substitution. Supports nested objects via dot notation.

escape

HTML-escape a string, converting &, <, >, ", and ' to their HTML entity equivalents

Module: template | Returns: string -- The HTML-escaped string

template.escape "<script>alert(1)</script>"
ParameterTypeRequiredDescription
stringstringYesThe string to HTML-escape

compile

Validate template syntax, checking for unclosed tags, mismatched sections, and other structural errors

Module: template | Returns: boolean -- True if the template syntax is valid. Throws an error with details if invalid.

template.compile "{{#items}}{{name}}{{/items}}"
ParameterTypeRequiredDescription
templatestringYesThe template string to validate

extractVariables

Extract all unique variable and section names used in a template

Module: template | Returns: array -- Deduplicated array of all variable and section names found in the template

template.extractVariables "{{greeting}}, {{name}}! {{#show}}Visible{{/show}}"
ParameterTypeRequiredDescription
templatestringYesThe template string to analyze

renderString

Simple string interpolation using ${key} placeholders (no sections or loops)

Module: template | Returns: string -- The string with all ${key} placeholders replaced by their values

template.renderString "Hello, ${name}!" { "name": "World" }
ParameterTypeRequiredDescription
stringstringYesThe string containing ${key} placeholders for substitution
dataobjectYesKey-value data object for placeholder substitution. Supports dot notation.

Error Handling

All functions throw on failure. Common errors:

ErrorCause
Unclosed unescaped tag starting at position ${openIdx}Check the error message for details
Unclosed tag starting at position ${openIdx}Check the error message for details
Unexpected closing tag {{/${tag.slice(1).trim()}}} at position ${openIdx}Check the error message for details
Unclosed tag starting at position ${nextOpen}Check the error message for details
Empty section name at position ${openIdx}Check the error message for details
Empty closing tag at position ${openIdx}Check the error message for details
Unclosed section "{{#...}}": no matching "{{/...}}" foundCheck the error message for details
Unclosed unescaped tag "{{{" at position ...Check the error message for details
@desc "Render and validate result"
do
  set $result as template.render "Hello, {{name}}!" { "name": "World" }
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. Multi-step Template workflow

Chain multiple template operations together.

@desc "Render, render file, and more"
do
  set $r_render as template.render "Hello, {{name}}!" { "name": "World" }
  set $r_renderFile as template.renderFile "/tmp/greeting.mustache" { "name": "World" }
  set $r_escape as template.escape "<script>alert(1)</script>"
  print "All operations complete"
enddo

2. Safe render with validation

Check results before proceeding.

@desc "Render and validate result"
do
  set $result as template.render "Hello, {{name}}!" { "name": "World" }
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/template

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size6.0 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Category

utilities