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

Render a template with includes #5

Open
emileenoh opened this issue Apr 20, 2021 · 11 comments
Open

Render a template with includes #5

emileenoh opened this issue Apr 20, 2021 · 11 comments

Comments

@emileenoh
Copy link

I'd like to test a twig file that extends another twig file in my templates directory. Jest throws an error because it can't find the base template. Is there a way to configure the default namespace with main or "." without setting up a custom Twig namespace?

@larowlan
Copy link
Owner

Can you share an example of your template and folder structure? I should be able to use that to write a test-case.

@rbnzdave
Copy link

rbnzdave commented Feb 1, 2022

Same issues here.

const { container} = await render(
'./templates/pages/bin/index.html.twig',
{
headerIcon: ''
}
)

Base templates are common, and with a large enough project, feature templates naturally can end up in their own folders.

The render doesnt provide a facility to indicate what the root template folder is, so if i have base.html.twig in the templates folder, and custom.html.twig in a feature folder off templates, with the test library you cant have {% extends 'base.html.twig' %} in the custom template, whilst this is fine when running the template in symfony for instance.

I was able to workaround to get onto other errors by making the path relative in the extends, such as '../../base.html.twig' but I doubt this would work for symfony as its expecting things to be in the template folder. (plus in symfony I'm rendering 'pages/bin/index' which refers to '/templates/pages/bin/index.html.twig')

a param to specify the base dir perhaps.

@larowlan
Copy link
Owner

larowlan commented Feb 1, 2022

I'm not too familiar with twig in symfony, but happy to take suggestions on how to solve this. Under the hood it's using twig.js so we're reliant on what it supports in terms of include paths, namespaces being the option we already support - but there may be more

@rbnzdave
Copy link

rbnzdave commented Feb 1, 2022

When twig is used in symphony, the code is essentially running from the project root. When its getting templates, it considers everything to be based in a templates directory. As it stands, when the testing lib loads a template, any references its making with extends are treating the file location to be the root, so while ....\base.html.twig works from the test, that will break the app. Needs to be relative to template folder, not the location of any particular template.

@rbnzdave
Copy link

rbnzdave commented Feb 1, 2022

Ok post messed that up. .. \ .. \ base.html.twig. this will break the original app but is accepted in the test.

@larowlan
Copy link
Owner

larowlan commented Feb 2, 2022

Give #11 a try

@rbnzdave
Copy link

rbnzdave commented Feb 2, 2022

Ooooh. Will be sure to give it a bash in a bit. Thanks for progressing things along.

@rbnzdave
Copy link

rbnzdave commented Feb 2, 2022

Ok so the source and the index.js in dist are vastly different, how do i give #11 a try exactly ?

@larowlan
Copy link
Owner

larowlan commented Feb 2, 2022

running npm run build from the root of this project should generate dist/index.js for you

@rbnzdave
Copy link

rbnzdave commented Feb 2, 2022

Ah right

@joseprest
Copy link

Same issues here.

const { container} = await render( './templates/pages/bin/index.html.twig', { headerIcon: '' } )

Base templates are common, and with a large enough project, feature templates naturally can end up in their own folders.

The render doesnt provide a facility to indicate what the root template folder is, so if i have base.html.twig in the templates folder, and custom.html.twig in a feature folder off templates, with the test library you cant have {% extends 'base.html.twig' %} in the custom template, whilst this is fine when running the template in symfony for instance.

I was able to workaround to get onto other errors by making the path relative in the extends, such as '../../base.html.twig' but I doubt this would work for symfony as its expecting things to be in the template folder. (plus in symfony I'm rendering 'pages/bin/index' which refers to '/templates/pages/bin/index.html.twig')

a param to specify the base dir perhaps.

@rbnzdave
I had exactly same issue.
Currently, I am using twig-testing-library in Symfony with Jest.
Unable to find template file templates\patient\base2.html.twig. Error: ENOENT: no such file or directory, stat 'templates\patient\base2.html.twig'

How can I solve that issue? 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants