Permalink
Browse files

Added examples

  • Loading branch information...
1 parent 69006cc commit a98e3a3de3766269a28deaf32818ffe29d6f1b9c @tj tj committed Nov 6, 2009
Showing with 115 additions and 43 deletions.
  1. +1 −1 Makefile
  2. +68 −2 Readme.md
  3. +17 −0 examples/template.html
  4. +19 −0 examples/template.html.js
  5. +10 −29 examples/template.js
  6. +0 −11 examples/template.js.html
View
@@ -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
View
@@ -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
View
@@ -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>
View
@@ -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>'
View
@@ -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))
View
@@ -1,11 +0,0 @@
-<div>
- <h2>{name}</h2>
- <ul>
- {! this loops roles :) }
- {{! double mustache is fine too }}
- {#roles}
- <li>{name}</li>
- {/roles}
- </ul>
-</div>
-

0 comments on commit a98e3a3

Please sign in to comment.