TMPLTR comes with a bleeding edge node:http server. Getting started is very easy. To set up a static file server, create a file called `server.mjs` with the following content:
import { App } from '../src/app.mjs';
const app = new App();
app.serve();
That's it! Just put the files you want to server in a folder called `static` and start the server by typing:
node server.mjs
TMPLTR uses the latest features of node. So you may need to add the --experimental-sqlite flag. You can also use the watch feature of node to make your life easier:
node --experimental-sqlite --watch server.mjs
Now the server will reload every time a file changes.
Although the Template Engine is designed as an on-request function to generate pages in the context of the server, it can also be used as a static page generator. This is the least complex way to get familiar with the engine. The main use case is described below.
Create a file called `page-generator.mjs` and paste the following code into it:
import { App } from '../src/app.mjs';
const app = new App();
const pageContent = app.renderTemplate('static-page.html');
console.log(pageContent);
Create a file called `static-page.html' in a folder called `templates', and place this folder next to the `page-generator.mjs' folder. Add the following text to `static-page.html
<h1>Pilot</h1>
<p>Jack awakens disoriented in a jungle..</p>
Run the following command:
node page-generator.mjs
This should print the following content to your screen:
Okay, lame.. But at least it is renders. Let's add some context! Change the content of `static-page.html` to match the following:
<h1>Pilot</h1>
<h2>Episode: {{ episodeNumber }}</h2>
<p>Jack awakens disoriented in a jungle..</p>
Modify the content of `page-generator.mjs`:
import { App } from '../src/app.mjs';
const app = new App();
const pageContent = app.renderTemplate('privacy.html', { episodeNumber: 1 });
console.log(pageContent);
Run page-generator.mjs again, this should be the result:
With these changes, we have added some context to our content. The episodeNumber can now be easily passed to the template.
TMPLTR comes with an ORM that is inspired by Django's built-in ORM. The main idea is to use class methods to retrieve objects from the database and instance methods to modify those objects. A small example:
User.getAll();
// returns a list of all users
User.getByFilter({ lastName: 'Kwon' });
// returns a list of all users with the last name 'Kwon'
const user = User.getByFilter({ id: '3' })[0];
// user contains now a single User-object.
user.delete();
// deletes the user from the database