Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ajs

Package Overview
Dependencies
Maintainers
0
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ajs

Experimental asyncronous templating in Node

  • 0.0.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
65
decreased by-32.29%
Maintainers
0
Weekly downloads
 
Created
Source

AJS

AJS is an experimental asyncronous templating language for Node.

NOTE: While AJS includes Connect middleware, it's currently NOT compatible with the ExpressJS view system due to its synchronous handling of template engines and responses.

Installation

$ npm install ajs

Usage

AJS includes Connect middleware:

var connect = require('connect')
  , mysql   = new (require('mysql').Client)
  , ajs     = require('ajs');

mysql.user = 'dbuser';
mysql.password = 'passwd';
mysql.connect();
mysql.useDatabase('blog');
  
var getPosts = function(viewCallback) {
  mysql.query("select * from posts", viewCallback);
}

var server = connect.createServer()
                    .use(ajs({dir: './views'}))
                    .use(function(req, res) {
                      res.render('index', {title: "Blog Home", getPosts: getPosts});
                    });

views/index.ajs:

<html>
  <head>
    <title><%= title %></title>
  </head>
  <body>
    <h1><%= title %></h1>
    <div id="posts">
      <% getPosts(function(err, posts) {
        if(posts) {
          posts.forEach(function(post) { %>
            <div class="post">
              <h3><a href="#"><%= post.title %></a></h3>
              <%- post.body %>
            </div>
          <%});
        } else { %>
          An error occured while trying to load the posts.
        <% }
      }) %>
    </div>
  </body>
</html>

For lower-level access to an AJS template, simply require it, call it with a locals object template(<locals>), and bind to its data, error and end events.

var template = require('views/index');
template({title: 'Hello World!'}).on('data', function(data) {
  console.log(data);
});

Syntax

index.ajs:

<html>
  <head>
    <title><%= 'Hello World' %></title>
  </head>
  <body>
  
    <!-- AJS is a superset of javascript, so things like 
         variable assignment just work as expected -->
  
    <% var async2 = function() { %>
    <div><%= 'async 2 done' %></div>
    <% } %>
  
    <% if(10 == (5 + 5)) { %>
    <h1>Hello world.</h1>
    <% } %>

    <% for(i=1; i<5; i++) { %>
      <%= "next: " + i  + "<br/>" %>
    <% } %>

    <!-- callbacks are flushed to the proper location in the template
         when they return, but they can't be nested -->
    
    <p>
      <% setTimeout(function() { %>
      <%= 'async 1 done' %>
      <% }, 10 ) %>
    </p>
  
    <!-- some native syncronous callbacks are exempt from the
         nested callback restriction. -->
    
    <ul>
      <% ['one', 'two', 'three'].forEach(function(item) { %>
        <% ['nested'].forEach(function(item2) { %>
        <li><%= item %></li>
        <% include('partials', {item: item2}) %>
        <% }); %>
      <% }); %>
    </ul>
  
    <!-- named callback functions work too.
         a callback's output is inserted into the template at the 
         spot where it was passed to its async function -->
  
    <p> <% setTimeout(async2, 100) %> </p>

    <!-- callbacks can be used multiple times -->
  
    <% setTimeout(async2, 100) %>
  
    <!-- other AJS partials can be embedded using the "include" function -->
  
    <% include('partials/message', {text: "Hello world!"}) %>
  
    <p><%= 'any statement can be printed - ' + (6 + 6) %></p>
  </body>
</html>

partials/message.ajs:

<div><%= text %></div>

Annotated Source

http://kainosnoema.github.com/ajs

Authors

  • Evan Owen

License

(The MIT License)

Copyright (c) 2011 Evan Owen <kainosnoema@gmail.com>

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.

Keywords

FAQs

Package last updated on 08 Jun 2011

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc