njshp

NJSHP - A simple Javascript Hypertext Preprocessor.


Keywords
njshp, hypertext, compiler, preprocessor, node, nodejs, template, templates
License
MIT
Install
npm install njshp@2.0.1-0

Documentation

NodeJS Hypertext Preprocessor

NHP

NHP is a NodeJS based Hypertext Preprocessor. Compiles your templates using eval function.

You can compile any plain text (Intended to be used alogn with HTML).


Njshp takes a .njshp file and processes it's Javascript code.

Example

Imagine that you have this index.njshp

<&
$scope.myAwesomeFunction = (a, b) => {
  return a+b;
};
&>
<!DOCTYPE html>
<html>
  <head>
    <title>
      <& echo("Hello world!"); &>
    </title>
    <body>
      <& echo("<h1>Hello there!</h1>"); &>
      <hr>
      <& echo(`3 + 4 = ${$scope.myAwesomeFunction(3, 4)}`); &>
    </body>
</html>

The client will only receive this:

<!DOCTYPE html>
<html>
  <head>
    <title>
      Hello world!
    </title>
    <body>
      <h1>Hello there!</h1>
      <hr>
      3 + 4 = 7
    </body>
</html>

Usage

In your NodeJS server script you should import njshp

const njshp = require('njshp');

Then use the .parse(data) method to parse the template file:

let template = njshp.parse(data.toString());

njshp.parse(data : String [, params : Object]) : TemplateData

This will return an object with two fields: data and scope:

  • data: Compiled data from the template.
  • scope: Variables set within the template.

The system exposes two variables to the templates:

  • $scope
  • $global

Where $scope remains only visible for the template and $global is maintained across all the server execution.

$global is intended to hold data like the session and cross-page stuff.

$scope may be userfull to store variables, functions and inject parameters like a POST or GET request data.