Class HandlebarsTemplateCompilerTask

java.lang.Object
com.mohamnag.jstatic.plugins.handlebars_template_compiler.HandlebarsTemplateCompilerTask
All Implemented Interfaces:
BuildTask

public class HandlebarsTemplateCompilerTask extends Object implements BuildTask
Compiles each page's data using defined template and puts the result under HandlebarsTemplateCompilerConfig.getTargetDataField() data field. Template name is looked up from data field named HandlebarsTemplateCompilerConfig.getTemplateDataField() ()} either on page's data or from its parent nodes all the way up using first value found. If no value is found, will use default template defined in HandlebarsTemplateCompilerConfig.getDefaultTemplate().

Templates' context has the following variables available:

  • page: which is of type DataPage and is the current page being compiled
  • node: which is of type DataNode and is the parent of current page being compiled
  • root: which is of type DataNode and refers to root node of
    invalid reference
    BuildContext#getDataTree()
  • buildContext: which is of type BuildContext and is the whole build context and data
Some are just shortcuts provided for convenience.

Custom helpers might be added either as Java helpers or as JavaScript helpers.

As for Java helpers, the class name has to be passed on HandlebarsTemplateCompilerConfig.getHelpers() list. For each class, all the public instance methods are registered as helpers with same name. Those classes are expected to have zero arg constructor. Java helpers can use SLF4J for logging. This will be then appended to default application's logs and will be visible for users.

More information can be found under "Helper Source" part of external documentation.

There is also a possibility of writing helpers in JavaScript, which is mostly suitable for cases when a helper is very specific or does not make sense to be reused. JavaScript helpers can be registered by passing their file name in HandlebarsTemplateCompilerConfig.getHelpers() list. Inside the JavaScript file multiple helpers can be registered using following syntax:


   Handlebars.registerHelper('hey', function (context) {
     return 'Hi ' + context.name;
   });
 

For more info about helpers refer to external documentation

  • Constructor Details

    • HandlebarsTemplateCompilerTask

      public HandlebarsTemplateCompilerTask(Config globalConfig)
  • Method Details

    • run

      public void run(BuildContext buildContext)
      Specified by:
      run in interface BuildTask
    • underWatchInputPaths

      public List<Path> underWatchInputPaths()
      Description copied from interface: BuildTask
      If a plugin depends on file input, it shall return list of its input paths to be watched for changes. Paths can be either files or directories. In case of directories, only root path are necessary to be listed and if input path has subdirectories, they will be automatically watched.
      Specified by:
      underWatchInputPaths in interface BuildTask
    • underWatchConfigPaths

      public List<Path> underWatchConfigPaths()
      Description copied from interface: BuildTask
      If a plugin depends on config files in addition to the general config file of the application, like functionality extensions stored in files, it shall return these paths here. A change detected in these paths will result in recreation of the plugin.
      Specified by:
      underWatchConfigPaths in interface BuildTask