├── .gitignore
├── README.md
├── index.js
├── lib
└── express-helpers.js
├── package.json
└── test
└── helpers.test.js
/.gitignore:
--------------------------------------------------------------------------------
1 | lib-cov
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Express Helpers
2 |
3 | Express Helpers is a port of EJS's ViewHelpers.
4 |
5 |
6 | ## Features
7 |
8 | Express Helpers provides view helpers for common tasks. These helpers are very similar to those found in the Ruby on Rails framework.
9 |
10 | * date_tag
11 | * form_tag
12 | * form_tag_end
13 | * hidden_field_tag
14 | * input_field_tag
15 | * link_to
16 | * submit_link_to
17 | * link_to_if
18 | * link_to_unless
19 | * password_field_tag
20 | * select_tag
21 | * single_tag_for
22 | * start_tag_for
23 | * submit_tag
24 | * tag
25 | * tag_end
26 | * text_area_tag
27 | * text_tag
28 | * text_field_tag
29 | * url_for
30 | * img_tag
31 |
32 |
33 | ## Installation
34 |
35 | npm install express-helpers
36 |
37 |
38 | ## How to use
39 |
40 | Require modules and cretae a server.
41 |
42 | var express = require('express');
43 | var helpers = require('express-helpers');
44 | var app = express.createServer();
45 |
46 | Register necessary view helpers...
47 |
48 | app.helpers({
49 | date_tag: helpers.date_tag
50 | });
51 |
52 | app.helpers({
53 | ...
54 |
55 | Or all view helpers:D
56 |
57 | app = helpers.all(app);
58 |
59 |
60 | ## Details
61 |
62 |
63 | ### date_tag(name, value, html_options)
64 | Creates a date tag
65 |
66 | date_tag('Installation[date]', new Date(1982, 10,20) )
67 |
68 |
69 | ### form_tag(action, html_options)
70 | Creates a start form tag.
71 |
72 | form_tag('/myaction',{multipart: true})
73 |
74 |
75 | ### end_form_tag()
76 | Creates a start form tag.
77 |
78 | form_tag_end()
79 |
80 |
81 | ### hidden_field_tag( name, value, html_options)
82 | Creates a hidden field.
83 |
84 | hidden_field_tag('something[interesting]', 5) =>
85 |
86 | ""
90 |
91 |
92 | ### img_tag
93 | Creates an image tag.
94 |
95 | img_tag('/some.png', 'something') => "
"
96 |
97 |
98 | ### input_field_tag
99 | Creates an input field tag.
100 |
101 | input_field_tag('something[interesting]', 5) =>
102 |
103 | ""
107 |
108 |
109 |
110 | ### link_to
111 | Creates a link to another page.
112 |
113 | link_to('hello world', '/something/here') => "hello world"
114 |
115 |
116 |
117 | ### submit_link_to
118 | Creates a submit button that links to another page.
119 |
120 | submit_link_to('holla', '/new/location') =>
121 |
122 | ""
125 |
126 |
127 |
128 | ### link_to_if(condition, name, url, html_options, post, block)
129 | Just like link_to if the condition is true. If condition is false it returns name.
130 |
131 | ### link_to_unless(condition, name, url, html_options, block)
132 | Just like link_to if the condition is false. If condition is true it returns name.
133 |
134 | ### password_field_tag
135 | Returns a password field.
136 |
137 | password_field_tag('something[interesting]', 5) =>
138 |
139 | ""
143 |
144 |
145 |
146 | ### select_tag
147 | Returns a select tag.
148 |
149 | var choices = [ {value: 1, text: 'First Choice' },
150 | {value: 2, text: 'Second Choice'},
151 | {value: 3, text: 'Third Choice'} ]
152 | select_tag('mySelectElement', 2, choices) =>
153 |
154 | ""
159 |
160 |
161 |
162 | ### single_tag_for
163 |
164 | Helper for creating a single tag like
165 |
166 | ### start_tag_for
167 |
168 | Helper for creating a beginning tag like
169 |
170 | ### submit_tag
171 | Creates a submit tag.
172 |
173 | submit_tag('Submit') => ""
174 |
175 | ### tag
176 |
177 | Creates a general tag.
178 |
179 | ### tag_end
180 |
181 | Creates an end tag like `
`.
182 |
183 | ### text_area_tag
184 |
185 | text_area_tag('task[description]', 'Here is some text.\nA new line.') =>
186 |
187 | ""
191 |
192 |
193 |
194 | ### text_field_tag
195 |
196 | text_field_tag('something[interesting]', 5) =>
197 |
198 | ""
202 |
203 |
204 |
205 | ### url_for
206 |
207 | returns a string that changes the url.
208 |
209 |
210 | ## License
211 |
212 | (The MIT License)
213 |
214 | EJS - Embedded JavaScript
215 |
216 | Copyright (c) 2007 Edward Benson
217 |
218 | Permission is hereby granted, free of charge, to any person obtaining a copy
219 | of this software and associated documentation files (the "Software"), to deal
220 | in the Software without restriction, including without limitation the rights
221 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
222 | copies of the Software, and to permit persons to whom the Software is
223 | furnished to do so, subject to the following conditions:
224 |
225 | The above copyright notice and this permission notice shall be included in
226 | all copies or substantial portions of the Software.
227 |
228 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
229 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
230 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
231 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
232 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
233 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
234 | THE SOFTWARE.
235 |
236 |
237 | Ported by Masahiro Hayashi
238 |
239 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | module.exports = require('./lib/express-helpers');
2 |
--------------------------------------------------------------------------------
/lib/express-helpers.js:
--------------------------------------------------------------------------------
1 | var express = require('express');
2 |
3 | exports.date_tag = function(name, value , html_options) {
4 | if (! (value instanceof Date))
5 | value = new Date();
6 |
7 | var month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
8 | var years = [], months = [], days =[];
9 | var year = value.getFullYear();
10 | var month = value.getMonth();
11 | var day = value.getDate();
12 | for (var y = year - 15; y < year+15 ; y++) {
13 | years.push({value: y, text: y});
14 | }
15 | for (var m = 0; m < 12; m++) {
16 | months.push({value: (m), text: month_names[m]});
17 | }
18 | for (var d = 0; d < 31; d++) {
19 | days.push({value: (d+1), text: (d+1)});
20 | }
21 | var year_select = select_tag(name+'[year]', year, years, {id: name+'[year]'} );
22 | var month_select = select_tag(name+'[month]', month, months, {id: name+'[month]'});
23 | var day_select = select_tag(name+'[day]', day, days, {id: name+'[day]'});
24 |
25 | return year_select+month_select+day_select;
26 | };
27 |
28 | exports.form_tag = function(action, html_options) {
29 | html_options = html_options || {};
30 | html_options.action = action;
31 | if (html_options.multipart == true) {
32 | html_options.method = 'post';
33 | html_options.enctype = 'multipart/form-data';
34 | }
35 |
36 | return start_tag_for('form', html_options);
37 | };
38 |
39 | exports.form_tag_end = function() { return tag_end('form'); };
40 |
41 | exports.hidden_field_tag = function(name, value, html_options) {
42 | return input_field_tag(name, value, 'hidden', html_options);
43 | };
44 |
45 | var input_field_tag = exports.input_field_tag = function(name, value , inputType, html_options) {
46 |
47 | html_options = html_options || {};
48 | html_options.id = html_options.id || name;
49 | html_options.value = value || '';
50 | html_options.type = inputType || 'text';
51 | html_options.name = name;
52 |
53 | return single_tag_for('input', html_options);
54 | };
55 |
56 | // exports.is_current_page = function(url) {
57 | // return (window.location.href == url || window.location.pathname == url ? true : false);
58 | // };
59 |
60 | var link_to = exports.link_to = function(name, url, html_options) {
61 | if (!name) var name = 'null';
62 | if (!html_options) var html_options = {};
63 |
64 | if (html_options.confirm) {
65 | html_options.onclick =
66 | " var ret_confirm = confirm(\""+html_options.confirm+"\"); if(!ret_confirm){ return false;} ";
67 | html_options.confirm = null;
68 | }
69 | html_options.href=url;
70 | return start_tag_for('a', html_options)+name+ tag_end('a');
71 | };
72 |
73 | exports.submit_link_to = function(name, url, html_options){
74 | if (!name) var name = 'null';
75 | if (!html_options) var html_options = {};
76 | html_options.onclick = html_options.onclick || '' ;
77 |
78 | if (html_options.confirm) {
79 | html_options.onclick =
80 | " var ret_confirm = confirm(\""+html_options.confirm+"\"); if(!ret_confirm){ return false;} ";
81 | html_options.confirm = null;
82 | }
83 |
84 | html_options.value = name;
85 | html_options.type = 'submit';
86 | html_options.onclick=html_options.onclick+
87 | (url ? url_for(url) : '')+'return false;';
88 | //html_options.href='#'+(options ? Routes.url_for(options) : '')
89 | return start_tag_for('input', html_options);
90 | };
91 |
92 | exports.link_to_if = function(condition, name, url, html_options, post, block) {
93 | return link_to_unless((condition == false), name, url, html_options, post, block);
94 | };
95 |
96 | var link_to_unless = exports.link_to_unless = function(condition, name, url, html_options, block) {
97 | html_options = html_options || {};
98 | if (condition) {
99 | if (block && typeof block == 'function') {
100 | return block(name, url, html_options, block);
101 | } else {
102 | return name;
103 | }
104 | } else
105 | return link_to(name, url, html_options);
106 | };
107 |
108 | // exports.link_to_unless_current = function(name, url, html_options, block) {
109 | // html_options = html_options || {};
110 | // return this.link_to_unless(this.is_current_page(url), name, url, html_options, block);
111 | // };
112 |
113 | exports.password_field_tag = function(name, value, html_options) { return input_field_tag(name, value, 'password', html_options); };
114 |
115 | var select_tag = exports.select_tag = function(name, value, choices, html_options) {
116 | html_options = html_options || {};
117 | html_options.id = html_options.id || name;
118 | html_options.value = value;
119 | html_options.name = name;
120 |
121 | var txt = '';
122 | txt += start_tag_for('select', html_options);
123 |
124 | for (var i = 0; i < choices.length; i++) {
125 | var choice = choices[i];
126 | var optionOptions = {value: choice.value};
127 | if (choice.value == value)
128 | optionOptions.selected ='selected';
129 | txt += start_tag_for('option', optionOptions )+choice.text+tag_end('option');
130 | }
131 | txt += tag_end('select');
132 | return txt;
133 | };
134 |
135 | var single_tag_for = exports.single_tag_for = function(_tag, html_options) { return tag(_tag, html_options, '/>');};
136 |
137 | var start_tag_for = exports.start_tag_for = function(_tag, html_options) { return tag(_tag, html_options); };
138 |
139 | exports.submit_tag = function(name, html_options) {
140 | html_options = html_options || {};
141 | //html_options.name = html_options.id || 'commit';
142 | html_options.type = html_options.type || 'submit';
143 | html_options.value = name || 'Submit';
144 | return single_tag_for('input', html_options);
145 | };
146 |
147 | var tag = exports.tag = function(_tag, html_options, end) {
148 | if (!end) var end = '>';
149 | var txt = ' ';
150 | for (var attr in html_options) {
151 | if (html_options[attr] != null)
152 | var value = html_options[attr].toString();
153 | else
154 | var value='';
155 | if (attr == "Class") // special case because "class" is a reserved word in IE
156 | attr = "class";
157 | if (value.indexOf("'") != -1)
158 | txt += attr+'=\"'+value+'\" ';
159 | else
160 | txt += attr+"='"+value+"' ";
161 | }
162 | return '<'+_tag+txt+end;
163 | };
164 |
165 | var tag_end = exports.tag_end = function(_tag) { return ''+_tag+'>'; };
166 |
167 | exports.text_tag = exports.text_area_tag = function(name, value, html_options) {
168 | html_options = html_options || {};
169 | html_options.id = html_options.id || name;
170 | html_options.name = html_options.name || name;
171 | value = value || '';
172 | if (html_options.size) {
173 | html_options.cols = html_options.size.split('x')[0];
174 | html_options.rows = html_options.size.split('x')[1];
175 | delete html_options.size;
176 | }
177 |
178 | html_options.cols = html_options.cols || 50;
179 | html_options.rows = html_options.rows || 4;
180 |
181 | return start_tag_for('textarea', html_options)+value+tag_end('textarea');
182 | };
183 |
184 | exports.text_field_tag = function(name, value, html_options) { return input_field_tag(name, value, 'text', html_options); };
185 |
186 | var url_for = exports.url_for = function(url) {
187 | return 'window.location="'+url+'";';
188 | };
189 |
190 | exports.img_tag = function(image_location, alt, options){
191 | options = options || {};
192 | options.src = image_location;
193 | options.alt = alt;
194 | return single_tag_for('img', options);
195 | };
196 |
197 | exports.all = function(app) {
198 | if (app instanceof express.Server) {
199 | for (name in exports) {
200 | if (name !== 'all' && typeof exports[name] === 'function') {
201 | var obj = {};
202 | obj[name] = exports[name];
203 | app.helpers(obj);
204 | }
205 | }
206 | }
207 | return app;
208 | };
209 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "express-helpers",
3 | "version": "0.1.0",
4 | "description": "Express Helpers",
5 | "author": "Masahiro Hayashi ",
6 | "keywords": ["express", "helper", "ejs"],
7 | "directories": { "lib": "./lib" },
8 | "engines": { "node": ">= 0.2.0" }
9 | }
10 |
--------------------------------------------------------------------------------
/test/helpers.test.js:
--------------------------------------------------------------------------------
1 | var assert = require('assert');
2 | var helpers = require('express-helpers');
3 |
4 | module.exports = {
5 | test_link_to: function() {
6 | assert.eql("hello world",
7 | helpers.link_to('hello world', '/something/here') );
8 | },
9 | test_date_tag: function() {
10 | var date = new Date(2007,10,20,1,1,1,1);
11 | assert.eql( "",
12 | helpers.date_tag('Installation[date]', date) );
13 | },
14 | test_form_tag: function() {
15 | assert.eql( "",
20 | helpers.form_tag_end() );
21 | },
22 | test_hidden_field_tag: function() {
23 | assert.eql( "",
24 | helpers.hidden_field_tag('something[interesting]', 5) );
25 | },
26 | test_input_field_tag: function() {
27 | assert.eql( "",
28 | helpers.input_field_tag('something[interesting]', 5) );
29 | },
30 | // test_current_page : function() {
31 | // assert( helpers.is_current_page(window.location.href) );
32 | // assert( helpers.is_current_page(window.location.pathname) );
33 | // assert.eql(false, helpers.is_current_page('juptierit.com') );
34 | // },
35 | test_submit_link_to : function() {
36 |
37 | assert.eql( "",
38 | helpers.submit_link_to('holla', '/new/location') );
39 | },
40 | test_link_to_unless : function() {
41 | assert.eql( "Reply", helpers.link_to_unless(false, 'Reply', '/reply' ) );
42 | assert.eql( "Reply", helpers.link_to_unless(true, 'Reply', '/reply' ) );
43 | },
44 | test_link_to_if : function() {
45 | assert.eql( "Reply", helpers.link_to_if(true, 'Reply', '/reply' ) );
46 | assert.eql( "Reply", helpers.link_to_if(false, 'Reply', '/reply' ) );
47 | },
48 | // test_link_to_unless_current : function() {
49 | // assert.eql( "Reply", helpers.link_to_unless_current('Reply', '/reply' ) );
50 | // assert.eql( "Reply", helpers.link_to_unless_current('Reply', window.location.pathname ) );
51 | // },
52 | test_password_field_tag : function() {
53 | assert.eql( "",
54 | helpers.password_field_tag('something[interesting]', 5) );
55 | },
56 | test_password_field_tag : function() {
57 | assert.eql( "",
58 | helpers.password_field_tag('something[interesting]', 5) );
59 | },
60 | test_select_tag : function() {
61 | var choices = [ {value: 1, text: 'First Choice' },
62 | {value: 2, text: 'Second Choice'},
63 | {value: '3', text: 'Third Choice'} ];
64 | assert.eql( "",
65 | helpers.select_tag('mySelectElement', 2, choices) );
66 | },
67 | test_text_area_tag : function() {
68 | assert.eql( "",
69 | helpers.text_area_tag('task[description]', 'Here is some text.\nA new line.') );
70 | },
71 | test_text_field_tag : function() {
72 | assert.eql( "",
73 | helpers.text_field_tag('something[interesting]', 5) );
74 | },
75 | test_text_img_tag : function() {
76 | assert.eql( "
",
77 | helpers.img_tag('/some.png', 'something') );
78 | },
79 | test_submit_tag : function() {
80 | assert.eql( "",
81 | helpers.submit_tag('Submit') );
82 | }
83 |
84 | };
85 |
--------------------------------------------------------------------------------