Nanotemplatejs
Nano templating engine for Javascript strings and DOM Elements
Install / Use
/learn @ruidlopes/NanotemplatejsREADME
Nano Template JS
This tiny Javascript library provides a small addition to the string and DOM Element objects that allows for simple templating features.
Usage
This library provides a single function, t (and an alias named template) that can be executed on Javascript strings. Templates are identified by the $ marker, followed either by a number starting from 0 (e.g., $2) or a word (e.g., $foo).
Positional parameters
The easiest way to use this library is passing the values to be replaced directly as function arguments:
string.t(value1, value2, value3, ...)
Example:
"test $0 $1".t("one", "two") returns "test one two"
Named parameters
Since arguments have implicit assignment to their position, this library provides word-based templates, which should be passed as simple Javascript objects:
string.t(object)
Example:
"<a href='$url'>$text</a>".t({url:"http://foo.com", text: "foo"}) returns "<a href='http://foo.com'>foo</a>"
Looping
This library also provides looping capabilities, which can be useful for applying a single template to a set of values. This is achieved by passing a Javascript array as a single parameter:
string.t(array)
Example:
"test $0 ".t(["one", "two"]) returns "test one test two "
DOM templates
Apart from extending string objects, this library allows for direct manipulation of DOM elements, following the same usage pattern:
element.t(parameters)
Example:
Assuming the following excerpt of an HTML document:
<p id='my-element'>$text</p>
The execution of the following Javascript snippet
document.getElementById('my-element').t({text: "lorem ipsum ..."})
will transform the respective DOM element into:
<p id='my-element'>lorem ipsum ...</p>
Note: this function can be applied several times to a DOM Element, since DOM Element templates are cached.
Additional examples
More example usage can be found in the test directory of this project:
assertions.jsprovides insights on the usage of this library, especially in the context of server-side Javascript;assertions.htmlprovides some usage scenarios to apply this library directly within HTML documents.
Cautions
This library extends the functionalities provided by the String and DOM Element objects. Use with caution, since it might break some libraries/scripts.
Related Skills
node-connect
348.5kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
348.5kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
348.5kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
