/dist/bin.js
/env.sh
/js_template-*.tgz
+/package-lock.json
/rollup.config.*
/src
/test
/test.js
+/yarn.lock
+/yarn-error.log
# JavaScript Template system
+An NDCODE project.
+
## Overview
-The `js_template` package exports a single function `js_template(root, dirname,
-pathname)`, which loads the given file from disk, parses it for a superset of
-JavaScript (ES6) which can contain HTML-like constructs, and then generates
-equivalent plain JavaScript code which in turn, can generate the desired HTML.
+The `jst` package exports a single function `jst(root, dirname, pathname)`,
+which loads the given file from disk, parses it for a superset of JavaScript
+(ES6) which can contain HTML-like constructs, and then generates equivalent
+plain JavaScript code which in turn, can generate the desired HTML.
The system is inspired by Pug (formerly Jade) templates, but emphasizing the
JavaScript, for instance you do not need a `-` sign at the start of each line,
## File management
-The `js_template()` function gives a "point and shoot" interface which makes it
+The `jst()` function gives a "point and shoot" interface which makes it
very easy to manage on-disk templates. You essentially just state the pathname
to the template, which is normally a `*.jst` file, and the template will be
parsed, converted to JavaScript, evaluated, and also cached for future use.
-What the `js_template()` function returns depends on what the template returns.
+What the `jst()` function returns depends on what the template returns.
Template exports are similar but slightly different to CommonJS module exports,
-as noted in examples above. Usually, `js_template()` returns a JavaScript
+as noted in examples above. Usually, `jst()` returns a JavaScript
function, which you call to generate HTML each time the page is to be served.
The HTML-generating function is re-useable in this way for efficiency reasons,
}
```
Suppose the above is saved as `/hello/index.html.jst` under your document root
-`/var/www/html`. You call `js_template(root, dirname, pathname)`, like this:
+`/var/www/html`. You call `jst(root, dirname, pathname)`, like this:
```
-let template_func = js_template(
+let template_func = jst(
'/var/www/html',
'/var/www/html/hello',
'index.jst'
## Memory vs disk caching of templates
-Templates compiled using `js_template()` are cached in memory, as long as the
+Templates compiled using `jst()` are cached in memory, as long as the
same `node` interpreter is running, so that they can be retrieved using either
-`js_template()`, or equivalently `_require()` inside a template, and they will
-not be re-executed. In the above example, if you call `js_template()` twice,
+`jst()`, or equivalently `_require()` inside a template, and they will
+not be re-executed. In the above example, if you call `jst()` twice,
you get the same object twice (it is a 2-argument function returning `String`).
As well as this, the compiled templates are also cached on disk, which requires
## GIT repository
The development version can be cloned, downloaded, or browsed with `gitweb` at:
-https://git.ndcode.org/public/js_template.git
+https://git.ndcode.org/public/jst.git
## License
## Contributions
-We would greatly welcome your feedback and contributions. The `js_template` is
+We would greatly welcome your feedback and contributions. The `jst` is
under active development (and is part of a larger project that is also under
development) and thus the API is considered tentative and subject to change. If
this is undesirable, you could possibly pin the version in your `package.json`.
* IN THE SOFTWARE.
*/
-let BuildCache = require('build_cache')
+let BuildCache = require('@ndcode/build_cache')
let acorn = require('./dist/acorn')
let astring = require('astring')
let fs = require('fs')
let fs_writeFile = util.promisify(fs.writeFile)
let build_cache = new BuildCache()
-let js_template = async (root, dirname, pathname, args) => {
+let jst = async (root, dirname, pathname, args) => {
while (pathname.charAt(0) === '/') {
dirname = root
pathname = pathname.slice(1)
let arg_names = ['_require', '_html_escape', '_root', '_dirname']
let arg_values = [
- async pathname => js_template(root, dirname, pathname, args),
+ async pathname => jst(root, dirname, pathname, args),
html_escape,
root,
dirname
)
}
-module.exports = js_template
+module.exports = jst
{
- "name": "js_template",
+ "name": "@ndcode/jst",
"version": "0.1.0",
- "description": "JavaScript HTML template engine",
+ "description": "JavaScript Templates for HTML.",
"keywords": [
"template",
"HTML",
"homepage": "https://www.ndcode.org",
"repository": {
"type": "git",
- "url": "https://git.ndcode.org/public/js_template.git"
+ "url": "https://git.ndcode.org/public/jst.git"
},
"bugs": {
"email": "nick@ndcode.org"
},
- "main": "js_template.js",
+ "main": "jst.js",
"engines": {
"node": ">=0.4.0"
},
"directories": {},
"dependencies": {
+ "@ndcode/build_cache": "^0.1.0",
"astring": "^1.3.1",
- "build_cache": "^0.1.0",
"html-escape": "^2.0.0",
"rollup": "^0.45.0",
"rollup-plugin-buble": "^0.16.0"
+++ /dev/null
-async (_env, _out) => {
- html.'cls-1'.cls-2#id-1(lang=_env.lang true=_env.val something-else="\"val\"") {
- head {}
- body {
- `hello
- & goodbye
- `
- script {
- for (let i = 0; i < 100; ++i)
- console.log(i)
- }
- let page1 = await _req('test/page1.jst')
- await page1(_env, _out)
- }
- }
-}
+++ /dev/null
-async (_env, _out) => {
- 'page3\n'
-}
+++ /dev/null
-/*
- * Copyright (C) 2018 Nick Downing <nick@ndcode.org>
- * SPDX-License-Identifier: MIT
- *
- * 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.
- */
-
-let js_template = require('./js_template')
-
-;(
- async () => {
- let page = await js_template('.', 'page.jst')
- out = []
- await page({lang: 'en', val: '<html>'}, out)
- console.log(out.join(''))
- }
-)()
+++ /dev/null
-async (_env, _out) => {
- let page2 = await _req('page2.jst')
- let page3 = await _req('/page3.jst')
- 'page1\n'
- await page2(_env, _out)
- await page3(_env, _out)
-}
+++ /dev/null
-async (_env, _out) => {
- 'page2\n'
-}