Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow accessing globals from rendered liquid partials #3212

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Engines/Liquid.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Liquid extends TemplateEngine {
constructor(name, eleventyConfig) {
super(name, eleventyConfig);

this.globalsReference = this.config.globalData;
this.liquidOptions = this.config.liquidOptions || {};

this.setLibrary(this.config.libraryOverrides.liquid);
Expand Down Expand Up @@ -50,11 +51,30 @@ class Liquid extends TemplateEngine {
};

let options = Object.assign(defaults, this.liquidOptions || {});

this.mergeGlobalData();
options.globals = this.globalsReference;
// debug("Liquid constructor options: %o", options);

return options;
}

mergeGlobalData() {
for (let name in this.config.liquidOptions.globals) {
if (name in this.config.globalData) continue;
this.config.globalData[name] = this.config.liquidOptions.globals[name];
}
}

/**
* Liquid needs to receive globals in order for {% render %} to have access to them
*
* @override
**/
needsGlobals() {
return true;
}

static wrapFilter(fn) {
return function (...args) {
if (this.context && "get" in this.context) {
Expand Down Expand Up @@ -111,6 +131,16 @@ class Liquid extends TemplateEngine {
this.liquidLib.registerTag(name, tagObj);
}

addGlobals(globals) {
for (let [name, value] of Object.entries(globals)) {
this.addGlobal(name, value);
}
}

addGlobal(name, value) {
this.globalsReference[name] = value;
}

addAllShortcodes(shortcodes) {
for (let name in shortcodes) {
this.addShortcode(name, shortcodes[name]);
Expand Down
4 changes: 4 additions & 0 deletions src/Engines/TemplateEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ class TemplateEngine {
return true;
}

needsGlobals() {
return false;
}

getExtraDataFromFile() {
return {};
}
Expand Down
8 changes: 8 additions & 0 deletions src/Template.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,14 @@ class Template extends TemplateContent {
debugDev("%o getData mergedData", this.inputPath);

this._dataCache = mergedData;
if (this.templateRender.engine.needsGlobals()) {
if (typeof this.templateRender.engine.addGlobals != "function") {
throw new Error(
"Internal error: the engine's `.needsGlobals()` method returned `true`, but it does not have an `.addGlobals()` method",
);
}
this.templateRender.engine.addGlobals(globalData);
}
return mergedData;
}

Expand Down
22 changes: 20 additions & 2 deletions test/TemplateRenderLiquidTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import EleventyExtensionMap from "../src/EleventyExtensionMap.js";

import { getTemplateConfigInstance } from "./_testHelpers.js";

async function getNewTemplateRender(name, inputDir, userConfig = {}) {
async function getNewTemplateRender(name, inputDir, userConfig = {}, configCallback) {
let eleventyConfig = await getTemplateConfigInstance({
dir: {
input: inputDir
}
}, null, userConfig);
}, null, userConfig, configCallback);

let tr = new TemplateRender(name, eleventyConfig);
tr.extensionMap = new EleventyExtensionMap([], eleventyConfig);
Expand Down Expand Up @@ -1039,3 +1039,21 @@ test("Liquid Parse for Symbols", async (t) => {

t.deepEqual(engine.parseForSymbols("{{ collections.mine | test }}>"), ["collections.mine"]);
});

test("Issue 1541: global data in rendered templates passed through liquidOptions", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/", {
liquidOptions: {
globals: {globalVariable: "Hello world"}
},
});
let fn = await tr.getCompiledTemplate(`<p>{% render "globals" %}</p>`);
t.is(await fn(), "<p>Hello world</p>");
});

test("Issue 1541: global data in rendered templates passed through addGlobalData", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/", null, eleventyConfig => {
eleventyConfig.userConfig.addGlobalData('globalVariable', 'Hello world');
});
let fn = await tr.getCompiledTemplate(`<p>{% render "globals" %}</p>`);
t.is(await fn(), "<p>Hello world</p>");
});
4 changes: 3 additions & 1 deletion test/_testHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isPlainObject } from "@11ty/eleventy-utils";
import TemplateConfig from "../src/TemplateConfig.js";
import ProjectDirectories from "../src/Util/ProjectDirectories.js";

async function getTemplateConfigInstance(configObj, dirs, configObjOverride = undefined) {
async function getTemplateConfigInstance(configObj, dirs, configObjOverride = undefined, configCallback = undefined) {
let eleventyConfig;
if(configObj instanceof TemplateConfig) {
eleventyConfig = configObj;
Expand All @@ -25,6 +25,8 @@ async function getTemplateConfigInstance(configObj, dirs, configObjOverride = un

eleventyConfig.setDirectories(dirs);

configCallback?.(eleventyConfig);

await eleventyConfig.init(configObjOverride || configObj); // overrides

return eleventyConfig;
Expand Down
1 change: 1 addition & 0 deletions test/stubs/_includes/globals.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ globalVariable }}