Permalink
Browse files

Updated Jade example so that it doesn't escape template HTML,

Wrote new Template module tests so that it tests text and HTML output for engines,
Moved ejs engine to `/lib/template/engines/ejs.js` and updated tests/templato
Finished Jade example so it matches with EJS example
  • Loading branch information...
1 parent 9ab5a2c commit 6de6460aff8aa46c4da2cd748f35cf68152598da @larzconwell larzconwell committed Jun 20, 2012
@@ -2,8 +2,8 @@ var TemplatoGeddyEJS = (function() {
function TemplatoGeddyEJS() {
// Hardcoded for specifically Geddy's EJS implementation
- // Location: `deps/ejs.js`
- this.engine = this.engine || require('../../../ejs');
+ // Location: `lib/template/engines/ejs.js`
+ this.engine = this.engine || require('../../../../lib/template/engines/ejs');
};
TemplatoGeddyEJS.prototype.compile = function(template, options) {
View
@@ -2,6 +2,7 @@
require('./color');
var data
+ , engine_options
, yield
, output
, engines
@@ -12,11 +13,14 @@ var data
, Templato
, templato
, errors = []
- , compiled;
+ , compiled
+ , test;
// Helpers
data = { content: 'content text' };
+engine_options = {};
output = "hey<p>some text in paragraph</p>content text";
+outputHTML = "<a href='#'>hey</a><p>some text in paragraph</p>content text";
// Data
engines = ['ejs', 'geddy_ejs', 'jade', 'mustache', 'handlebars'];
@@ -27,35 +31,81 @@ enginesText = [
, '{{yield}}<p>some text in paragraph</p>{{content}}'
, '{{yield}}<p>some text in paragraph</p>{{content}}'
];
+enginesTextHTML = [
+ '<%- yieldHTML() %><p>some text in paragraph</p><%= content %>'
+ , '<%= yieldHTML(); %><p>some text in paragraph</p><%= content; %>'
+ , '!= yieldHTML()\np some text in paragraph\n= content'
+ , '{{{yieldHTML}}}<p>some text in paragraph</p>{{content}}'
+ , '{{{yieldHTML}}}<p>some text in paragraph</p>{{content}}'
+];
Templato = require('./index');
Templato.registerHelper('yield', function() { return 'hey'; });
+Templato.registerHelper('yieldHTML', function() { return "<a href='#'>hey</a>"; });
-for(i in engines) {
- engine = engines[i]
- engineText = enginesText[i];
+test = function(options, engine_options) {
+ options = options || {};
+ engine_options = engine_options || {};
templato = new Templato;
templato.set({
- engine: engine
- , template: engineText
+ engine: options.engine
+ , template: options.template
+ , options: engine_options
});
try {
- compiled = templato.render(data);
+ compiled = templato.render(options.data);
- if(compiled === output) {
- console.log('==> '.cyan + engine + ' passed'.green);
+ if(compiled === options.output) {
+ content = '==> '.cyan + options.engine + ' passed'.green;
+ if(options.html) content += ' html_mode'.blue;
+
+ console.log(content);
} else {
- errors.push(engine.red + ': '.red + compiled);
- console.log('==> '.cyan + engine + ' failed, output not same'.red);
+ if(options.html) {
+ errors.push(options.engine.red + ': '.red + 'html_mode '.blue + '\n Output: ' + compiled + '\n Expected: ' + options.output);
+ } else errors.push(options.engine.red + ': '.red + compiled);
+
+ content = '==> '.cyan + options.engine + ' failed, output not same'.red;
+ if(options.html) content += ' html_mode'.blue;
+
+ console.log(content);
}
} catch(err) {
- errors.push(engine.red + ': '.red + err);
- console.log('==> '.cyan + engine + ' failed from compile error'.red);
+ if(options.html) {
+ errors.push(options.engine.red + ': '.red + 'html_mode '.blue + err);
+ } else errors.push(options.engine.red + ': '.red + err);
+
+ content = '==> '.cyan + options.engine + ' failed from compile error'.red;
+ if(options.html) content += ' html_mode'.blue;
+
+ console.log(content);
}
};
+console.log('\nTesting normal output:');
+for(i in engines) {
+ test({
+ engine: engines[i]
+ , template: enginesText[i]
+ , data: data
+ , output: output
+ }, engine_options);
+}
+
+console.log('\nTesting HTML output:');
+for(i in engines) {
+ test({
+ engine: engines[i]
+ , template: enginesTextHTML[i]
+ , data: data
+ , output: outputHTML
+ , html: true
+ }, engine_options);
+}
+
+// Log errors once finished
console.log('\nErrors:');
for(i in errors) {
console.log(errors[i]);
@@ -42,4 +42,4 @@ html(lang="en")
a(href="http://geddyjs.org/docs.html") Docs
.container
- = yield()
+ != yield()
@@ -14,11 +14,11 @@ form#todo-form.form-horizontal(action=action, method="POST")
label.control-label(for="title") Title
.controls
input.span6(placeholder="enter title", name="title", value=titleValue)
- - if (errors)
+ - if(errors)
p
- - for (var p in errors)
+ - for(var p in errors)
div= errors[p]
- - if (isUpdate)
+ - if(isUpdate)
.control-group
label(for="status") Status
.controls
@@ -27,5 +27,5 @@ form#todo-form.form-horizontal(action=action, method="POST")
option done
.form-actions
input.btn.btn-primary(type="submit", value=btnText)
- - if (isUpdate)
+ - if(isUpdate)
button.btn.btn-danger(type="submit", formaction=deleteAction, formmethod="POST") Remove
@@ -1,13 +1,14 @@
.hero-unit
h2 To Do List
- a(href="/todos/add", class="btn pull-right") Create a new To Do
+ a.btn.pull-right(href="/todos/add") Create a new To Do
-- if (todos && todos.length)
- - for (var i in todos)
- .row.todo-item
- .span8
- h3
- a(href="/todos/"+todos[i].id+"/edit")= todos[i].title
- .span4
- h3
- i.icon-list-alt= todos[i].status
+- if(todos && todos.length)
+- for(var i in todos)
+ .row.todo-item
+ .span8
+ h3
+ a(href="/todos/"+todos[i].id+"/edit")= todos[i].title
+ .span4
+ h3
+ i.icon-list-alt
+ = todos[i].status
@@ -1,46 +1,46 @@
- <%
- var isUpdate = params.action == 'edit'
- , formTitle = isUpdate ? 'Update this To Do Item' : 'Create a new To Do Item'
- , action = isUpdate ? '/todos/' + todo.id + '?_method=PUT' : '/todos'
- , deleteAction = isUpdate ? '/todos/' + todo.id + '?_method=DELETE' : ''
- , btnText = isUpdate ? 'Update' : 'Add'
- , doneStatus = isUpdate ? 'checked' : ''
- , titleValue = isUpdate ? todo.title : ''
- , errors = params.errors;
- %>
- <form id="todo-form" class="form-horizontal" action="<%= action %>" method="POST">
- <fieldset>
- <legend><%= formTitle %></legend>
+<%
+ var isUpdate = params.action == 'edit'
+ , formTitle = isUpdate ? 'Update this To Do Item' : 'Create a new To Do Item'
+ , action = isUpdate ? '/todos/' + todo.id + '?_method=PUT' : '/todos'
+ , deleteAction = isUpdate ? '/todos/' + todo.id + '?_method=DELETE' : ''
+ , btnText = isUpdate ? 'Update' : 'Add'
+ , doneStatus = isUpdate ? 'checked' : ''
+ , titleValue = isUpdate ? todo.title : ''
+ , errors = params.errors;
+%>
+<form id="todo-form" class="form-horizontal" action="<%= action %>" method="POST">
+ <fieldset>
+ <legend><%= formTitle %></legend>
+ <div class="control-group">
+ <label for="title" class="control-label">Title</label>
+ <div class="controls">
+ <input type="text" class="span6" placeholder="enter title" name="title" value='<%= titleValue %>'/>
+ <% if (errors) { %>
+ <p>
+ <% for (var p in errors) { %>
+ <div><%= errors[p]; %></div>
+ <% } %>
+ </p>
+ <% } %>
+ </div>
+ </div>
+ <% if (isUpdate) { %>
<div class="control-group">
- <label for="title" class="control-label">Title</label>
+ <label for="status">Status</label>
<div class="controls">
- <input type="text" class="span6" placeholder="enter title" name="title" value='<%= titleValue %>'/>
- <% if (errors) { %>
- <p>
- <% for (var p in errors) { %>
- <div><%= errors[p]; %></div>
- <% } %>
- </p>
- <% } %>
+ <select name="status">
+ <option>open</option>
+ <option>done</option>
+ </select>
</div>
</div>
+ <% } %>
+ <div class="form-actions">
+ <input type="submit" class="btn btn-primary" value="<%= btnText %>"/>
<% if (isUpdate) { %>
- <div class="control-group">
- <label for="status">Status</label>
- <div class="controls">
- <select name="status">
- <option>open</option>
- <option>done</option>
- </select>
- </div>
- </div>
+ <button type="submit" formaction="<%= deleteAction %>" formmethod="POST" class="btn btn-danger">Remove</button>
<% } %>
- <div class="form-actions">
- <input type="submit" class="btn btn-primary" value="<%= btnText %>"/>
- <% if (isUpdate) { %>
- <button type="submit" formaction="<%= deleteAction %>" formmethod="POST" class="btn btn-danger">Remove</button>
- <% } %>
- </div>
- </fieldset>
- </form>
+ </div>
+ </fieldset>
+</form>
@@ -17,7 +17,7 @@
*/
// Note: If moved remember to change utils directory
-var utils = require('../lib/utils')
+var utils = require('../../utils')
, ejs = {};
ejs.Template = function(params) {
@@ -19,7 +19,7 @@ var utils = require('../utils')
exports.linkTo = {
name: 'linkTo',
- action: function(path) {return path;}
+ action: function() {}
};
exports.scriptLink = {

0 comments on commit 6de6460

Please sign in to comment.