Browse Source

Merge pull request #17 from babolivier/fix/default-template

Add default template and fallback to it if no template found
Brendan Abolivier 6 years ago
parent
commit
cda4aaff1f
No account linked to committer's email
2 changed files with 61 additions and 26 deletions
  1. 3
    1
      README.md
  2. 58
    25
      server.js

+ 3
- 1
README.md View File

145
 
145
 
146
 ## Templating
146
 ## Templating
147
 
147
 
148
-Each e-mail sent by the form follows a template described in `template.pug` (it's [Pug](pugjs.org/)). If you want to change the way the e-mails you receive are displayed in your mailbox, just edit it! You don't even need to restart the server aftewards :smile:
148
+Each e-mail sent by the form follows a template described in `template.example.pug` (it's [Pug](pugjs.org/)). If you want to change the way the e-mails you receive are displayed in your mailbox, just edit it! You don't even need to restart the server aftewards :smile:
149
 
149
 
150
 The template also features custom fields, iterating over the `custom` object, containing the field's label and user-input value:
150
 The template also features custom fields, iterating over the `custom` object, containing the field's label and user-input value:
151
 
151
 
156
 }
156
 }
157
 ```
157
 ```
158
 
158
 
159
+The template needs to be named `template.pug`. If no template could be found under this name, SMAM will use a default one, which features both default and custom fields, and should be sufficient for non-advanced usage.
160
+
159
 ## Personnalising
161
 ## Personnalising
160
 
162
 
161
 As you might have already seen, the contact form is generated without any form of style except your browser's default one. But that doesn't meen that you have to add an ugly form to your site to receive contact e-mails, as every element has a specific id (beginning with the `form_` prefix), allowing you to use your own style on your contact form.
163
 As you might have already seen, the contact form is generated without any form of style except your browser's default one. But that doesn't meen that you have to add an ugly form to your site to receive contact e-mails, as every element has a specific id (beginning with the `form_` prefix), allowing you to use your own style on your contact form.

+ 58
- 25
server.js View File

1
-var pug			= require('pug');
2
-var nodemailer  = require('nodemailer');
3
-var crypto		= require('crypto');
4
-var settings	= require('./settings');
1
+var pug = require('pug');
2
+var nodemailer = require('nodemailer');
3
+var crypto = require('crypto');
4
+var fs = require('fs');
5
+var settings = require('./settings');
5
 
6
 
6
 // Translation
7
 // Translation
7
-var locale		= require('./locales/' + settings.language);
8
-var lang		= locale.server;
8
+var locale = require('./locales/' + settings.language);
9
+var lang = locale.server;
9
 
10
 
10
 // Web server
11
 // Web server
11
-var bodyParser	= require('body-parser');
12
-var cors		= require('cors');
13
-var express		= require('express');
14
-var app			= express();
12
+var bodyParser = require('body-parser');
13
+var cors = require('cors');
14
+var express = require('express');
15
+var app = express();
15
 
16
 
16
 // Logging
17
 // Logging
17
 var printit = require('printit');
18
 var printit = require('printit');
28
 // Verification tokens
29
 // Verification tokens
29
 var tokens = {};
30
 var tokens = {};
30
 
31
 
32
+// Default template
33
+// JavaScript has no native way to handle multi-line strings, so we put our template
34
+// in a comment inside a function fro which we generate a string.
35
+// cf: https://tomasz.janczuk.org/2013/05/multi-line-strings-in-javascript-and.html
36
+var defaultTemplate = (function() {/*
37
+html
38
+	body
39
+		p.subj
40
+			span(style="font-weight:bold") Subject: 
41
+			span= subject
42
+		p.from
43
+			span(style="font-weight:bold") Sent from: 
44
+			span= replyTo
45
+		each field in custom
46
+			p.custom
47
+				span(style="font-weight:bold")= field.label + ': '
48
+				span= field.value
49
+		p.message
50
+			span(style="font-weight:bold") Message: 
51
+		
52
+		p= html
53
+*/}).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1];
31
 
54
 
32
 // Serve static (JS + HTML) files
55
 // Serve static (JS + HTML) files
33
 app.use(express.static('front'));
56
 app.use(express.static('front'));
104
 	// Replacing the mail's content with HTML from the pug template
127
 	// Replacing the mail's content with HTML from the pug template
105
 	// Commenting the line below will bypass the generation and only user the
128
 	// Commenting the line below will bypass the generation and only user the
106
 	// text entered by the user
129
 	// text entered by the user
107
-	params.html = pug.renderFile('template.pug', params);
108
-
109
-	log.info(lang.log_sending, params.replyTo);
110
-
111
-	// Send the email to all users
112
-	sendMails(params, function(err, infos) {
130
+	fs.access('template.pug', function(err) {
131
+		// Checking if the template exists.
132
+		// If not, fallback to the default template.
133
+		// TODO: Parameterise the template file name.
113
 		if(err) {
134
 		if(err) {
114
-			log.error(err);
115
-		}
116
-		logStatus(infos);
117
-	}, function() {
118
-		if(status.failed === status.total) {
119
-			res.status(500).send();
135
+			params.html = pug.render(defaultTemplate, params);
120
 		} else {
136
 		} else {
121
-			res.status(200).send();
137
+			params.html = pug.renderFile('template.pug', params);
122
 		}
138
 		}
123
-	})
139
+
140
+		log.info(lang.log_sending, params.replyTo);
141
+
142
+		// Send the email to all users
143
+		sendMails(params, function(err, infos) {
144
+			if(err) {
145
+				log.error(err);
146
+			}
147
+			logStatus(infos);
148
+		}, function() {
149
+			if(status.failed === status.total) {
150
+				res.status(500).send();
151
+			} else {
152
+				res.status(200).send();
153
+			}
154
+		});
155
+	});
156
+
124
 });
157
 });
125
 
158
 
126
 
159
 
334
 	}
367
 	}
335
 
368
 
336
 	return fields;
369
 	return fields;
337
-}
370
+}