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
571 stars 81 forks source link

Partials does not work in Electron #192

Closed jenstornell closed 3 years ago

jenstornell commented 4 years ago

I have placed the file mypartial.html in the root of Electron which runs with node.

Uncaught Squirrelly Error: Could not fetch template "./mypartial.html"

var Sqrl = require("squirrelly");
var results = Sqrl.render(
      `
      {{@include("./mypartial.html")/}}
      Hi {{it.user}}
    `,
      { user: "Ada Lovelace" }
);
console.log(results);
nebrelbug commented 4 years ago

Hmm, I'm not sure why this would be happening.

Can you try doing {{@includeFile("mypartial.html")/}}?

I'm guessing Electron uses a different root folder.

VictorAyalaMX commented 3 years ago

I had the same issue with nodejs, In my case the confusion happened because this doc page never mentions we are supposed to define partials in advance during Squirrelly setup:

let Sqrl = require("squirrelly");
app.set('view engine', 'squirrelly')
app.set('views', './views')
Sqrl.templates.define("footer", Sqrl.compile("This is my footer"));

The above is explained in the last page of the documentation.

@nebrelbug Question: How can I define a Partial from an existing template file without having to open the file template file to read and pass all the content as a string value?

Thank you.

nebrelbug commented 3 years ago

Hi @VictorAyalaMX, great question (and sorry for the poor documentation).

Actually, you should be able to use {{@ includeFile('../footer.html', {data: ...}) /}}. Note that for this to work you'll have to pass the views option in the configuration of the template file you're rendering (if you're using Express this works automatically)

Embarrassingly, it looks like I didn't actually document that at all :grimacing:

Anyway, hope that helps :smile:.