Skip to content
/ templ Public

mustache compatible template engine with extras

Notifications You must be signed in to change notification settings

fockjef/templ

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

templ

Regex based template engine with some nifty features. Fully compatibility with mustache templates while incorporating the ability to use simple logic within templates.

Features:

  • Backtracking with ../ syntax
  • Reference current array index (1-based) with {{#}}
  • Ability to execute arbitrary javascript within template {{somenum.toFixed(3)}}
  • this syntax for referencing current context

Caveats:

  • Slower performance than tokenizer based engines (but still plenty fast)
  • Ability to execute arbitrary javascript within template {{maliciousFunction()}}

Examples:

Data:

{
   first: "Bob",
   last: "Smith",
   dob: "3/2/1987",
   pets: [
      { name: "Rover", type: "dog", favorite: true },
      { name: "Kitty", type: "cat" },
      { name: "Goldy", type: "fish" }
   ]
}

Template:

{{first}} {{last}} is {{new Date(Date.now()-new Date(this.dob)).getFullYear()-1970}} years old.<br>
{{first}} has {{pets.length}} pet{{pets.length==1?"":"s"}}.<br>
{{#pets}}
{{name}} is a {{type}}{{#favorite}} and is {{../../first}}'s favorite pet{{/favorite}}.<br>
{{/pets}}

Results:

Bob Smith is 31 years old.
Bob has 3 pets.
Rover is a dog and is Bob's favorite pet.
Kitty is a cat.
Goldy is a fish.


For more examples and a better understanding of templating in general see the mustache documentation at https://mustache.github.io/mustache.5.html

About

mustache compatible template engine with extras

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published