Skip to content

Commit

Permalink
feature: handlebarsJS helper raw (ommit compile server side)
Browse files Browse the repository at this point in the history
  • Loading branch information
wonknu committed Oct 13, 2016
1 parent c5f11d7 commit 261c712
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
31 changes: 31 additions & 0 deletions docs/abe-handlebars-helper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Raw helper

## usage (in template) :
```
{{{{raw}}}}
{{someVariable}}
{{{{/raw}}}}
```
someVariable won't compile server side only client side when injected on the iframe or when the iframe reload.

## Use case
This can be useful when working with inline script inside template, for exemple when a JS array is updated with abe-each.

template.html
```
{{#each objs}}
​ {{abe type='text' key='objs.item' desc='some item' visible='false' reload='true'}} // note that there is a reload attr
{{/each}}
{{{{raw}}}}
// objs = [{item: 1}, {item: 2}, {item: 3}]
var myArray = [
{{#each objs}}
{{item}},
{{/each objs}}
];
{{{{/raw}}}}
```

Everything inside raw bloc will be compiled client side and as the reload attribut is on the json that will be used is the one client side (not the one saved inside a json file)
when a new item will be added or removed from abe form *objs* inside the json file won't have changed but as it compiled on the client after reloading the iframe myArray will contains as much item as there is on objs each bloc
12 changes: 12 additions & 0 deletions src/cli/cms/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export default class Page {
// ie. <title><abe>{{abe type='text' key='meta_title' desc='Meta title' tab='Meta' order='4000'}}</abe></title>
this._encloseAbeTag()
}
else {
this._removeHandlebarsRawFromHtml()
}

// je rajoute les index pour chaque bloc lié à un each
this._indexEachBlocks()
Expand Down Expand Up @@ -285,6 +288,15 @@ export default class Page {
return this
}

/**
* remove {{{{raw}}}}{{{{/raw}}}} from html
*/
_removeHandlebarsRawFromHtml() {
this.template = this.template.replace(/\{\{\{\{\/?raw\}\}\}\}/g, '')

return this
}

_addSource(json) {
var listReg = /({{abe.*type=[\'|\"]data.*}})/g
var match
Expand Down
3 changes: 3 additions & 0 deletions src/cli/cms/editor/handlebars/raw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function raw(obj) {
return obj.fn(this).replace(/\[\[/g, '{{').replace(/\]\]/g, '}}');
}
5 changes: 4 additions & 1 deletion src/cli/cms/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import recursivePrintConfig from './handlebars/recursivePrintConfig'
import sourceAttr from './handlebars/sourceAttr'
import sourceAutocomplete from './handlebars/sourceAutocomplete'
import sourceOption from './handlebars/sourceOption'
import raw from './handlebars/raw'

Handlebars.registerHelper('abeImport', abeImport)
Handlebars.registerHelper('abe', compileAbe)
Expand All @@ -24,6 +25,7 @@ Handlebars.registerHelper('listPage', listPage)
Handlebars.registerHelper('printBlock', printBlock)
Handlebars.registerHelper('printConfig',printConfig)
Handlebars.registerHelper('printInput', printInput)
Handlebars.registerHelper('raw', raw)

HandlebarsIntl.registerWith(Handlebars)

Expand All @@ -41,6 +43,7 @@ export {
recursivePrintConfig,
sourceAttr,
sourceAutocomplete,
sourceOption
sourceOption,
raw
}

5 changes: 5 additions & 0 deletions src/server/public/scripts/modules/EditorReload.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*global document, window */

import Handlebars from 'handlebars'
import Nanoajax from 'nanoajax'
import qs from 'qs'
import Json from '../modules/EditorJson'
Expand Down Expand Up @@ -58,6 +59,8 @@ export default class Reload {

var doc = iframe.contentWindow.document
str = str.replace(/<\/head>/, '<base href="/" /></head>')
var template = Handlebars.compile(str, {noEscape: true})
str = template(json)
doc.open()
doc.write(str)
doc.close()
Expand Down Expand Up @@ -88,6 +91,8 @@ export default class Reload {
},
(code, responseText) => {
if(typeof responseText !== 'undefined' && responseText !== null) {
var template = Handlebars.compile(responseText, {noEscape: true})
responseText = template(json)
this.inject(responseText)
}

Expand Down

0 comments on commit 261c712

Please sign in to comment.