Skip to content

Commit b8c0529

Browse files
committed
10: Custom templating system and updated readme
1 parent ba93c4c commit b8c0529

File tree

8 files changed

+56
-6
lines changed

8 files changed

+56
-6
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ To browse codes by articles, browse to the related branch.
55
2. [Reading and Serving Files - Node.js Backend Development, Part 2](https://github.com/SabujXi/Learn-Node.js-with-Sabuj/tree/002_reading_serving_files)
66
3. [Creating Custom Modules - Node.js Backend Development, Part 3](https://github.com/SabujXi/Learn-Node.js-with-Sabuj/tree/003_writing_custom_modules)
77
4. [Understanding and Working with Response - Node.js Backend Development, Part 4](https://github.com/SabujXi/Learn-Node.js-with-Sabuj/tree/004_http_server_response)
8+
5. [Working With Urls, Methods and Forms - Node.js Backend Development, Part 5](https://github.com/SabujXi/Learn-Node.js-with-Sabuj/tree/005_urls_methods_forms)
9+
6. [Implementing Routing - Node.js Backend Development, Part 6](https://github.com/SabujXi/Learn-Node.js-with-Sabuj/tree/006_implementing_routing)
10+
7. [Implementing Improved Routing - Node.js Backend Development, Part 7](https://github.com/SabujXi/Learn-Node.js-with-Sabuj/tree/007_implementing_routing_2)
11+
8. [Serving Static Files - Node.js Backend Development, Part 8](https://github.com/SabujXi/Learn-Node.js-with-Sabuj/tree/008_static_file_serving)
12+
9. [Global Objects - Node.js Backend Development, Part 9](https://github.com/SabujXi/Learn-Node.js-with-Sabuj/tree/009_global_objects)
13+
10. [Implementing a Templating System - Node.js Backend Development, Part 10](https://github.com/SabujXi/Learn-Node.js-with-Sabuj/tree/010_custom_templating_system)
814

915
## About Myself
1016
I am **Md. Sabuj Sarker** - a Software Engineer, Trainer and Writer. I have over 10 years of experience in software development, web design and development, professional training, writing and some other cool stuffs including few years of experience in mobile application development. I am also an open source contributor. The open source framework called **Synamic** is developed by me. Visit my Github repositories at [SabujXi](https://github.com/SabujXi "Md. Sabuj Sarker on Github") or visit my personal website at [www.sabuj.me](http://www.sabuj.me "Website of Md. Sabuj Sarker")

custom_modules/responder.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ exports.serveRequests = function(request, response){
1515
for (i = 0; i < routes.length; i++)
1616
{
1717
var router = routes[i];
18-
console.log("Router: ")
19-
console.log(router)
2018
var pattern = router.pattern;
2119
var handler = router.handler;
2220

custom_modules/templating.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var fs = require('fs');
2+
3+
exports.render = function(template_name, values, callback){
4+
var str_array = [];
5+
var last_idx = 0;
6+
fs.readFile('templates/' + template_name, 'utf8', function(err, data){
7+
var re = /({{\s*([0-9a-zA-Z_]+)\s*}})/g;
8+
data.replace(re, function(match, full, identifier, idx) {
9+
str_array.push(data.slice(last_idx, idx) + values[identifier]);
10+
last_idx = idx + full.length;
11+
});
12+
13+
if (last_idx !== data.length - 1){
14+
str_array.push(data.slice(last_idx, data.length));
15+
}
16+
17+
callback(str_array.join(""));
18+
});
19+
}

html/home.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!doctype html>
22
<html>
33
<head>
4-
<title>Learning Node.js Part 2</title>
4+
<title>Learning Node.js with Sabuj</title>
55
</head>
66
<body>
77
<h1>My Header for Node.js Learning</h1>

routes.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var process_form = require("./views/process_form").process_form;
44
var view_404 = require("./views/view_404").view_404;
55
var static_files = require("./views/static_files").static_files;
66
var globalo = require("./views/globalo").globalo;
7+
var templated_view = require("./views/templated_view").templated_view;
78

89
var routes = [
910
{
@@ -25,11 +26,12 @@ var routes = [
2526
{
2627
pattern: 'globalo',
2728
handler: globalo
29+
},
30+
{
31+
pattern: 'templated_view',
32+
handler: templated_view
2833
}
2934
]
3035

31-
32-
33-
3436
exports.routes = routes;
3537
exports.view_404 = view_404;

templates/.gitignore

Whitespace-only changes.

templates/my_template.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Learning Node.js with Sabuj</title>
5+
</head>
6+
<body>
7+
<h1> A Header {{ random_no }}</h1>
8+
<p>Random Timestamp: {{ now }}</p>
9+
</body>
10+
</html>

views/templated_view.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var render = require('../custom_modules/templating').render;
2+
3+
exports.templated_view = function(request, response, url){
4+
render('my_template.html', {
5+
random_no: Math.random() * 100,
6+
now: Date.now() * Math.random()
7+
},
8+
function(data){
9+
response.writeHead(200, {
10+
'Content-Type': 'text/html'
11+
});
12+
response.end(data);
13+
}
14+
);
15+
}

0 commit comments

Comments
 (0)