Fractal: Render (sub)component's variant with handler defined in config

Created on 24 Apr 2017  路  8Comments  路  Source: frctl/fractal

What is a proper way to {{render ...}} sub-component with handler defined in parent-component's config?

Currently in our team we successfully solved this in a wierd way:
{{render '@handler' subComponentContext merge=true}} where subComponentContext property is defined in a YAML config as a string "@handler--variant". Note that @handler and @handler--variant should match the component e.g. name handler.

In other case we tried in eq. {{render subComponentContext}} which generates an error.

I'm not sure if our "wierd way solution" is a bug or a feature... :thinking:

Most helpful comment

For more on using lookups, see here https://github.com/frctl/fractal/issues/278#issuecomment-316977660

All 8 comments

Seems like a duplicate of #195

Hey @tlenex, I've tried to use your workaround but I couldn't understand or get your advice to work.

Steps I took:

  1. Create partial called handler.hbs
  2. Create partial my-module.hbs within which I want to use dynamic partials
  3. Specify partial variable in my-module.config.yml, e.g. dynamicPartial: '@another-partial'
  4. Call handler via {{> render '@handler' dynamicPartial merge=true}}

This does not work for me. Could you explain what you have in each of your partials possibly?

@iamkeir drop point 1, handler name was just an example for component name.

Try this:

  1. Create partial my-module.hbs and another-partial.hbs.
  2. Create config files my-module.config.yml and another-partial.config.yml
  3. another-partial needs to have an variant (docs here) in it's config file. Set that variant's name to my-variant, so the handler for this variant should be @another-partial--my-variant.

another-partial.config.yml should look like:

variants:
  - name: 'my-variant'
  1. Specify my-module's context property in my-module.config.yml, e.g. dynamicPartial: '@another-partial--my-variant'

my-module.config.yml should look like:

context:
  dynamicPartial: '@another-partial--my-variant'
  1. in my-module.hbs write {{render '@another-partial' dynamicPartial merge=true}}.

Note that in step 5 I used non-variant handler @another-partial, the variant handler lies in dynamicPartial string context property set in step 4. Both strings should match the same component and one of it's variants.
Note this "hack" only works for variants.
Note you can create another-partial--my-variant.hbs with completely new HTML template.

Thanks @tlenex, it worked! Really appreciated - but wow, how odd!

@iamkeir you're welcome, but keep in mind, that this might be a bug or rather unintended feature.

@allmarkedup @dkhuntrods could you tell, if example above is a bug or a feature? :)

Yeah, noted. I'm impressed you found it!

@tlenex What's happening there is that you're telling Fractal to render@another-partial with @another-partial--my-variant's context data. By default, any defined views (in this case, the *.hbs file) are included with a variant's context data, which is why this works.
There's no plan to change this behaviour in v1, but things will work quite differently in v2.

For more on using lookups, see here https://github.com/frctl/fractal/issues/278#issuecomment-316977660

Was this page helpful?
0 / 5 - 0 ratings

Related issues

allmarkedup picture allmarkedup  路  6Comments

julmot picture julmot  路  3Comments

fvanderwielen picture fvanderwielen  路  4Comments

patphongs picture patphongs  路  5Comments

allmarkedup picture allmarkedup  路  5Comments