Skip to content

Commit

Permalink
add global functions
Browse files Browse the repository at this point in the history
  • Loading branch information
redspirit committed Nov 4, 2024
1 parent 8f44004 commit 8331928
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
29 changes: 23 additions & 6 deletions Listok.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Listok {
this.sectionReg = new RegExp(`${tl}([#!])([\\da-z_.!$]+?)(\\(.*?\\))?${tr}(.*?)${tl}\\/\\2${tr}`, 'gis');

this.context = {};
this.funcContext = {};
}

isPrimitive(test) {
Expand All @@ -36,6 +37,16 @@ class Listok {
return params;
}

mergeObjects (target, source) {
const result = { ...target };
for (const key in source) {
if (source[key] !== null && source[key] !== undefined) {
result[key] = source[key];
}
}
return result;
}

getFromContext(context, key, tagType = '#') {
let ctx;
if (key === this.subKey) {
Expand All @@ -44,7 +55,9 @@ class Listok {
if(key.charAt(0) === '$') {
ctx = this.isPrimitive(this.context) ? '' : get(this.context, key.substring(1));
} else {
ctx = this.isPrimitive(context) ? '' : get(context, key);
// console.log('key', key);
// console.log('context', this.mergeObjects(context, this.funcContext));
ctx = get(this.mergeObjects(context, this.funcContext), key);
}
}
return tagType === '!' ? !ctx : ctx;
Expand Down Expand Up @@ -78,9 +91,9 @@ class Listok {
}
return multiBody;
} else if (typeof subContext === 'function') { // is function
let funcResult = subContext(this.parseFunctionParams(tagParams));
console.log('REPL innerBody', innerBody);
console.log('REPL funcResult', funcResult);
let funcResult = subContext(this.parseFunctionParams(tagParams, subContext));
// console.log('REPL innerBody', innerBody);
// console.log('REPL funcResult', funcResult);
return this.replaceSection(innerBody, funcResult);
} else { // is object
return this.parseSections(innerBody, subContext);
Expand All @@ -97,15 +110,15 @@ class Listok {
} else if (typeof value === 'string') {
return value;
} else if (typeof value === 'function') {
return value();
return value(context);
} else {
return value ? value.toString() : '';
}
}

replaceFunction(context, key, tagParams) {
if (key === 'include' && tagParams) {
console.log('INCL', key, tagParams);
// console.log('INCL', key, tagParams);
let fileName = tagParams.trim().slice(1,-1);
return this.renderFile(fileName, context, false);
}
Expand All @@ -119,6 +132,10 @@ class Listok {
}
}

defineFunction(name, fn) {
this.funcContext[name] = fn;
}

render(template, context, setGlobalContext = true) {
if (setGlobalContext) this.context = context;
return this.parseSections(template, context);
Expand Down
18 changes: 14 additions & 4 deletions test/test1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,29 @@ let listok = new Listok();

const view = {
menuTitle: 'The Blog!',
showThis: true,
copyright: 'LISTOK.js',
author: (params) => {
return 'Red Spirit ' + JSON.stringify(params);
},
booksArr: [{name: 'book 1'}, {name: 'book 2'}],
// booksObj: false,
booksObj: {name: 'For dummies )'},
booksFunc: (params) => {
return 'Zloy Awaw is ' + params.name;
booksFunc: () => {
return [
{name: 'book 1'},
{name: 'book 2'},
]
}

};

let content = listok.renderFile('./template.html', view);
listok.defineFunction('globFunc', (params) => {
return [
{name: 'book 1'},
{name: 'book 2'},
]
});

let content = listok.renderFile('test/template.html', view);
console.log('--------------------------------------------');
console.log(content);

0 comments on commit 8331928

Please sign in to comment.