Skip to content
This repository has been archived by the owner on Jul 22, 2020. It is now read-only.

Allow Twig paths to be compatible with PHP Twig #29

Closed
BrianWalters opened this issue Jun 25, 2019 · 4 comments
Closed

Allow Twig paths to be compatible with PHP Twig #29

BrianWalters opened this issue Jun 25, 2019 · 4 comments

Comments

@BrianWalters
Copy link

Here's my use case: I want a Fractal instance to be a component library for a Symfony project. I would like to use the same Twig templates in the Symfony codebase and in the Fractal codebase.

My problem is Fractal has a very opinionated way of including templates. You don't use a filesystem path, like PHP Twig does, instead you use a Fractal handle. If I had this directory structure:

└── components
    └── button
        └── button.twig

To include the button component, I would have to do:

{% include '@button' %}

but I want to configure Fractal to include it like:

{% include 'button/button.twig %}

so that I could use the same templates with an instance of PHP Twig.

I could get around this by making a custom Twig Loader for PHP Twig, like what these guys did for Drupal 8. But I'd rather not have to introduce this complexity and requirement for using this component library. And this seems like a fairly common use case for PHP devs. Is there any way around this? Could the Twig adapter be extended to handle this use case?

@BrianWalters
Copy link
Author

I figured out a workaround for my issue. Looks like Fractal does have support for paths, but twigjs does compute those paths to be relative sometimes. I found this issue twigjs/twig.js#73 that does provide a way for twigjs to not use relative paths. Anyway, gonna make a PR for this and add a config option.

@Chapabu
Copy link
Member

Chapabu commented Aug 4, 2019

We've had this issue on Drupal 8 projects in the past. If I remember correctly, we resolved it by using an array of templates (in the docs here, right at the bottom). I think it looked something similar to:

include ['@button', 'buttons/button.html.twig`]

The first path doesn't resolve in Drupal, so it falls through to the second which does.

I'd have to confirm this with a colleague though (@iwright?)

@iwright
Copy link

iwright commented Aug 5, 2019

Hello,

As you have noticed, twig works slight different between the languages. Below are how we have worked with Drupal 8 template fallbacks and Fractal fallbacks. Please note, the Drupal 8 version is actually to make the theme child friendly too.

For Drupal, we also use the components module so you can set the correct namespace. https://www.drupal.org/project/components

Fractal:
Node twig 1.11.0 and below doesn't support template array syntax.

{% if button %}
  {% set button_cta = {
    cta: button.cta,
    type: button.type,
    href: button.href,
    text: button.text,
    external: button.external,
    classes: button.classes
  } %}
  
  {# Fractal #}
  {% include '@buttons' ignore missing with button_cta %}

  {# Drupal #}
  {% include '@atoms/buttons/buttons.twig' ignore missing with button_cta %}
{% endif %}

Drupal 8:

{{ include([
  directory ~ '/templates/includes/footer.html.twig',
  '@base_theme/includes/footer.html.twig'
]) }}

I hope this helps,

@Chapabu Chapabu closed this as completed Dec 5, 2019
@Chapabu
Copy link
Member

Chapabu commented Dec 5, 2019

I think this one was fixed by #30. If not, then please feel free to re-open :)

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

No branches or pull requests

3 participants