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

node-tpl

v0.0.6

Published

Generates templates in a Smarty like fashion

Downloads

43

Readme

node-tpl is an easy to use, fast, templating tool to template anything you want, from webpages to emails.

Basic usage

Add placeholder items

var tpl = require('tpl');

tpl.assign("musketeers", {
    "Athos": {color: "red", age: "20"},
    "Aramis": {color: "green", age: "30"},
    "Porthos": {color: "blue", age: "12"},
    "D'Artagnan": {color: "black", age: "25"}
});

tpl.display("templates/myfile.tpl");

Create the html template

<!doctype>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" href="/css/main.css">
        <title>Template Demo</title>
    </head>
    <body>
        <p>
            {if new Date().getHours() >= 12}
                Good afternoon!
            {else}
                Good morning!
            {/if}
        </p>
        <ul>
            {for $row in $musketeers}
                <li>
                    {$row}
                    <ul>
                        <li>Age: {$row.age}</li>
                        <li>Favorite Color: {$row.color}</li>
                    </ul>
                </li>
            {/for}
        </ul>
    </body>
</html>

API Documentation

Set the current directory

Files are loaded based on the current working directory (cwd). By default the cwd is set to __dirname. This can be changed by setting it with setcwd without a trailing /.

tpl.setcwd("/path/to/default");

Files that are included within the markup are also loaded based on the cwd.

Variables

Variables are items that link to a variable within the template markup, they are added by using the assign method.

Note: Assigning a variable 2 or more times will overwrite the previous variable.

tpl.assign("variable", "My variable value");
tpl.assign("variable", ["Red", "White", "Blue"]);

Variables can hold an array of items, including (but not limited to):

  • strings
  • integers
  • objects
  • arrays

Fetch

Fetch allows you to fetch a template and replace its values; it then gets returned back to the script. With the value you can do anything you please, such as send it in an email.

tpl.assign("name", {first: "Billy", last: "Bob"});
var body = tpl.fetch("templates/email.tpl");
console.log(body);

Display

Display allows you to fetch a template and it automatically gets displayed through process.stdout.write.

tpl.assign("name", {first: "Billy", last: "Bob"});
tpl.display("templates/page1.tpl");

Markup Documentation

Variables

Variables are prefixed with a $ and are between { and }. To access an array/object item use . to get the value from array/object.

{$variable}
{$variable.array_item}
{for $var in $variable}
{if $var == $another_var}
{elseif $var == $another_var}

If statements

If statements are blocks that perform tests on a list of statements. Once one validates as true its contents will be displayed.

<p>
    {if 1 == 2}
        This should never be displayed.
    {elseif '$color' == 'blue'}
        This will show if color equals "blue".
    {elseif new Date().getHours() >= 12}
        Good afternoon!
    {else}
        If nothing worked then you are seeing this.
    {/if}
</p>

If it is after hour 11, and $color does not equal blue, and well 1 never equals 2, we would then get this output:

<p>
    Good afternoon!
</p>

For statements

For statements loop through a list of array items or an object and display them on a page.

<div class="column">
    {for $row in $store_items}
        <div class="row">
            <h2><a href="/item/{$row.id}">{$row.title}</a></h2>
            <p>{$row.description}</p>
            <p><small>{$row.updated}</small></p>
        </div>
    {/for}
</div>

We might then get something like this as output:

<div class="column">
    <div class="row">
        <h2><a href="/item/1">White Socks</a></h2>
        <p>Cotton white socks</p>
        <p><small>2015/01/01</small></p>
    </div>
    <div class="row">
        <h2><a href="/item/2">Blue Socks</a></h2>
        <p>Cotton blue socks</p>
        <p><small>2015/01/25</small></p>
    </div>
    <div class="row">
        <h2><a href="/item/3">Black Socks</a></h2>
        <p>Silk black socks</p>
        <p><small>2015/02/12</small></p>
    </div>
</div>

Includes

Files can be included using the include command and included files can also include other files.

{include "templates/nav.tpl"}
{include "templates/$filename"}

Note: Any file that includes itself or a parent file will probably create an endless loop. Note: Currently a variable followed by a an extention such as $filename.tpl will break the code.

Literals

A literal is a way to force the engine not to replace markup, because sometimes you don't always want to replace blocks, such as JavaScript in a browser, because they can sometimes mean the same.

For example, take this template markup:

<script>
    $(".rows").each(function(){$(this).remove()});
</script>

When the template markup is replaced, you will get this new javascript:

<script>
    $(".rows").each(function());
</script>

Not only is it invalid JavaScript, but it isn't what you wanted.

We can fix that by placing {literal}'s around that block of code and it won't change like this:

{literal}
<script>
    $(".rows").each(function(){$(this).remove()});
</script>
{/literal}

Now what gets output is exactly what we wanted because we told the engine not to replace it.

<script>
    $(".rows").each(function(){$(this).remove()});
</script>

Changelog

  • 0.0.1 - 4/10/15
    • Initialization
  • 0.0.2 - 4/10/15
    • Documentation update
  • 0.0.3 - 4/11/15
    • Added support for if/elseif/else
  • 0.0.4 - 4/11/15
    • Documentation update
    • Enhancements
    • Added extra npm tags
  • 0.0.5 - 4/11/15
    • Added support for literal's
    • Added npm repository
  • 0.0.6 - 4/11/15
    • Added file include support
    • Documentation update