Skip to content

Instantly share code, notes, and snippets.

@chrisdickinson
Forked from toastdriven/WIP_base.html
Created July 30, 2010 17:18
Show Gist options
  • Save chrisdickinson/500930 to your computer and use it in GitHub Desktop.
Save chrisdickinson/500930 to your computer and use it in GitHub Desktop.
FULL ON DOUBLE RAINBOW
<html>
<head>
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block content %}
<!-- content goes here plz -->
{% endblock %}
</body>
</html>
{% extends "base.html" %}
{% block title %}{{ object.title|title }}{% endblock %}
{% block content %}
<h1>{{ object.title|title }}</h1>
<p>{{ object.tease }}</p>
<div class="article">
{{ object.body }}
</div>
<span>{{ object.created|date:"b dl" }}</span>
{% endblock %}
{% extends "base.html" %}
{% block title %}Entries{% endblock %}
{% block content %}
<h1>Entries</h1>
<ul>
{% for entry in object_list %}
<li>
<div>
<a href="{{ entry.get_absolute_url }}" title="{{ entry.title|title }}">{{ entry.title|title }}</a>
</div>
<div>{{ entry.tease }}</div>
</li>
{% empty %}
<li>There are no entries.</li>
{% endfor %}
</ul>
{% endblock %}
var pieshop = require('pieshop'),
fields = pieshop.fields,
jsdtl = require('jsdtl');
var Entry = pieshop.core.resource({
'table': 'entries',
'id': new fields.PositiveIntegerField(),
'slug': new fields.CharField({'max_length':50}),
'title': new fields.CharField({'max_length':255}),
'tease': new fields.CharField({'max_length':255}),
'body': new fields.TextField(),
'created': new fields.DateTimeField({'auto_now_add':true}),
'get_absolute_url':function() {
return '/'+jsdtl.datetime.strftime(this.created, "Y/b/d")+'/'+this.slug+'/';
}
});
exports.Entry = Entry;
var settings = require('pieshop').settings;
var jsdtlsettings = require('jsdtl').settings;
settings.set_value('DB_HOST', 'localhost');
settings.set_value('DB_NAME', 'whisper');
settings.set_addon('backend', 'postpie.backends:PostgresBackend');
settings.set_addon('transport', 'postpie.transports:PostgresTransport');
jsdtlsettings.set_addon('loaders', 'jsdtl.loaders:filesystemLoader');
exports.templateLoader = jsdtlsettings.get_addon('loaders');
// Whisper.js
// A light-weight blog in node.js.
// Requires:
// http://github.com/ry/node_postgres.git
// http://github.com/chrisdickinson/pieshop.git
// http://github.com/chrisdickinson/postpie.git
// ..and JSDTL, but that's not publically available yet (sorry)
// Licensed under the BSD.
/*
Setup:
#. Create the DB via ``createdb whisper``.
#. Run the following SQL against that db using ``psql whisper``.
BEGIN;
CREATE TABLE entries (
id SERIAL,
slug VARCHAR(50),
title VARCHAR(255),
tease VARCHAR(1000),
body TEXT,
created TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
COMMIT;
*/
// Change this to something unique!
var SECRET_PASSWORD = 't3ll_m3_4_s3kr3t';
var PORT = 8000;
var DB_NAME = 'whisper';
var author = 'Daniel Lindsley';
var version = [0, 1, 0];
var fs = require('fs'),
http = require('http'),
sys = require('sys'),
url = require('url');
var settings = require('./settings'),
loaders = settings.templateLoader,
Entry = require('./models').Entry,
jsdtl = require('jsdtl');
var getObjectOr404 = function(model, filters, callback) {
model.objects.filter(filters).all(function(objects) {
if(objects.length < 1) throw new model.DoesNotExist();
callback(objects[0]);
});
};
var getTemplate = function(name, callback) {
loaders.get_template(name, callback);
};
var detail_view = function(request, response, year, month, day, slug) {
getObjectOr404(Entry, {'slug__exact':slug}, function(obj) {
getTemplate('entry_detail.html', function(template) {
var context = new jsdtl.core.Context({
'object':obj
});
template.render(context, function(data) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data);
response.end();
});
});
});
};
var list_view = function(request, response) {
Entry.objects.filter({}).limit(5).all(function(objects) {
getTemplate('entry_list.html', function(template) {
var context = new jsdtl.core.Context({
'object_list':objects
});
template.render(context, function(data) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data);
response.end();
});
});
});
};
http.createServer(function(request, response) {
var routes = [
[/^\/$/, list_view],
[/^\/(\d{4})\/(\w{3})\/(\d{1,2})\/([\w\-]+)\/$/, detail_view]
];
var path = url.parse(request.url, true).pathname;
for(var i = 0, len = routes.length; i < len; ++i) {
var match = routes[i][0].exec(path);
if(match) {
var args = [request, response];
args = args.concat(match.slice(1));
routes[i][1].apply({}, args);
return;
}
}
}).listen(PORT);
sys.puts('Whisper running at http://localhost:'+PORT+'/...');
sys.puts('Ctrl-C to stop.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment