mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Merge pull request #1504 from valadaptive/store-compiled-templates
Cache compiled Handlebars templates
This commit is contained in:
@ -526,14 +526,17 @@ function getUrlSync(url, cache = true) {
|
|||||||
}).responseText;
|
}).responseText;
|
||||||
}
|
}
|
||||||
|
|
||||||
const templateCache = {};
|
const templateCache = new Map();
|
||||||
|
|
||||||
export function renderTemplate(templateId, templateData = {}, sanitize = true, localize = true, fullPath = false) {
|
export function renderTemplate(templateId, templateData = {}, sanitize = true, localize = true, fullPath = false) {
|
||||||
try {
|
try {
|
||||||
const pathToTemplate = fullPath ? templateId : `/scripts/templates/${templateId}.html`;
|
const pathToTemplate = fullPath ? templateId : `/scripts/templates/${templateId}.html`;
|
||||||
const templateContent = (pathToTemplate in templateCache) ? templateCache[pathToTemplate] : getUrlSync(pathToTemplate);
|
let template = templateCache.get(pathToTemplate);
|
||||||
templateCache[pathToTemplate] = templateContent;
|
if (!template) {
|
||||||
const template = Handlebars.compile(templateContent);
|
const templateContent = getUrlSync(pathToTemplate);
|
||||||
|
template = Handlebars.compile(templateContent);
|
||||||
|
templateCache.set(pathToTemplate, template);
|
||||||
|
}
|
||||||
let result = template(templateData);
|
let result = template(templateData);
|
||||||
|
|
||||||
if (sanitize) {
|
if (sanitize) {
|
||||||
|
Reference in New Issue
Block a user