Skip to content

Commit

Permalink
Environment and environment are globals in ringo
Browse files Browse the repository at this point in the history
The constructor is still called `Environment` but the exposed
property on the `reinhardt` module is now called `Reinhardt`.

also exposing the middleware on the main reinhardt module
because that's just sane
  • Loading branch information
oberhamsi committed Dec 31, 2014
1 parent 247ce80 commit c60d63d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 29 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,27 @@ Demonstration of a small Reinhardt template:

# Quickstart

Create a templates environment and render a template:
Create a Reinhardt template environment, which loads templates from
the filesystem and render a template:

var {Environment} = require('reinhardt');
var templates = new Environment({
var {Reinhardt} = require('reinhardt');
var templates = new Reinhardt({
loader: '/path/to/templates/',
debug: true
});

var template = templates.getTemplate('index.html');
var html = template.render({"hello": "world"});

// or for convinience the template environment
// an return a 200 response with the template as the
// response body
template.renderResponse({"hello": "other world"})

For easier template debugging, add the reinhardt middleware
to your application:
to your Stick application:

app.configure(require('reinhardt/middleware'), 'route')
app.configure(require('reinhardt'), 'route')

# Speed

Expand Down
43 changes: 28 additions & 15 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
Quickstart Guide
===================

Install reinhardt with Ringo's admin command:
Install reinhardt with [rp](https://github.com/grob/rp/wiki):

$ ringo-admin install oberhamsi/reinhardt
$ rp install --global reinhardt

The most basic way to render a template is to instantiate it from a string:
Create a template from a String and render it:

>> var template = new Template('Hello {{ username}}');
>> template.render({username: 'Reinhardt'});
'Hello Reinhardt'

A templating `Environment` allows you to configure additional tags and filters,
which will be available in all templates loaded through the environment. You will
typically use an Environment for anything but very simple applications:
A Reinhardt environment
---------------

>> var env = new Environment({
A Reinhardt environment makes it easy to load templates from the filesystem.
You can also define additional tags and filters per Reinhardt,
which will be available in all templates.

The following code creates an Environment which
loads files from the "./templates" directory and has additional filters as defined
in the module "./mycustomfilters":

>> var {Reinhardt} = require('reinhardt');
>> var templates = new Reinhardt({
loader: module.resolve('./templates/'),
filters: require('./mycustomfilters')

});
>> env.renderResponse("index.html", context)
>> templates.renderResponse("index.html", context)
{"status": 200, body: ["<html>..."]}

Also see the help page on [Reinhardt environments](./environment.md) for more information.

Debugging templates
---------------------

Enable debugging in the environment and put the reinhardt middleware into your application:
During development reinhardt can display detailed template error pages with the relevant template source,
the line number and additional information such as the origin from which the template
was loaded.

>> app.configure(require('reinhardt/middleware'));
>> var env = new Environment({
For the detailed error pages, you need to add the reinhardt middleware to your [Stick application](http://github.com/ringo/stick):

>> app.configure(require('reinhardt'));

and add the debugging flag to your Reinhardt environment:

>> var templates = new Reinhardt({
debug: true
});

Debugging is disabled by default and you should not enable it in production since it displays
the source and location of your templates.
15 changes: 7 additions & 8 deletions examples/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var {Environment} = require('reinhardt');
var {Reinhardt} = require('reinhardt');
var {Loader} = require('reinhardt/loaders/filesystem');
var {Application} = require('stick');

var app = exports.app = Application();
// for easier template debugging
// include the reinhard/middleware
app.configure(require("reinhardt/middleware"), "route");
// for easier template debugging put "reinhardt" as the first middleware:
app.configure(require('reinhardt'), "route");

var context = {
links: [
Expand All @@ -15,9 +15,8 @@ var context = {
djangoImage: require('ringo/base64').encode(require('fs').read(module.resolve('./static/django-reinhardt.jpg'), {binary: true}))
}

var templates = new Environment({
loader: [module.resolve('./templates/'), module.resolve('./templates2')],
debug: true
var templates = new Reinhardt({
loader: new Loader(module.resolve('./templates/'), module.resolve('./templates2'))
});

app.get('/', function() {
Expand All @@ -35,4 +34,4 @@ app.get('/earlylife.html', function() {

if (require.main == module) {
require("ringo/httpserver").main(module.id);
}
}
3 changes: 2 additions & 1 deletion lib/reinhardt.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
exports.Environment = require('./environment').Environment;
exports.Reinhardt = require('./environment').Environment;
exports.middleware = require('./middleware').middleware;

0 comments on commit c60d63d

Please sign in to comment.