squirrellyjs / squirrelly

Semi-embedded JS template engine that supports helpers, filters, partials, and template inheritance. 4KB minzipped, written in TypeScript ⛺
https://squirrelly.js.org
MIT License
592 stars 82 forks source link

Is there a way to disable template caching/precompilation on renderFile()? #115

Closed tabarra closed 4 years ago

tabarra commented 5 years ago

I am currently using the renderFile() function but would like to have squirrelly go get the file each time I call the function.

For now I'm using the following "hack":

const Sqrl = require("squirrelly");
const util = require('util');
const readFile = util.promisify(fs.readFile);

async function renderTemplate(view, data){
    let rawTemplate = await readFile(`../public/${view}.html`);
    return Sqrl.Render(rawTemplate.toString(), data); 
}

console.log(renderTemplate('testview', {msg: 'Hello World'}));

But it would be good if the renderFile() could have "no cache" as the third parameter.

nebrelbug commented 5 years ago

Thanks for the question @tabarra!

Actually, there should be a way to do this already.

In your data, you can set $cache to false. So, for example, you can do:

console.log(renderFile('testview', {msg: 'Hello World', $cache: false}));

Let me know if this solves your problem, and if you have any more questions!

tabarra commented 5 years ago

Hey @nebrelbug,
I'm having trouble with the cache option.

Consider test.js:

const Sqrl = require("squirrelly");

setInterval(() => {
    let renderData = {
        $cache: false,
        msg: Math.random()
    }
    let result = Sqrl.renderFile('template.txt', renderData);
    console.log(result);
}, 1000);

And template.txt:

aaa {{msg}} bbbb

I'm getting TypeError: Cannot read property 'slice' of undefined when cache is set to false.
Screenshot of the terminal: https://i.imgur.com/PbjyMvm.png

Currently using squirrelly@7.1.3 btw.

nebrelbug commented 5 years ago

Hi @tabarra, I figured out the problem! I'll try and push a fix by the middle of next week.

Thanks for the great questions and feedback!

nebrelbug commented 4 years ago

Hi @tabarra! With Squirrelly 7.2.0, this issue should be fixed.

As warning, however, your custom function may be faster than renderFile because renderFile isn't async.

Hope this helps!