- Notifications
You must be signed in to change notification settings - Fork20
Raku library for the Mustache template format
License
softmoth/raku-Template-Mustache
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Raku implementation of Mustache templates,http://mustache.github.io/.
use Template::Mustache;# Call .render as a class methodTemplate::Mustache.render('Hello, {{planet}}!', {planet=>'world' }).say;# Or instantiate an instancemy$stache= Template::Mustache.new::from<./views>;# Subroutines are calledsay$stache.render('The time is {{time}}', {time=> {~DateTime.new(now).local }});my@people= {:name('James T. Kirk'),:title<Captain> }, {:name('Wesley'),:title('Dread Pirate'),:emcee }, {:name('Dana Scully'),:title('Special Agent') }, ;# See this template in ./t/views/roster.mustache$stache.render('roster', {:@people }).say;my%context=event=>'Masters of the Universe Convention',:@people, ;my%partials=welcome=>qq:b{Welcome to the{{event}}! We’re pleased to have you here.\n\n}, ;# See this result in ./t/50-readme.tTemplate::Mustache.render(q:to/EOF/, {{> welcome}} {{> roster}} Dinner at 7PM in the Grand Ballroom. Bring a chair! EOF%context,:from([%partials,'./views'])).say;
Messages are logged with varying severity levels (from most to least severe):Fatal,Error,Warn,Info,Verbose,Audit,Debug,Trace,Trace2
By default, only messages ofError or worse are logged. That default can be changed with theTEMPLATE_MUSTACHE_LOGLEVEL environment variable.
TEMPLATE_MUSTACHE_LOGLEVEL=DebugThe default is overridden with the:log-level option toTemplate::Mustache.new, or aTemplate::Mustache::Logger object can be passed via the:logger option.
my$stache= Template::Mustache.new::log-level<Trace>;my$logger= Template::Mustache::Logger.new::level<Debug>;my$stache= Template::Mustache.new::$logger;
Either method can be used with the.render method, as well.
my%data=hello=>'world';Template::Mustache.render:'Hello, {{hello}}!',%data,:log-level<Trace>;my$logger= Template::Mustache::Logger.new::level<Debug>;Template::Mustache.render:'Hello, {{hello}}!',%data,:$logger;
By default, any messages at levelWarn or worse are logged with thewarn routine. ACONTROL block can handle such warnings if needed; seeLanguage/phasers for details. Less severe messages (Info and up) are logged with thenote routine.
The routine can be set per log level, in theTemplate::Mustache::Logger.routines hash.
# Use say instead of note for Info and up; the more severe# levels (C<Warn> down to C<Fatal>) still use the warn routinemy$stache= Template::Mustache.new::log-routine(&say);# But even those can be set explicitly$stache.logger.routines{$_}=&diefor <Warn Error Fatal>;$stache.render:'{{missing}}', {};# dies
multi method log(Exception $exception, LogLevel :$level)multi method log(LogLevel :$level, *@msgs)
Emit a message at$level (Info by default).
Support forhogan.js-styletemplate inheritence is available.
Specify:pragma<KEEP-UNUSED-VARIABLES> to eitherTemplate::Mustache.new or.render, and any variables which are not defined in the data context will be kept in the rendered text. Seet/13-pragmas.t for examples.
The Mustache spec provides a wealth of examples to demonstrate exactly how the format behaves.
https://github.com/mustache/spec/tree/master/specs/
All of the official Mustache spec tests pass. A copy of the tests is distributed int/specs.
To check against the official specs repository, clone it into../mustache-spec:
git clone --depth=1 https://github.com/mustache/spec.git ../mustache-specprove -v -e 'raku -Ilib' t/The test filet/specs/inheritable_partials.json is taken fromgroue/GRMustache.
There are many, many Mustache implementations in various languages. Some of note are:
The original Ruby versionhttps://github.com/defunkt/mustache
Twitter's hogan.jshttps://github.com/twitter/hogan.js
mustache.javahttps://github.com/spullara/mustache.java
GRMustache (Objective C)https://github.com/groue/GRMustache
mustache.phphttps://github.com/bobthecow/mustache.php
full object support (with method calls; currently the object is just stringified)
global helpers (context items that float at the top of the stack)
database loader
pragmas (FILTERS?)
About
Raku library for the Mustache template format
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.