Class ListByIdHandlebarsHelper
- java.lang.Object
-
- com.terminalfour.publish.handlebars.helper.BaseHelper
-
- com.terminalfour.publish.handlebars.helper.list.ListByIdHandlebarsHelper
-
- All Implemented Interfaces:
com.github.jknack.handlebars.Helper<Object>
@Component public class ListByIdHandlebarsHelper extends BaseHelper
The {{listById ...}} handlebars helper is used in the output of Predefined Lists directly.Usage
The listById handlebars helper is used to output Predefined Lists directly. Because of the iterative nature of lists, outputting them can seem daunting at first.
The listById handlebars helper returns an array of the entries in the list, which can be iterated over using the core handlebars each helper.
Attributes
The listById handlebars helper takes a single attribute.
- id - The id of the Predefined 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 by default, 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 (listById id="54")}} {{#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 listById 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 (listById id="54")}} {{#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 ListByIdHandlebarsHelper(IListAPI listApi, IElementAPI elementApi)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Object
apply(Object context, com.github.jknack.handlebars.Options options)
Theapply
method is the main entry point for handlebars.java helpers.
-
-
-
Constructor Detail
-
ListByIdHandlebarsHelper
@Autowired public ListByIdHandlebarsHelper(IListAPI listApi, IElementAPI elementApi)
-
-
Method Detail
-
apply
public Object apply(Object context, com.github.jknack.handlebars.Options options) throws IOException
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.
- Throws:
IOException
- If processing of the expressions fails.
-
-