Skip to content

Commit

Permalink
Merge 0ab4167 into 764294e
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelF77 committed Nov 4, 2013
2 parents 764294e + 0ab4167 commit 3d32125
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 7 deletions.
40 changes: 33 additions & 7 deletions lib/main.js
Expand Up @@ -5,14 +5,30 @@ var path = require('path');
var Parser = require('./parser.js');
var parser = new Parser();

var ViewBag = {};
var Url = {
baseUrl: '',
Content : function(url) {
return url.replace('~',Url.baseUrl).toLowerCase();
}
};

module.exports = function (config) {
config = config || {
root: __dirname,
layout: null
layout: null,
};

Url.baseUrl = config.baseUrl || '';

var cachedFiles = {};

return {
render: function(view, model, body, sections, depth) {
renderCached: function(view, usecache) {
return this.render(view, undefined, undefined, undefined, 0, usecache);
},

render: function(view, model, body, sections, depth, usecache) {
if (!view) {
throw new Error("You must specify the view to be rendered.");
}
Expand All @@ -24,13 +40,13 @@ module.exports = function (config) {
}

if ('string' === typeof view) {
return this.renderFromFile(view, model, body, sections, depth)
return this.renderFromFile(view, model, body, sections, depth, usecache)
}

throw new Error("I've not implemented another path for views. Please specify a file name for the view as a string.");
},

renderFromFile: function(fileName, model, body, sections, depth) {
renderFromFile: function(fileName, model, body, sections, depth, usecache) {
if (!fileName) {
throw new Error("You must specify the file name to be rendered.");
}
Expand All @@ -47,6 +63,16 @@ module.exports = function (config) {
}
}

if (usecache) {
var r = cachedFiles[fileName];
if (!r) {
r = this.renderFromString(fs.readFileSync(fileName).toString(), model, body, sections, depth);
cachedFiles[fileName] = r;
}
return r;
}


return this.renderFromString(fs.readFileSync(fileName).toString(), model, body, sections, depth);
},

Expand All @@ -55,10 +81,10 @@ module.exports = function (config) {
var result = funcResult.razor.render(model, body, sections);

if (result.layout) {
return this.render(result.layout, model,
return this.render(result.layout, model,
function() {
return result.contents;
},
},
function(name) {
var tokens = funcResult.sections[name];
var sectionResult = "";
Expand All @@ -69,7 +95,7 @@ module.exports = function (config) {
sectionResult += token.contents;
} else if (token.type == 'razor') {
sectionResult += eval(token.token);
}
}
}

return sectionResult;
Expand Down
14 changes: 14 additions & 0 deletions lib/parser.js
Expand Up @@ -80,6 +80,20 @@ module.exports = function(config) {
}
loopIterator.next();
return this.createRazorToken("renderSection(" + sectionName + ")");
} else if (token == 'Url.Content') {
loopIterator.skipWhiteSpace();
if (loopIterator.currentChar() != '(') {
throw new Error('Expected "(" after "Url.Content".');
}
loopIterator.next();
loopIterator.skipWhiteSpace();
var urlToRender = tokenizer.getNextStringLiteral(loopIterator);
loopIterator.skipWhiteSpace();
if (loopIterator.currentChar() != ')') {
throw new Error('Expected ")" after "Url.Content".');
}
loopIterator.next();
return this.createRazorToken("Url.Content(" + urlToRender + ")");
} else {
return this.createRazorToken(token);
}
Expand Down
Binary file added samples/sample2/Public/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions samples/sample2/application.js
@@ -0,0 +1,16 @@
var express = require('express');
var app = express();
var port = process.env.port || 1337;
var KallyRazor = require('../../lib/main');

app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res) {
var razor = KallyRazor({ root: __dirname, layout: 'views/_Layout.cshtml' , baseUrl:''});
var result = razor.render('views/index.html', {
name: 'My Test Name',
});
res.send(result);
});

app.listen(port);
console.log('Listening on port ' + port);
7 changes: 7 additions & 0 deletions samples/sample2/package.json
@@ -0,0 +1,7 @@
{
"name": "KallyRazor_Sample_1",
"version": "1.0.0",
"dependencies": {
"express" : "3.x"
}
}
11 changes: 11 additions & 0 deletions samples/sample2/views/_Layout.cshtml
@@ -0,0 +1,11 @@
@{ layout = null; }
<!DOCTYPE html>
<html lang="en">
<head>
<title>@ViewBag.Title</title>
</head>
<body>
@renderBody()
@renderSection("scripts")
</body>
</html>
10 changes: 10 additions & 0 deletions samples/sample2/views/index.html
@@ -0,0 +1,10 @@
@{
ViewBag.Title = "Sample page title";
}
@section scripts{
<script type="text/javascript" src="@Url.Content("~/script.js")"></script>
}

<img src="@Url.Content("~/logo.png")" />
Welcome, @model.name! Hope you enjoy the samples!

0 comments on commit 3d32125

Please sign in to comment.