Verified Commit e7871caf authored by Aral Balkan's avatar Aral Balkan
Browse files

Add very basic partials support

parent 8a50a0dc
......@@ -29,10 +29,13 @@
jsdom = require 'jsdom'
fs = require 'fs'
path = require 'path'
# Load the Tally engine.
set = (fs.readFileSync __dirname + '/set.js', 'utf8').toString()
partialsCache = {}
# The Express 3 export.
exports.__express = (path, data, callback) ->
fs.readFile path, 'utf8', (error, template) ->
......@@ -44,7 +47,49 @@ exports.__express = (path, data, callback) ->
callback null, html
#
# Add partials to the template and return it
#
# Partial format:
#
# <!-- include route/to/partial.file -->
#
addPartials = (template) ->
includeRegEx = /\<!--\s*include\s+(\S+)\s*--\>/g
matches = []
viewsPath = path.normalize (path.join __dirname, '..', 'views')
while (theMatch = includeRegEx.exec template) != null
matches.push theMatch
for match in matches
tokenToReplace = match[0]
partialFilePath = path.join viewsPath, match[1]
console.log partialFilePath
# cachedPartial = partialsCache[partialFilePath]
#partial = if cachedPartial != undefined then cachedPartial else fs.readFileSync partialFilePath, 'utf8'
partial = fs.readFileSync partialFilePath, 'utf8'
console.log partial
template = template.replace (new RegExp tokenToReplace, 'g'), partial
return template
render = (template, data) ->
# Add partials to template
template = addPartials template
# Create the DOM.
document = jsdom.jsdom(template)
......
################################################################################
#
# Set partials example.
#
# Demonstrates how partials are used.
#
# Copyright © Aral Balkan.
# Released with ♥ by Ind.ie under the MIT License.
# Portions released under the Apache License.
#
################################################################################
exports.route = (request, response) ->
response.render 'partials', {}
......@@ -37,6 +37,9 @@ createRoute '/'
# Simple template example (with static data)
createRoute '/simple'
# Partials demo
createRoute '/partials'
# App.net global timeline example.
createRoute '/posts'
......
<!-- include partials/common-header.html -->
<div class='main' role='main'>
<h2>Main file</h2>
<p>The header and footer are included as partials.</p>
<!-- include partials/simple-footer.html -->
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<h1>Header (partial)</h1>
<hr>
<hr>
<h2>Footer (partial)</h2>
<p>© 2017 Aral Balkan. Released under the MIT License.</p>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment