frctl / twig

Use Twig templates with Fractal.
32 stars 34 forks source link

fractal.components.engine doesn't set the 'engine' config value #6

Closed fvsch closed 4 years ago

fvsch commented 7 years ago

Very minor "bug" here I guess. Using 1.0.0-beta.2 and @frctl/fractal 1.1.7, with this config:

const fractal = module.exports = require('@frctl/fractal').create();
fractal.components.engine(require('@frctl/twig')());
fractal.components.set('ext', '.twig');

Twig templates are working alright, but the _config.components.engine value is still set to "@frctl/handlebars":

{{ dump(_config.components.engine) }}
{# string(17) "@frctl/handlebars" #}

Instead if I use this config, the value will be correct:

const fractal = module.exports = require('@frctl/fractal').create();
fractal.components.set('engine', '@frctl/twig');
fractal.components.set('ext', '.twig');
mihkeleidast commented 5 years ago

we could solve this by adding a name property to the returned object in the adapter like so:

name: '@frctl/twig',

we could also get the name automatically if the engine is registered via string, e.g. fractal.components.engine('@frctl/twig');, but there's no way to get it when object is passed in.

and then do a conditional set in fractal core:

if (adapter.name) {
  this._app.components.set('engine', adapter.name);
}

so this would need a change in core & in all the adapters as none of them can currently tell what the package name is.

alternatively we could also set the config value in the adapter class constructor (as app is passed in to the constructor), so core changes would not be necessary: this._app.components.set('engine', '@frctl/twig');


i feel like core should be responsible for updating the config value when the engine is registered, but since it's a oneliner to do it directly in the adapter, maybe that's the better way forward.

willing to fix all the adapters if we agree on the most correct solution.

mihkeleidast commented 5 years ago

@allmarkedup what do you think about this? how should we set the config value from the adapter?