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

haje

v0.1.1

Published

HTML As Javascript for Express. A view engine.

Downloads

2

Readme

Overview

HAJE (HTML As Javascript Engine) is the lightest rendering engine for Express or standalone use. Unlike other rendering systems, like pug or ejs, the views are written in pure javascript and compiled when launching the application server.

A simple example

 
const express=require("express")    
const haje=require('../../haje')    // First we need to include the HAJE module
eval(haje.tagFunctions())           // then bring the HTML tag functions into current module space

app=express()
  .get('/example1', function(req,res) {
    let s =  
      $document([                   // Create a top-level document objects.
        $h1("It works !")           // Then add children.
      ])
    res.end(s.toHTML())             // Finally send to the response.
  })
  .listen(3000)

En example using rendering ending interface

The server


const express=require("express")    
const haje=require('../../haje')    // First we need to include the HAJE module

example2View = require('./views/example2.js').myView

app=express()
  .use(haje.middleware)             // Using the middleware add the _render_ method to response object
  .get('/example1', function(req,res) {
    let s =  
      $document([                   // Create a top-level document objects.
        $h1("It works !")           // Then add children.
      ])
    res.end(s.toHTML())             // Finally send to the response.
  })
  .get('/example2', function(req,res) {
    res.render(example2View, {name: "Your name here"}) 
  })  
  .listen(3000)

The view

const haje=require('../..')   // Import haje module
eval(haje.tagFunctions())     // Import tag functions into this module


module.exports.myView = function(data){
  return $document([                                                    // Top level is perferably a document
      $doctype("html"),                                                 // Specify a doctype
      $head(),
      $body([                                                           // Body
        $h1({style:"border-bottom: 1px solid silver" },                 // Use of HTML attributes
                "Example2: simple view"),                                   
        $p(`This demonstrates a simple HTML page with ${data.name}`)    // Including data from the view parameters
      ])
  ])
}  

How-to

Using the rendering engine with your application server

You need to first use the middleware that will add render method to the response object:

app.use(haje.middleware)

In the file that contains your page controller, include the view (here we use the "views" folder):

example2View = require('./views/example2.js').myView

In your page you would use the view as a parameter when calling render method:

  .get('/example2', function(req,res) {
    res.render(example2View, {name: "Your name here"}) 
  })  

Finally you need to create the example2.js view file in views folder:

const haje=require('../..')   // Import haje module
eval(haje.tagFunctions())     // Import tag functions into this module


module.exports.myView = function(data){
  return $document([                                                    // Top level is perferably a document
      $doctype("html"),                                                 // Specify a doctype
      $head(),
      $body([                                                           // Body
        $h1({style:"border-bottom: 1px solid silver" },                 // Use of HTML attributes
                "Example2: simple view"),                                   
        $p(`This demonstrates a simple HTML page with ${data.name}`)    // Including data from the view parameters
      ])
  ])
}  

Pass data to the view Data can be passed to the view in the second parameter when calling render. The type must match what the view expects:

In the controller you would write (not the name passed in the data object):

  .get('/example2', function(req,res) {
    res.render(example2View, {name: "Your name here"}) 
  })  

The view is responsible for processing the data passed as a single parameter:

module.exports.myView = function(data){                                 // Don't forget the formal parameter here
  return $document([  
      $doctype("html"), 
      $head(),
      $body([ 
        $h1({style:"border-bottom: 1px solid silver" }, 
                "Example2: simple view"),                                   
        $p(`This demonstrates a simple HTML page with ${data.name}`)    // Including data from the view parameters
      ])
  ])
}