Class SelectedHandlebarsHelper

  • All Implemented Interfaces:
    com.github.jknack.handlebars.Helper<Object>

    @Component
    public class SelectedHandlebarsHelper
    extends BaseHelper
    The {{selected ...}} handlebars helper is used to output the selected entries within List Content Elements.

    Usage

    The selected 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 selected handlebars helper differs from the list helper, in that the returned array of entries is restricted to only those that are selected.

    Attributes

    The selected 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 (selected element="List Content Element")}}
     
       {{#if @first}}
     <ul>
       {{/if}}
     
       <li>{{name}}</li>
     
       {{#if @last}}
     </ul>
       {{/if}}
     
     {{/each}}
     

    The above example iterates over the array returned by the selected 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 entry name surrounded by an li block. Finally it outputs a closing ul tag if it's the last selected 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 (selected element="List Element")}}
     {{#if @first}}
     <ul>
     {{/if}}
     
       <li>{{name}}
     
     {{#if hasSubList}}
       {{#each (selected subList)}}
         {{#if @first}}
         <ul>
         {{/if}}
     
           <li>{{name}}</li>
     
         {{#if @last}}
         </ul>
         {{/if}}
       {{/each}}
     {{/if}}
     
       </li>
     
     {{#if @last}}
     </ul>
     {{/if}}
     {{/each}}
     
    See Also:
    ListHandlebarsHelper
    • Constructor Detail

      • SelectedHandlebarsHelper

        @Autowired
        public SelectedHandlebarsHelper​(IElementAPI elementApi)
    • Method Detail

      • apply

        public List<Map<String,​Object>> apply​(Object context,
                                                    com.github.jknack.handlebars.Options options)
        Description copied from class: BaseHelper
        The apply 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 interface com.github.jknack.handlebars.Helper<Object>
        Specified by:
        apply in class BaseHelper
        Parameters:
        context - The currently executing context.
        options - The options.
        Returns:
        The generated output.