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

Support passing a root #11

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,14 @@ describe('Accordion toggling', () => {
// Namespace support
{
'my_namespace': './some/path'
});
},
// Callback support
(Twig) => {
// Do something with Twig here.
},
// Template root folder support.
'/path/to/where/templates/live'
);
const accordionElement = container.querySelector('.accordion');
const accordion = new Accordion.Accordion(accordionElement);
accordion.init();
Expand Down
9 changes: 5 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ async function render(
twigFile,
context = {},
namespaces = {},
twigCallback = () => {}
twigCallback = () => {},
root = ''
) {
const baseElement = document.body
const container = baseElement.appendChild(document.createElement("div"))
Expand All @@ -23,7 +24,7 @@ async function render(
mountedContainers.add(container)
twigCallback(Twig)

container.innerHTML = await loadTemplate(twigFile, context, namespaces)
container.innerHTML = await loadTemplate(twigFile, context, namespaces, root)

return {
container,
Expand All @@ -38,7 +39,7 @@ async function render(
}
}

const loadTemplate = async (file, context = {}, namespaces) => {
const loadTemplate = async (file, context = {}, namespaces, root) => {
Twig.registryReset = () => {
Twig.Templates.registry = {}
}
Expand Down Expand Up @@ -80,7 +81,7 @@ const loadTemplate = async (file, context = {}, namespaces) => {
})
}
return Twig.twigAsync({
path: file,
path: root ? `${root}/${file}` : file,
}).then((template) => {
if (!context.hasOwnProperty("attributes")) {
context.attributes = new DrupalAttribute()
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/bar.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hey there :wave:
1 change: 1 addition & 0 deletions tests/fixtures/foo.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% include "bar.twig" %}
6 changes: 6 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,10 @@ describe('Test library by testing an accordion', () => {
const accordionElement = container.querySelector('.accordion');
expect(accordionElement.dataset.foo).toEqual('bar');
});

it('Can nominate a root', async () => {
const { getByText } = await render('foo.twig',
{}, {}, () => {}, './tests/fixtures');
expect(getByText('Hey there :wave:')).toBeTruthy();
});
});