Templating Engine
Go to file
nicco 9bbdff6d74 Drone CI 2018-02-25 14:10:38 +01:00
dist Dist Render 2018-02-21 12:30:59 +01:00
examples wrong include 2018-02-22 10:38:01 +01:00
src Support for brakets 2018-02-21 12:30:43 +01:00
test Testing 2018-02-21 19:39:15 +01:00
.drone.yml Drone CI 2018-02-25 14:10:38 +01:00
.gitignore Ignroe Files 2018-02-22 10:33:01 +01:00
.npmignore Packaging 2018-02-22 12:42:21 +01:00
.travis.yml Add Travis File 2018-02-21 19:42:44 +01:00
README.md Constructor Doc 2018-02-22 13:38:56 +01:00
package.json Packaging 2018-02-22 12:42:21 +01:00
tsconfig.json Include src and output to dist 2018-02-07 11:46:08 +01:00

README.md

Cometa

Build Status

Yet another templating engine 📠

Cometa is a templating engine with no dependencies written in JS. That was the reason it was created.

Quickstart 💥

# Go to examples
cd examples/simple

# Install
npm i

# Run
node app.js

Setup 🏗

General Import

Constructor Options

// Import
const Cometa = require('cometa')

// Initialize
const cometa = new Cometa()

Installation 🚂

npm i cometa

Run 🚀

Constructor Options

new Cometa(options, expressions)

All options and expressions are optional.

  • options (default) description
Example
new Cometa({
	views: './someDir'
}, {
	begin: '<<',
	comment: '^'
})

Options

  • views (./views) Root template folder
  • extension (html) File extension of the templates
  • encoding (utf-8) Encoding of the files

Expressions

  • begin ({{) Opening tags
  • ending (}}) Closing tags
  • comment (#) Comment char
  • inlcude (>) Include char
  • if (?) If statement char
  • if_invert (!) Invert the variable in if statement
  • for (*) For char
  • for_in (in) Divider between array and i
  • closing_tag (/) Closing tag for ifs and loops

Render template

const Cometa = require('cometa')
const cometa = new Cometa()

// Data
const template = 'index' // file called index.html in ./views
const data = {name: 'World'}
const callback = (err, html) => console.log(html)

// Do the render
cometa.render(template, data, callback)

Render file

const Cometa = require('cometa')
const cometa = new Cometa()

// Data
const data = {name: 'World'}
const callback = (err, html) => console.log(html)

// Do the render
cometa.renderFile('myDir/myTemplate.myExtesion', data, callback)

Express

const express = require('express')
const app = express()

app.set('views', `${__dirname}/views`)
app.set('view engine', 'html')
app.engine('html', require('cometa').__express)

app.get('/', (req, res) => {
	res.render('index', {
		title: 'Cometa!'
	})
})

app.listen(3000, () => console.log('Example app listening on port 3000!'))

Reference 📒

Variable

{"myVar": "ok"}
<span>{{myVar}}</span>

Comments

<div>
	{{# Me Me Comment #}}
</div>

If

True is everything that is different from:

  • undefined
  • false
  • null
  • ''
{
	"myVar": "something",
	"myVar": true,
	"myVar": [1,2,3],
	"myVar": 42,
}
{{? myVar }}
	<span>Only show me when I exist: {{ myVar }}</span>
{{/?}}

Loop

{
	"links": [
		{"id":0, "name": "One"},
		{"id":1, "name": "Two"},
		{"id":2, "name": "Three"}
	]
}
<ul>
	{{* link in links}}
		<li id="{{link.id}}">{{ link.name }}</li>
	{{/*}}
</ul>