mirror of
https://github.com/zebrajr/express.git
synced 2025-12-06 00:19:48 +01:00
Added markdown template engine example
This commit is contained in:
parent
42f3ad436d
commit
61aec6e961
41
examples/markdown/app.js
Normal file
41
examples/markdown/app.js
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
|
||||
// Expose modules in ./support for demo purposes
|
||||
require.paths.unshift(__dirname + '/../../support');
|
||||
|
||||
// $ npm install markdown
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var express = require('../../lib/express')
|
||||
, md = require('markdown').markdown;
|
||||
|
||||
var app = express.createServer();
|
||||
|
||||
// register .md so that markdown may comply
|
||||
// with the express view system by implementing
|
||||
// a .compile() method
|
||||
|
||||
app.register('.md', {
|
||||
compile: function(str, options){
|
||||
var html = md.toHTML(str);
|
||||
return function(locals){
|
||||
return html.replace(/\{([^}]+)\}/g, function(_, name){
|
||||
return locals[name];
|
||||
});
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// Optional since express defaults to CWD/views
|
||||
|
||||
app.set('views', __dirname + '/views');
|
||||
app.set('view engine', 'md');
|
||||
|
||||
app.get('/', function(req, res){
|
||||
res.render('index', { layout: false, title: 'Markdown Example' });
|
||||
});
|
||||
|
||||
app.listen(3000);
|
||||
console.log('Express app started on port 3000');
|
||||
4
examples/markdown/views/index.md
Normal file
4
examples/markdown/views/index.md
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
|
||||
# {title}
|
||||
|
||||
Just an example view rendered with _markdown_.
|
||||
Loading…
Reference in New Issue
Block a user