Coder Social home page Coder Social logo

jst's Introduction

JST - Template for JavaScript - Proposal of Jade like template engine (v0.3)

with:

  1. Easily identifiable block with brackets,
  2. Easily identifiable JavaScript expressions with AngularJS 2 expression syntax,
  3. Easily switch between template and JavaScript expression, and
  4. Build in ES2015 support.

Processing Phases

The processing of jst has three phases:

  1. Expansion phase

The {{ }} block will be transpiled to a function, so variables are local. And then the function will be executed immediately, emitting new parts of jst.

  1. Parsing phase

The whole jst then parsed to AST.

  1. Generating phase

Generator generates HTML from the AST. Apply hooks if registered.

Sample

Here is a simple example, see document source [index.jst] for full examples and generated [document] for reference.

doctype(html)

html {
	head {
		// switch to JavaScript expression mode with {{expr}} syntax.
		// the JavaScript expression also called "jst script".
		// the jst script can be processed by plugins, e.g. babel.
		{{
			global.href = '//github.com/amobiz/jst';
			global.title = 'Template for JavaScript - Proposal of Jade like template engine (v3)';
		}}

		/*
		the above code then being transipled to an IIFE, so variables are local:

		(function (global) {
			global.href = '//github.com/amobiz/jst';
			global.title = 'Template for JavaScript - Proposal of Jade like template engine (v3)';
		})(context.global);
		*/

		// inline scripts are first treated as text with JavaScript expression interpolation,
		// and then can be processed using plugins, e.g. babel.
		script {
			var href = {{ global.href }};
			var title = {{ global.title }};

			function onclick(e) {
			}
		}

		// use variable defined in the above jst script section.
		// in template, use JavaScript expressions / variables with {{ expr }} syntax
		title {{ global.title }}
		// put attributes in () parenthesis, omit () parenthesis when no attributes
		link(href=styles.css rel=stylesheet)
		style(href=styles.css)	// short hand for above
		icon(href=favicon.ico)	// same as: link(href=favicon.ico rel=icon)

		// external script
		script(src=main.js)
	}
	body {
		// directives starts with '@' character.
		// unless there is blank space characters, '' or "" can be omitted.
		@import header.jst

		header {
			h1 {
				a(href={{ global.href }} target=_blank) {{ global.title }}
			}
		}

		// add id with '#' character; add class with '.' character;
		section.header {{ global.title }}

		{{
			var user = {
				name: 'guest',
				description: 'foo bar baz',
				nested: {
					key: {
				}}	// this should also work without problem.
			};
			var items = [{
				name: 'apple',
				item: 3
			}, {
				name: 'orange',
				item: 5
			}];

			var styles = {
				color: '#333'
			};

			{{
				section#content.container(style={{ styles }}) {
					h2 {{ user.name }}
					p {{ user.description }}

					// skip template {} bracket if the only inner item is JavaScript expression.
					// you can think of `{{}}` as a way to switch between template and JavaScript expression.
					ul {{	// switch to js expression.
						for (name, item in items) {{	// switch to template expression.
							// AngularJS 2.0 like event handler.
							li(id={{ name }} class={{ item.active ? 'active' : '' }} (click)={{ onclick }}) {
								{{ name }}: {{ item }}
							}
						}}
					}}
				}
			}}
		}}

		@import footer.html
	}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.