Class ListHandlebarsHelper
- java.lang.Object
-
- com.terminalfour.publish.handlebars.helper.BaseHelper
-
- com.terminalfour.publish.handlebars.helper.content.element.ListHandlebarsHelper
-
- All Implemented Interfaces:
com.github.jknack.handlebars.Helper<Object>
@Component public class ListHandlebarsHelper extends BaseHelper
The {{list ...}} handlebars helper is used in the output of List Content Elements.Usage
The list handlebars helper is used to output lists within Content Items. Because of the iterative nature of lists, outputting them is more complicated that other Content Elements.
The list handlebars helper returns an array of the entries in the list, which can be iterated over using the core handlebars each helper.
Attributes
The list handlebars helper takes a single attribute.
- element - The Content Element that contains the list.
Variables
When iterating a list entry, there are a number of variables made available.
- listId - The Id of the list that this entry belongs to.
- listName - The name of the list that this entry belongs to.
- entryId - The Id of this list entry.
- name - The name of this list entry.
- value - The value of this list entry.
- sequence - The sequence of this entry in the list.
- language - The language of the list entry.
- selected - true if the list entry is selected, false otherwise. This does not consider sub-lists.
- hasSubList - true if the entry contains a sub-list.
- subList - A reference to the sub-list, if one is present.
- subListId - The id of the sub-list, if present.
- subListName - The name of the sub-list, if present.
- hasSubListSelections - true if the list entry contains a sub list that has one or more selected entries,
false
otherwise.
In addition to the variables relating to each list entry returned, there are a number of variables made available to give the developer more information about the position of each entry in the list.
- @first - true if this is the first entry in the list/array.
- @last - true if this is the last entry in the list/array.
- @odd - true if this this entry has an odd index. Note that the array is zero-indexed.
- @even - true if this this entry has an even index. Note that the array is zero-indexed.
- @index - Returns the index of this entry within the list/array.
Examples
Standard list
Outputting single-level standard lists is easier than more complex cascading lists.
{{#each (list element="List Content Element")}} {{#if @first}} <ul> {{/if}} <li> {{#if selected}}<strong>{{else}}<em>{{/if}}{{name}}{{#if selected}}</strong>{{else}}</em>{{/if}} </li> {{#if @last}} </ul> {{/if}} {{/each}}
The above example iterates over the array returned by the list helper. It does this by using the #each block level helper.
For each returned entry, it outputs an opening ul tag if it's the first entry. It then outputs the name of the entry in an li tag, bolding it if the entry is selected and using italics if it is not selected. Finally it outputs a closing ul tag if it's the last entry in the list.
Cascading and multi-select lists
When outputting lists with sub-lists, the process is a bit more complex, as it requires two levels of
each
blocks.Layout code
{{#each (list element="List Element")}} {{#if @first}} <ul> {{/if}} <li> {{#if selected}}<strong>{{else}}<em>{{/if}}{{name}}{{#if selected}}</strong>{{else}}</em>{{/if}} {{#if hasSubList}} {{#each (list subList)}} {{#if @first}} <ul> {{/if}} <li> {{#if selected}}<strong>{{else}}<em>{{/if}}{{name}}{{#if selected}}</strong>{{else}}</em>{{/if}} </li> {{#if @last}} </ul> {{/if}} {{/each}} {{/if}} </li> {{#if @last}} </ul> {{/if}} {{/each}}
- See Also:
SelectedHandlebarsHelper
-
-
Constructor Summary
Constructors Constructor Description ListHandlebarsHelper(IElementAPI elementApi)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description List<Map<String,Object>>
apply(Object context, com.github.jknack.handlebars.Options options)
Theapply
method is the main entry point for handlebars.java helpers.
-
-
-
Constructor Detail
-
ListHandlebarsHelper
@Autowired public ListHandlebarsHelper(IElementAPI elementApi)
-
-
Method Detail
-
apply
public List<Map<String,Object>> apply(Object context, com.github.jknack.handlebars.Options options)
Description copied from class:BaseHelper
Theapply
method is the main entry point for handlebars.java helpers.It is called internally by the handlebars.java engine when a matching handlebars.java expression is encountered.
- Specified by:
apply
in interfacecom.github.jknack.handlebars.Helper<Object>
- Specified by:
apply
in classBaseHelper
- Parameters:
context
- The currently executing context.options
- The options.- Returns:
- The generated output.
-
-