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

template-tal

v0.2.9

Published

XML Lightweight Template Attribute Language implementation for Javascript

Downloads

128

Readme

template-tal

TAL Implementation for nodejs. TAL is an XML templating language. template-tal is suitable for node.js or client-side, and has no dependancies.

** UPDATE ** as from 0.2.0, template-tal supports invoking synchronous & asynchronous functions from the template expressions, see below.

SYNOPSIS

In your Javascript code:

let tal  = require ('./lib/template-tal')
let xml = `
  <html>
    <body>
      <p tal:content="foo">Dummy Content</p>
      <p tal:content="bar()">Dummy Content</p>
      <p tal:content="baz()">Dummy Content</p>
    </body>
  </html>
`

let data = {

  // value
  foo: "FOO",

  // function
  bar: function() { return "BAR" },

  // async function
  baz: function() {
    return new Promise(function(resolve) {
      setTimeout(function() { resolve('BAZ')}, 100)
    })
  }
}

tal
  .process(xml, data)
  .then(function(result){
    console.log(result)
  })

It should produce something like this:

  <html>
    <body>
      <p>FOO</p>
      <p>BAR</p>
      <p>BAZ</p>
    </body>
  </html>

SUMMARY

Template Attribute Language https://en.wikipedia.org/wiki/Template_Attribute_Language is an amazingly clever specification on how to template XML documents. It does so simply by adding extra attributes to your XML Template file.

The specification has been implemented in many languages, including Javascript, but the two existing Javascript implementation either depend on external libraries (jstal/E4X) or work using the DOM (distal).

template-tal does neither. It is a completely self-contained library, suitable for node.js, client side applications and / or embedded apps. It is a port of the Perl Petal::Tiny package. http://search.cpan.org/~jhiver/Petal-Tiny-1.03/

This Readme hence steals a lot of its documentation and explains the differences between the two modules.

NAMESPACE

template-tal has no namespace support, you must use the tal: prefix throughout.

KICKSTART

Typically, you may have a data structure looking like this:

var data = {
    user: "Bob",
    getBasket: fetchBasketFunction,
};

Where fetchBasketFunction() would either return a list of fruits, or a Promise object which should resolve to a list of fruits, like ['apple', 'banana', 'kiwi', 'mango']

You want your end result to look like this:

<body>
  <h1>Bob</h1>
  <p>Basket List:</p>
  <ul>
    <li>apple</li>
    <li>banana</li>
    <li>kiwi</li>
    <li>mango</li>
  </ul>
</body>

So let's work this out. First let's replace the username "Bob":

<h1 tal:content="user">Bob</h1>

Now lets work out the list:

<ul>
  <li tal:repeat="item getBasket()"
      tal:content="item">some fruit goes here</li>
</ul>

Our template now looks like:

<body>
  <h1 tal:content="user">Bob</h1>
  <p>Basket List:</p>
  <ul>
    <li tal:repeat="item getBasket()"
        tal:content="item">some fruit goes here</li>
  </ul>
</body>

Ah! But what is happening if our basket is empty? Then we'd get the following output:

<body>
  <h1>Bob</h1>
  <p>Basket List:</p>
  <ul>
  </ul>
</body>

Which is not very elegant. Thus we change the template as follows:

<body tal:define="basket getBasket()">
  <h1 tal:content="user">Bob</h1>
  <div tal:condition="true:basket">
    <p>Basket List:</p>
    <ul>
      <li tal:repeat="item basket"
          tal:content="item">some fruit goes here</li>
    </ul>
  </div>
  <div tal:condition="false:basket">
    <p><span tal:replace="user">SomeUser</span>'s basket is empty :-(.</p>
  </div>
</body>

Let's explain what we did:

  1. We used tal:define to assign the list of fruits to a variable called basket using a javascript expression, getBasket(), which can either return the desired value or a Promise to resolve to the desired value.

  2. We used tal:content to replace the content of a tag with a javascript expression. This implementation of the TAL spec evals Javascript directly, so you can do things like <theAnswer tal:content="21*2" />

Which would output

<theAnswer>42</theAnswer>

Your expression must return something or a Promise of something.

  1. We then used tal:condition in conjunction with the "true:" modifier to either display a list if the list is populated, or display a message describing an empty basket.

  2. Finally, we used tal:repeat to iterate through each item of the basket.

You can find a TAL reference at http://www.owlfish.com/software/simpleTAL/tal-guide.html, and this library tried to implement most of it.

RESTRICTIONS:

SUPPORTED TAL STATEMENTS:

<p tal:on-error="string:sorry, could not fetch user.">
  <b tal:content="database.getUser().name">replace me</b>
</p>
<p tal:define="user database.getUser()">
  <span tal:replace="user.name" />
</p>
<p tal:condition="true:stuff">
  stuff is true, let's do something.
</p>
<p tal:condition="false:stuff">
  stuff is false, let's do something else.
</p>
<table>
  <th>
    <td>username</td>
    <td>email</td>
  </th>
  <tr tal:repeat="user users()">
    <td tal:content="user.login()">login</td>
    <td tal:content="user.email()">email</td>
  </tr>
</table>

Note that tal:repeat also auto creates the following variables, which you can access from within your repeat block.

<p tal:content="someStuff">I will be replaced</p>
<p tal:replace="someStuff">I will be replaced, including the p tag.</p>
<a href="#" alt="desc"
   tal:attributes="href url.href; alt url.desc"
   tal:content="url.label">SomeLabel</a>
<em tal:omit-tag="false:important">I may be important.</em>

Note that tal:omit-tag="" ALWAYS strips the tag.

EXPRESSIONS

structure keyword

Prefixing an expression with the keyword structure will make it not escape HTML when replacing the output. This is handy when you are using variables which themselves return HTML.

<!-- bad, will be double encoded -->
<span tal:content="getSomeHTML()">replace me</span>

<!-- yup, that should work -->
<span tal:content="structure getSomeHTML()">replace me</span>

template keyword

Same as structure, except the resulting expression is expected to be a template. Useful for importing template fragments and even building recursive templates such as sitemaps.

<span tal:content="template getSomeTALTemplate()">replace me</span>

true:EXPRESSION

If EXPRESSION returns an array reference
  If this array reference has at least one element
    Returns TRUE
  Else
    Returns FALSE
Else
  If EXPRESSION returns a TRUE value (according to Javascript 'trueness')
    Returns TRUE
  Else
    Returns FALSE

the true: or false: modifiers should always be used when using the tal:condition statement.

false:EXPRESSION

I'm pretty sure you can work this one out by yourself :-)

string:STRING

The string: modifier lets you interpolate tal expressions within a string and returns the value.

IMPORTANT NOTE string interpolation does not support asynchronous expressions which returns Promises. You can always get the async value in another variable using tal:define and then interpolate that.

string:Welcome ${user.realName()}, it is ${myLocalTime()}!

Writing your own modifiers is easy:

tal.MODIFIERS.uppercase = function (expr, self, callback) {
  resolve(expr, context, function(error, arg) {
    if(error) { return callback(error) }
    if (typeof arg === 'string') { string = string.toUpperCase() }
    return callback(null,string)
  })
}

Then in your template:

<p tal:content="uppercase:database.getUser().name">hello</p>

EXPORTS

tal.process(), tal.MODIFIERS

BUGS

If you find any, please drop me an email - jhiver (at) gmail (dot) com. Patches are always welcome of course.

SEE ALSO

This library is a port of the Perl package Petal::Tiny, which I also wrote.

Jean-Michel Hiver - jhiver (at) gmail (dot) com

This module free software and is distributed under the same license as node.jis itself (MIT license)

Copyright (C) 2018 - Jean-Michel Hiver

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.