Skip to content

Commit

Permalink
Add Swag and OpenPlans handlebars helpers
Browse files Browse the repository at this point in the history
- nlToBr for user input values by default
  • Loading branch information
atogle committed Jul 9, 2014
1 parent 93bed31 commit a0e2db2
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/sa_web/jstemplates/place-detail-survey.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h4 class="survey-header">
{{#items}}
<div class="response-item response-item-{{ name }}">
<span class="response-label response-label-{{ name }}">{{ label }}</span>
<p class="response-value response-value-{{ name }}">{{ value }}</p>
<p class="response-value response-value-{{ name }}">{{nlToBr value }}</p>
</div>
{{/items}}

Expand Down
2 changes: 1 addition & 1 deletion src/sa_web/jstemplates/place-detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h1>{{#if name }}{{ name }}{{^}}{{>location-string location }}{{/if}}</h1>
{{#each_place_item "submitter_name" "name" "location_type"}}
<div class="place-item place-item-{{ name }}">
<span class="place-label place-label-{{ name }}">{{ label }}</span>
<p class="place-value place-value-{{ name }}">{{ value }}</p>
<p class="place-value place-value-{{ name }}">{{nlToBr value }}</p>
</div>
{{/each_place_item }}
</section>
Expand Down
72 changes: 72 additions & 0 deletions src/sa_web/static/libs/handlebars-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*globals Handlebars, moment, jQuery */

(function($) {
// Get the current url
Handlebars.registerHelper('windowLocation', function(place_id) {
return window.location;
});

// Change new lines to <br> tags. This one is better than Swag.
Handlebars.registerHelper('nlToBr', function(str) {
if (str) {
str = Handlebars.Utils.escapeExpression(str);
return new Handlebars.SafeString(str.replace(/\r?\n|\r/g, '<br>'));
} else {
return str;
}
});

// Date and time ------------------------------------------------------------
Handlebars.registerHelper('formatDateTime', function(datetime, format) {
if (datetime) {
return moment(datetime).format(format);
}
return '';
});

Handlebars.registerHelper('fromNow', function(datetime) {
if (datetime) {
return moment(datetime).fromNow();
}
return '';
});

// Iteration ----------------------------------------------------------------
Handlebars.registerHelper('times', function(n, options) {
var accum = '', i;
for(i = 0; i < n; ++i){
accum += options.fn(i);
}
return accum;
});

Handlebars.registerHelper('range', function(from, to, options) {
var accum = '', i;
for(i = from; i < to; i++){
accum += options.fn(i);
}
return accum;
});

// HTML ---------------------------------------------------------------------
Handlebars.registerHelper('select', function(value, options) {
var $el = $('<div/>').html(options.fn(this)),
selectValue = function(v) {
$el.find('[value="'+v+'"]').attr({
checked: 'checked',
selected: 'selected'
});
};

if ($.isArray(value)) {
jQuery.each(function(i, v) {
selectValue(v);
});
} else {
selectValue(value);
}

return $el.html();
});

}(jQuery));
6 changes: 6 additions & 0 deletions src/sa_web/static/libs/swag.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/sa_web/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,12 @@ <h3>
<script src="{{settings.COMPRESS_URL}}libs/canvas-to-blob.min.js"></script>
<script src="{{settings.COMPRESS_URL}}libs/spin.min.js"></script>
<script src="{{settings.COMPRESS_URL}}libs/gatekeeper.js"></script>
<script src="{{settings.COMPRESS_URL}}libs/swag.min.js"></script>

<script src="{{settings.COMPRESS_URL}}js/utils.js"></script>
<script src="{{settings.COMPRESS_URL}}js/template-helpers.js"></script>
<!-- https://github.com/openplans/handlebars-helpers -->
<script src="{{settings.COMPRESS_URL}}libs/handlebars-helpers.js"></script>
{% endcompress %}

<script>
Expand Down

0 comments on commit a0e2db2

Please sign in to comment.