Skip to content

Commit

Permalink
Add Handlebar helpers to export Google Tag Manager Datalayer & Add Ha…
Browse files Browse the repository at this point in the history
…ndlebar helpers for data dumping
  • Loading branch information
fboes committed Aug 30, 2018
1 parent 46c5a41 commit bf71853
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ For detailed information check [Blogophon's releases on Github](https://github.c
* :gift: Add [abc music notation](http://abcnotation.com/wiki/abc:standard:v2.1) syntax highlighter
* :gift: Add [Exapunks' AXIOM](http://www.zachtronics.com/exapunks/) syntax highlighter
* :gift: Add more information to `opensearch.xml`
* :gift: Add Handlebar helpers to export Google Tag Manager Datalayer
* :gift: Add Handlebar helpers for data dumping

1.5.0
-----
Expand Down
4 changes: 3 additions & 1 deletion docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ Last but not least have a look at [`lib/helpers/blogophon-handlebars-quoters.js`
<!-- Test if two strings are equal -->
{{#ifEquals tag 'new'}}It's new!{{/ifEquals}}

<!-- Test if first string matches against rgeular expression -->
<!-- Test if first string matches against regular expression -->
{{#ifMatches tag '[Nn]ew'}}It's new!{{/ifMatches}}

<!-- Export template variables to Javascript (e.g. for Google Tag Manager) -->
<script>{{{dataLayer post.meta}}}</script>
```

### Favicons
Expand Down
27 changes: 27 additions & 0 deletions lib/helpers/blogophon-handlebars-quoters.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,33 @@ const blogophonHandlebarsQuoters = {
return superString(text).niceShorten(maxChars, replaceString);
},

/**
* [description]
* @param {Object} dumpData [description]
* @param {String} options [description]
* @return {String} [description]
*/
consoleDump: function(dumpData, options) {
let html = '<script>';
html += 'window.dumpData = window.dumpData || [];';
html += 'window.dumpData.push(' + options.fn(JSON.stringify(dumpData)) + ');';
html += 'console.log(window.dumpData[window.dumpData.length - 1]);';
html += '</script>';
return html;
},

/**
* [description]
* @param {Object} dataLayer [description]
* @param {String} options) [description]
* @return {String} [description]
*/
dataLayer: function(dataLayer, options) {
let html = 'window.dataLayer = window.dataLayer || [];';
html += 'window.dataLayer.push(' + options.fn(JSON.stringify(dataLayer)) + ');';
return html;
},

/**
* @see https://stackoverflow.com/questions/34252817/handlebarsjs-check-if-a-string-is-equal-to-a-value
* @param {String} haystack [description]
Expand Down

0 comments on commit bf71853

Please sign in to comment.