Skip to content

Commit 10fc320

Browse files
committed
Rename confusing 'context' variables
no issue - In Ghost, 'context' means the page or section of a blog we're currently within when rendering a theme, e.g. 'post' or 'tag' or 'home'. - In handlebars 'context' refers to the blob of JSON that is tied to a template. - These two uses of the word 'context' have gotten very confusing, so I've removed all usage of 'context' within the Ghost handlebars helpers, EXCEPT where they actually refer to the current context (e.g. the is helper)
1 parent 6bbcbab commit 10fc320

File tree

13 files changed

+62
-67
lines changed

13 files changed

+62
-67
lines changed

core/server/data/meta/asset_url.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
var config = require('../../config');
22

3-
function getAssetUrl(context, isAdmin, minify) {
3+
function getAssetUrl(path, isAdmin, minify) {
44
var output = '';
55

66
output += config.paths.subdir + '/';
77

8-
if (!context.match(/^favicon\.ico$/) && !context.match(/^shared/) && !context.match(/^asset/)) {
8+
if (!path.match(/^favicon\.ico$/) && !path.match(/^shared/) && !path.match(/^asset/)) {
99
if (isAdmin) {
1010
output += 'ghost/';
1111
} else {
1212
output += 'assets/';
1313
}
1414
}
1515

16-
// Get rid of any leading slash on the context
17-
context = context.replace(/^\//, '');
16+
// Get rid of any leading slash on the path
17+
path = path.replace(/^\//, '');
1818

1919
// replace ".foo" with ".min.foo" in production
2020
if (minify) {
21-
context = context.replace(/\.([^\.]*)$/, '.min.$1');
21+
path = path.replace(/\.([^\.]*)$/, '.min.$1');
2222
}
2323

24-
output += context;
24+
output += path;
2525

26-
if (!context.match(/^favicon\.ico$/)) {
26+
if (!path.match(/^favicon\.ico$/)) {
2727
output = output + '?v=' + config.assetHash;
2828
}
2929

core/server/helpers/asset.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
var getAssetUrl = require('../data/meta/asset_url'),
77
hbs = require('express-hbs');
88

9-
function asset(context, options) {
9+
function asset(path, options) {
1010
var isAdmin,
1111
minify;
1212

@@ -18,7 +18,7 @@ function asset(context, options) {
1818
minify = false;
1919
}
2020
return new hbs.handlebars.SafeString(
21-
getAssetUrl(context, isAdmin, minify)
21+
getAssetUrl(path, isAdmin, minify)
2222
);
2323
}
2424

core/server/helpers/author.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ var hbs = require('express-hbs'),
1616
utils = require('./utils'),
1717
author;
1818

19-
author = function (context, options) {
20-
if (_.isUndefined(options)) {
21-
options = context;
22-
}
23-
19+
author = function (options) {
2420
if (options.fn) {
2521
return hbs.handlebars.helpers.with.call(this, this.author, options);
2622
}

core/server/helpers/date.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,28 @@
66
var moment = require('moment'),
77
date;
88

9-
date = function (context, options) {
10-
if (!options && context.hasOwnProperty('hash')) {
11-
options = context;
12-
context = undefined;
9+
date = function (date, options) {
10+
if (!options && date.hasOwnProperty('hash')) {
11+
options = date;
12+
date = undefined;
1313

1414
// set to published_at by default, if it's available
1515
// otherwise, this will print the current date
1616
if (this.published_at) {
17-
context = this.published_at;
17+
date = this.published_at;
1818
}
1919
}
2020

2121
// ensure that context is undefined, not null, as that can cause errors
22-
context = context === null ? undefined : context;
22+
date = date === null ? undefined : date;
2323

2424
var f = options.hash.format || 'MMM Do, YYYY',
25-
timeago = options.hash.timeago,
26-
date;
25+
timeago = options.hash.timeago;
2726

2827
if (timeago) {
29-
date = moment(context).fromNow();
28+
date = moment(date).fromNow();
3029
} else {
31-
date = moment(context).format(f);
30+
date = moment(date).format(f);
3231
}
3332
return date;
3433
};

core/server/helpers/encode.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
//
55
// Returns URI encoded string
66

7-
var hbs = require('express-hbs'),
7+
var hbs = require('express-hbs'),
88
encode;
99

10-
encode = function (context, str) {
11-
var uri = context || str;
10+
encode = function (string, options) {
11+
var uri = string || options;
1212
return new hbs.handlebars.SafeString(encodeURIComponent(uri));
1313
};
1414

core/server/helpers/excerpt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Defaults to words="50"
77

88
var hbs = require('express-hbs'),
9-
_ = require('lodash'),
9+
_ = require('lodash'),
1010
getMetaDataExcerpt = require('../data/meta/excerpt');
1111

1212
function excerpt(options) {

core/server/helpers/foreach.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ var hbs = require('express-hbs'),
1010
hbsUtils = hbs.handlebars.Utils,
1111
foreach;
1212

13-
foreach = function (context, options) {
13+
foreach = function (itemType, options) {
1414
if (!options) {
1515
errors.logWarn(i18n.t('warnings.helpers.foreach.iteratorNeeded'));
1616
}
1717

1818
var fn = options.fn,
1919
inverse = options.inverse,
2020
columns = options.hash.columns,
21-
length = _.size(context),
21+
length = _.size(itemType),
2222
limit = parseInt(options.hash.limit, 10) || length,
2323
from = parseInt(options.hash.from, 10) || 1,
2424
to = parseInt(options.hash.to, 10) || (from - 1) + limit,
@@ -30,8 +30,8 @@ foreach = function (context, options) {
3030
contextPath = hbsUtils.appendContextPath(options.data.contextPath, options.ids[0]) + '.';
3131
}
3232

33-
if (hbsUtils.isFunction(context)) {
34-
context = context.call(this);
33+
if (hbsUtils.isFunction(itemType)) {
34+
itemType = itemType.call(this);
3535
}
3636

3737
if (options.data) {
@@ -55,9 +55,9 @@ foreach = function (context, options) {
5555
}
5656
}
5757

58-
output = output + fn(context[field], {
58+
output = output + fn(itemType[field], {
5959
data: data,
60-
blockParams: hbsUtils.blockParams([context[field], field], [contextPath + field, null])
60+
blockParams: hbsUtils.blockParams([itemType[field], field], [contextPath + field, null])
6161
});
6262
}
6363

@@ -79,8 +79,8 @@ foreach = function (context, options) {
7979
});
8080
}
8181

82-
if (context && typeof context === 'object') {
83-
iterateCollection(context);
82+
if (itemType && typeof itemType === 'object') {
83+
iterateCollection(itemType);
8484
}
8585

8686
if (length === 0) {

core/server/helpers/get.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ pathAliases = {
2525
/**
2626
* ## Is Browse
2727
* Is this a Browse request or a Read request?
28-
* @param {Object} context
28+
* @param {Object} resource
2929
* @param {Object} options
3030
* @returns {boolean}
3131
*/
32-
function isBrowse(context, options) {
32+
function isBrowse(resource, options) {
3333
var browse = true;
3434

3535
if (options.id || options.slug) {
@@ -85,18 +85,18 @@ function parseOptions(data, options) {
8585

8686
/**
8787
* ## Get
88-
* @param {Object} context
88+
* @param {Object} resource
8989
* @param {Object} options
9090
* @returns {Promise}
9191
*/
92-
get = function get(context, options) {
92+
get = function get(resource, options) {
9393
options = options || {};
9494
options.hash = options.hash || {};
9595
options.data = options.data || {};
9696

9797
var self = this,
9898
data = hbs.handlebars.createFrame(options.data),
99-
apiOptions = _.omit(options.hash, 'context'),
99+
apiOptions = options.hash,
100100
apiMethod;
101101

102102
if (!options.fn) {
@@ -105,28 +105,28 @@ get = function get(context, options) {
105105
return Promise.resolve();
106106
}
107107

108-
if (!_.contains(resources, context)) {
108+
if (!_.contains(resources, resource)) {
109109
data.error = i18n.t('warnings.helpers.get.invalidResource');
110110
errors.logWarn(data.error);
111111
return Promise.resolve(options.inverse(self, {data: data}));
112112
}
113113

114114
// Determine if this is a read or browse
115-
apiMethod = isBrowse(context, apiOptions) ? api[context].browse : api[context].read;
115+
apiMethod = isBrowse(resource, apiOptions) ? api[resource].browse : api[resource].read;
116116
// Parse the options we're going to pass to the API
117117
apiOptions = parseOptions(this, apiOptions);
118118

119119
return apiMethod(apiOptions).then(function success(result) {
120120
var blockParams;
121121

122122
// If no result is found, call the inverse or `{{else}}` function
123-
if (_.isEmpty(result[context])) {
123+
if (_.isEmpty(result[resource])) {
124124
return options.inverse(self, {data: data});
125125
}
126126

127127
// block params allows the theme developer to name the data using something like
128128
// `{{#get "posts" as |result pageInfo|}}`
129-
blockParams = [result[context]];
129+
blockParams = [result[resource]];
130130
if (result.meta && result.meta.pagination) {
131131
result.pagination = result.meta.pagination;
132132
blockParams.push(result.meta.pagination);
@@ -143,7 +143,7 @@ get = function get(context, options) {
143143
});
144144
};
145145

146-
module.exports = function getWithLabs(context, options) {
146+
module.exports = function getWithLabs(resource, options) {
147147
var self = this,
148148
errorMessages = [
149149
i18n.t('warnings.helpers.get.helperNotAvailable'),
@@ -153,7 +153,7 @@ module.exports = function getWithLabs(context, options) {
153153

154154
if (labs.isSet('publicAPI') === true) {
155155
// get helper is active
156-
return get.call(self, context, options);
156+
return get.call(self, resource, options);
157157
} else {
158158
errors.logError.apply(this, errorMessages);
159159
return Promise.resolve(function noGetHelper() {

core/server/helpers/navigation.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ navigation = function (options) {
1616
currentUrl = options.data.root.relativeUrl,
1717
self = this,
1818
output,
19-
context;
19+
data;
2020

2121
if (!_.isObject(navigationData) || _.isFunction(navigationData)) {
2222
return errors.logAndThrowError(i18n.t('warnings.helpers.navigation.invalidData'));
@@ -62,9 +62,9 @@ navigation = function (options) {
6262
return out;
6363
});
6464

65-
context = _.merge({}, {navigation: output});
65+
data = _.merge({}, {navigation: output});
6666

67-
return template.execute('navigation', context, options);
67+
return template.execute('navigation', data, options);
6868
};
6969

7070
module.exports = navigation;

core/server/helpers/page_url.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var config = require('../config'),
1515
page_url,
1616
pageUrl;
1717

18-
page_url = function (context, block) {
18+
page_url = function (pageNum, options) {
1919
/*jshint unused:false*/
2020
var url = config.paths.subdir;
2121

@@ -27,8 +27,8 @@ page_url = function (context, block) {
2727
url += '/' + config.routeKeywords.author + '/' + this.authorSlug;
2828
}
2929

30-
if (context > 1) {
31-
url += '/' + config.routeKeywords.page + '/' + context;
30+
if (pageNum > 1) {
31+
url += '/' + config.routeKeywords.page + '/' + pageNum;
3232
}
3333

3434
url += '/';
@@ -44,13 +44,13 @@ page_url = function (context, block) {
4444
// Returns the URL for the page specified in the current object
4545
// context. This helper is deprecated and will be removed in future versions.
4646
//
47-
pageUrl = function (context, block) {
47+
pageUrl = function (pageNum, options) {
4848
errors.logWarn(i18n.t('warnings.helpers.page_url.isDeprecated'));
4949

5050
/*jshint unused:false*/
5151
var self = this;
5252

53-
return page_url.call(self, context, block);
53+
return page_url.call(self, pageNum, options);
5454
};
5555

5656
module.exports = page_url;

0 commit comments

Comments
 (0)