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

Support passing a root #11

wants to merge 1 commit into from

Conversation

larowlan
Copy link
Owner

@larowlan larowlan commented Feb 2, 2022

Fixes #5

@rbnzdave
Copy link

rbnzdave commented Feb 2, 2022

ok so finally figured out my brainfart while trying to build it ( had literally setup an ubuntu virtualbox after git bash accepted rm but not rollup, and i failed to check if rollup was node... anyway) and have placed the dist file in my project.

After fluffing around figuring out how the root location worked, its essentially the exact same problem.

im loading a template with

         const { container} = await render(

            'pages/bin/index.html.twig',
            {
                headerIcon: '<i class="fas fa-box"></i>'
            },
            {},
            ()=> {},
            './templates/'
        )

and while it finds the initial twig file, where you have joined the root and filepath, it fails to find the template import, which is trying to extend a file from the root directory as named.

Unable to find template file templates\pages\bin\lookup.html.twig. Error: ENOENT: no such file or directory, stat 'templates\pages\bin\lookup.html.twig'

because the lookup.html.twig is in ./templates

@larowlan
Copy link
Owner Author

larowlan commented Feb 2, 2022

Does symfony have its own twig-loader that special cases the templates folder?

@rbnzdave
Copy link

rbnzdave commented Feb 2, 2022

Very likely, they made twig.

@rbnzdave
Copy link

rbnzdave commented Feb 2, 2022

From the docs, twig templates are compiled to php so extends will be mapped as it does it.

@rbnzdave
Copy link

rbnzdave commented Feb 2, 2022

are you wrapping a js library that loads them ?

@larowlan
Copy link
Owner Author

larowlan commented Feb 3, 2022

@rbnzdave
Copy link

rbnzdave commented Feb 3, 2022

Yeah I dont think it can be done, twig in php has a registered base of where templates are loaded from, and twigjs treats the first one loaded as the reference.

While this is fine for using twig in a js project, it isnt compatable with the php implementation. shame. In order to get this I'd have to modify the twigjs library to have an abilioty to process everything from a decalred root. And I just dont have the time it'll take for me to figure out how to do that.

Thanks for the attempt tho :)

@rbnzdave
Copy link

rbnzdave commented Feb 3, 2022

that being said, if i could gather the templates into an array and then just reference them by name.... tho then i'd have to ensure all paths had their own names, and for instance in the project I'm looking at now its got like 15 compoenet paths where the page ends up being called index.html.twig. So sure theres a workaround in there somewhere. maybe even being able to pass the templates in as an array with the reference names being the extends names, and then they could all be treated as if in the same place.

@larowlan
Copy link
Owner Author

larowlan commented Feb 3, 2022

Namespaces is how I deal with this in Drupal

@rbnzdave
Copy link

rbnzdave commented Feb 3, 2022

Thanks, looking into this now.

@rbnzdave
Copy link

rbnzdave commented Feb 3, 2022

is the namespace support in the testing library so i can map my namespaces or so i can reference one to use from twig.yaml ?

@larowlan
Copy link
Owner Author

larowlan commented Feb 3, 2022

the third argument to render is a map of namespaces - see the example in the readme

@rbnzdave
Copy link

rbnzdave commented Feb 3, 2022

Yeah sorry this is why I'd asked. in your example you dont make use of the namespace so I'm not sure what I'm setting up there. you define my_namespace and then dont refer to it. You dont provide the content of the example accordion.js in the brief intro docs and the files in the tests folder also define but then dont appear to use namespaces.

I'm expecting

        const { container} = await render(

            '@binlookup/index.html.twig',
            {
                headerIcon: '<i class="fas fa-box"></i>'
            },
            {
                'binlookup' : './templates/pages/bin'

            }
        )

to work, have tried with and without the @

@larowlan
Copy link
Owner Author

larowlan commented Feb 3, 2022

So here's an example

{# some-file.twig #}
<div>
  {% include "@my_namespace/foo.twig" %}
</div>
render(
  '/path/to/some-file.twig',
  {},
  {
    my_namespace: '/path/to/where/the/namespace/root/is'
  }
)

i.e you use the fixed path in the test, but in the templates it renders, it can find the includes by their namespace

@rbnzdave
Copy link

rbnzdave commented Feb 3, 2022

ohhhhhhh. right. context. Choice. thanks, please add that to your docs too, I've now got an undefined document object, but have yet to suspect its template related.

Also there doesnt appear to be any reference to this library on the more examples pages, fyi.

@larowlan
Copy link
Owner Author

larowlan commented Feb 3, 2022

Going to close this, as it seems namespaces are the better option.
Happy to accept a PR for docs updates to convey the above concept more clearly

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 this pull request may close these issues.

Render a template with includes
2 participants