nhsuk / nhsuk-prototype-kit-version-one

NHSUK Prototype Kit
https://nhsuk-prototype-kit.herokuapp.com/
MIT License
2 stars 3 forks source link

Add guide for using partials in Handlebars templates #40

Open paul-mitchell-bjss opened 6 years ago

paul-mitchell-bjss commented 6 years ago

A recent support request has revealed that it would be helpful for the kit to contain documentation on using partials in Handlebars templates. Although the example prototypes do demonstrate simple use of partials there are more advanced uses that are not obvious, such as how to prepare and render a dynamic list of partials with associated contexts.

Documenting how to create and use partials in both simple and advanced forms will help users get the best out of the kit.

paul-mitchell-bjss commented 6 years ago

Here is the solution for rendering a dynamic list of partials with associated contexts.

In the code-behind file

 input.dynamicPartials = [
    'components/local-header',
    'components/callout',
    'components/callout'
  ]
  input.dynamicPartialContexts = [
    { title: 'Dynamic Partials', text: 'Very useful feature' },
    { modifier: 'muted', title: 'First callout', text: 'It should be muted style' },
    { title: 'Second callout', text: 'It should be default style' }
  ]

In the template

{{#dynamicPartials}}
  {{> (lookup ../dynamicPartials @index) (lookup ../dynamicPartialContexts @index) }}
{{/dynamicPartials}}