Skip to content

Commit

Permalink
Added examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Nov 6, 2009
1 parent 69006cc commit a98e3a3
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -16,7 +16,7 @@ parser:
@$(LEG) < src/parser.leg > src/parser.c

inspect: all
@./$(BIN) < examples/template.js.html
@./$(BIN) < examples/template.html

test: all
@./$(BIN) < spec/fixtures/article.html > spec/fixtures/article.html.js
Expand Down
70 changes: 68 additions & 2 deletions Readme.md
Expand Up @@ -20,20 +20,86 @@

## Mojo Binary

$ mojo < in > out
$ mojo &lt; in &gt; out
$ mojo --help

## Running Tests

$ make test

## Example

examples/template.html

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;{title}&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;{title}&lt;/h1&gt;
{#articles}
&lt;div class="article"&gt;
&lt;h2&gt;{title}&lt;/h2&gt;
&lt;p&gt;{body}&lt;/p&gt;
{#published}
&lt;p&gt;{title} is published&lt;/p&gt;
{/published}
&lt;/div&gt;
{/articles}
&lt;/body&gt;
&lt;/html&gt;

example template rendering implementation:

load('lib/mojo.js')

function render(template, o) {
return eval(readFile('examples/' + template + '.html.js'))
}

template object:

page = {
title: 'Articles',
articles: [
{ title: 'One', body: 'some more one' },
{ title: 'Two', body: 'some more two', published: true }
]
}

print(render('template', page))

rendered markup output:

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Articles&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Articles&lt;/h1&gt;

&lt;div class="article"&gt;
&lt;h2&gt;One&lt;/h2&gt;
&lt;p&gt;some more one&lt;/p&gt;

&lt;/div&gt;

&lt;div class="article"&gt;
&lt;h2&gt;Two&lt;/h2&gt;
&lt;p&gt;some more two&lt;/p&gt;

&lt;p&gt;Two is published&lt;/p&gt;

&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;

## License

(The MIT License)

Copyright (c) 2008 - 2009 TJ Holowaychuk <tj@vision-media.ca>
Copyright (c) 2008 - 2009 TJ Holowaychuk &lt;tj@vision-media.ca&gt;

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
17 changes: 17 additions & 0 deletions examples/template.html
@@ -0,0 +1,17 @@
<html>
<head>
<title>{title}</title>
</head>
<body>
<h1>{title}</h1>
{#articles}
<div class="article">
<h2>{title}</h2>
<p>{body}</p>
{#published}
<p>{title} is published</p>
{/published}
</div>
{/articles}
</body>
</html>
19 changes: 19 additions & 0 deletions examples/template.html.js
@@ -0,0 +1,19 @@
'<html>\n\
<head>\n\
<title>' + (Mojo.escape(o.title)) + '</title>\n\
</head>\n\
<body>\n\
<h1>' + (Mojo.escape(o.title)) + '</h1>\n\
' + Mojo.enumerate(o, o.articles, function(o){
return '\n\
<div class="article">\n\
<h2>' + (Mojo.escape(o.title)) + '</h2>\n\
<p>' + (Mojo.escape(o.body)) + '</p>\n\
' + Mojo.enumerate(o, o.published, function(o){
return '\n\
<p>' + (Mojo.escape(o.title)) + ' is published</p>\n\
'}) + '\n\
</div>\n\
'}) + '\n\
</body>\n\
</html>'
39 changes: 10 additions & 29 deletions examples/template.js
@@ -1,35 +1,16 @@

user = {
name: 'Tj',
roles: [
{ name: 'Admin' },
{ name: 'Manager' }
]
}
load('lib/mojo.js')

Mojo = {
version: '0.0.1',
enumerate: function(a, fn) {
for (var buf = [], i = 0, len = a.length; i < len; ++i)
buf.push(fn(a[i]))
return buf.join(' ')
}
function render(template, o) {
return eval(readFile('examples/' + template + '.html.js'))
}

function render(o) {
return '<div>\n\
<h2>' + (o.name) + '</h2>\n\
<ul>\n\
\n\
\n\
' + Mojo.enumerate(o.roles, function(o){
return '\n\
<li>' + (o.name) + '</li>\n\
'}) + '\n\
</ul>\n\
</div>\n\
\n\
'
page = {
title: 'Articles',
articles: [
{ title: 'One', body: 'some more one' },
{ title: 'Two', body: 'some more two', published: true }
]
}

print(render(user))
print(render('template', page))
11 changes: 0 additions & 11 deletions examples/template.js.html

This file was deleted.

0 comments on commit a98e3a3

Please sign in to comment.