npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ktl

v0.7.0

Published

Koder's Template Language

Downloads

21

Readme

Koder's Template Language

Join the chat at https://gitter.im/lekoder/ktl Build Status Coverage Status npm npm npm

KTL is a parser factory.

String templating system inspired by doT. It parses template string into javascript function, which in turn can be called with data to return parsed string.

It returns verbatim string (no escaping, etc) makin it useful for generation of configuration files.

It should be augmented with HTML escaper when using on web. Escaping is not implemented by design.

Usage

var output = ktl(template)(data,[sanitizer]);

Supported tags:

Evaluation

All values support complete JavaScript notation. You can freely use operators, methods, global scope objects (ie. Math), etc.

|Tag | Meaning |---------------------------|-------------------------------------------------------------- |{{ prop }} | Selected property of object passed to parser |{{ prop.sub }} | Subproperties can be accessed with dot notation |{{ method() }} | Methods can be called |{{ value.toFixed(2) }} | Methods of properties can also be called |{{ prop ? prop : '-' }} | All operators are available (in this case: default to '-') |{{ _ }} | Verbatim object passed to parser, cast to string. Useful in iterations. |{{ _.toFixed(4) }} | Methods can also be called on verbatim objects

Iteration

Iteration starts with {{# <array> }} and ends with {{#}}. Iterations can be nested. String between {{# <array> }} and {{#}} is treated as new template. Verbatim evaluation ({{ _ }}) is useful for arrays of primitives. $ is available as index inside iteration.

|Tag | Meaning |---------------------------|-------------------------------------------------------------- |{{# array }} | Iterate over array passed as {array:[]} to parser |{{# _ }} | Verbatim iteration (when passing [] to parser)

Condition

Condition starts with {{? condition }} and ends with {{?}} with an optional else {{:}}.

|Tag | Meaning |-------------------------------------|-------------------------------------------------------------- |{{? bool }} true {{?}} | Simple condition |{{? bool }} true {{:}} false {{?}} | Condition with else

Sanitizing output

If parser built with KTL is called with function as last argument, it will use this function to sanitize all evaluations. Sanitizer is a function that takes a data value and returns sanitized string.

This feature can be used to use KTL as HTML parser.

Examples:

Template:

Hi {{ name }}! You have {{ messages.length || 'no' }} new messages.
{{# messages }}
    {{ title.toUppercase() }}: from {{ from }}
{{#}} 

Data:

{
    "name":"koder",
    "messages": [
        { "title": "message 1", "from": "koder" },
        { "title": "message 2", "from": "dekoder" }       
    ]
}   

Output

Hi koder! You have 2 new messages.

MESSAGE 1: from koder

MESSAGE 2: from dekoder

Why KTL?

Most templating languages are web-centric. I required a templating language which works on strings without assuming what those strings will be used for.

Design choices

  • KTL gives you parser for a string, which you can call with an object to get something of it.
  • It has no dependencies.
  • It does not care what do you use this string for.
  • It returns verbatim string if there is no data.
  • Template caching is out of it's scope. You can cache parsers if you like.
  • Escaping string is also out of it's scope.

Alternatives

  • doT is an excellent templating language with blazing-fast implementation and it was inspiration to write KTL. It is however www-centric and it notation is not that useful.

  • mustache is another brilliant templating language, but it still requires specific {{{ tag }}} to avoid escaping.