Fractal: Render partial via config variable

Created on 14 Jul 2017  路  10Comments  路  Source: frctl/fractal

I was hoping to use some config data (YAML) to dynamically specify another partial to render/include.

Handlebars' documentation suggests this is possible, like so:

{{> (lookup . 'myVariable') }}

See: http://handlebarsjs.com/partials.html

This however results in The partial could not be found.

I also found #195 and #239 regarding this.

I tried using the 'workaround' specified here but no luck: https://github.com/frctl/fractal/issues/239#issue-223851818

Is this possible?

[type] documentation

Most helpful comment

Hi @iamkeir

This is definitely possible:

  1. Define the template name in your config data (note the double backslash, this escapes the component name and so prevents Fractal trying to inject context data):
context: {
  inputTemplate: '\\@input-pair'
}
  1. Render your dynamic partial in your .hbs file:
{{>(lookup . 'inputTemplate') }}

Let me know if this helps.

All 10 comments

Ah, more in depth instructions in #239 worked: https://github.com/frctl/fractal/issues/239#issuecomment-315387941

Would be good to fix the 'proper way' though.

Hi @iamkeir

This is definitely possible:

  1. Define the template name in your config data (note the double backslash, this escapes the component name and so prevents Fractal trying to inject context data):
context: {
  inputTemplate: '\\@input-pair'
}
  1. Render your dynamic partial in your .hbs file:
{{>(lookup . 'inputTemplate') }}

Let me know if this helps.

@dkhuntrods yippee, thank you so much! Although it was actually a single backslash I needed in the end: \@input-pair - two slashes threw The partial \@input-pair could not be found.

Thanks very much - @tlenex will be pleased :)

I am. Thanks @dkhuntrods ! Only one \ (backslash) is required before @.

Is this also possible using the nunjucks-adapter?

Hi, I was wondering the same as @thomasaull. Has anyone managed this with nunjucks?

I'm doing it like this, maybe there's a nicer way though.

component.config.yml

context:
  childTemplate: card--zoom
  childContext:
    foo: bar

component.nunj

{% render '@' + childTemplate, childContext %}

Thank you @LeBenLeBen. Works as intended!

@flepp, @LeBenLeBen Another way to do it is to set your childTemplate like \\@card--zoom and render it like

{% render childTemplate, childContext %}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

fvanderwielen picture fvanderwielen  路  4Comments

Maybach91 picture Maybach91  路  5Comments

gui-gui picture gui-gui  路  6Comments

sturobson picture sturobson  路  6Comments

siiron picture siiron  路  7Comments