java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.view.AbstractCachingViewResolver
org.springframework.web.servlet.view.UrlBasedViewResolver
org.springframework.web.servlet.view.AbstractTemplateViewResolver
com.github.jknack.handlebars.springmvc.HandlebarsViewResolver
All Implemented Interfaces:
com.github.jknack.handlebars.HelperRegistry, org.springframework.beans.factory.Aware, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.core.Ordered, org.springframework.web.context.ServletContextAware, org.springframework.web.servlet.ViewResolver

public class HandlebarsViewResolver extends org.springframework.web.servlet.view.AbstractTemplateViewResolver implements org.springframework.beans.factory.InitializingBean, com.github.jknack.handlebars.HelperRegistry
A Handlebars view resolver.
Since:
0.1
Author:
edgar.espina
  • Field Details

  • Constructor Details

    • HandlebarsViewResolver

      public HandlebarsViewResolver(Class<? extends HandlebarsView> viewClass)
      Creates a new HandlebarsViewResolver.
      Parameters:
      viewClass - The view's class. Required.
    • HandlebarsViewResolver

      public HandlebarsViewResolver()
      Creates a new HandlebarsViewResolver.
    • HandlebarsViewResolver

      public HandlebarsViewResolver(com.github.jknack.handlebars.Handlebars handlebars)
      Creates a new HandlebarsViewResolver that utilizes the parameter handlebars for the underlying template lifecycle management.
      Parameters:
      handlebars - The Handlebars instance used for template lifecycle management. Required.
    • HandlebarsViewResolver

      public HandlebarsViewResolver(com.github.jknack.handlebars.Handlebars handlebars, Class<? extends HandlebarsView> viewClass)
      Creates a new HandlebarsViewResolver that utilizes the parameter handlebars for the underlying template lifecycle management.
      Parameters:
      handlebars - The Handlebars instance used for template lifecycle management. Required.
      viewClass - The view's class. Required.
  • Method Details

    • afterPropertiesSet

      public void afterPropertiesSet()
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
    • getHandlebars

      public com.github.jknack.handlebars.Handlebars getHandlebars()
      A handlebars instance.
      Returns:
      A handlebars instance.
    • setValueResolvers

      public void setValueResolvers(com.github.jknack.handlebars.ValueResolver... valueResolvers)
      Set the value resolvers.
      Parameters:
      valueResolvers - The value resolvers. Required.
    • setFormatters

      public void setFormatters(com.github.jknack.handlebars.Formatter... formatters)
      Set variable formatters.
      Parameters:
      formatters - Formatters to add.
    • setHandlebarsJsFile

      public void setHandlebarsJsFile(String location)
      Set the handlebars.js location used it to compile/precompile template to JavaScript.

      Using handlebars.js 2.x:

       Handlebars handlebars = new Handlebars().handlebarsJsFile("handlebars-v2.0.0.js");
       

      Using handlebars.js 1.x:

       Handlebars handlebars = new Handlebars().handlebarsJsFile("handlebars-v4.7.7.js");
       
      Default handlebars.js is handlebars-v4.7.7.js.
      Parameters:
      location - A classpath location of the handlebar.js file.
    • setFailOnMissingFile

      public void setFailOnMissingFile(boolean failOnMissingFile)
      True, if the view resolver should fail on missing files. Default is: true.
      Parameters:
      failOnMissingFile - True, if the view resolver should fail on missing files. Default is: true.
    • setHelpers

      public void setHelpers(Map<String,com.github.jknack.handlebars.Helper<?>> helpers)
      Register all the helpers in the map.
      Parameters:
      helpers - The helpers to be registered. Required.
      See Also:
      • Handlebars.registerHelper(String, Helper)
    • setHelperSources

      public void setHelperSources(List<?> helpers)
      Register all the helpers in the list. Each element of the list must be a helper source.
      Parameters:
      helpers - The helpers to be registered. Required.
      See Also:
      • Handlebars.registerHelpers(Class)
      • Handlebars.registerHelpers(Object)
    • registerHelpers

      public HandlebarsViewResolver registerHelpers(Object helperSource)
      Register all the helper methods for the given helper source.

      A helper method looks like:

       public static? CharSequence methodName(context?, parameter*, options?) {
       }
       
      Where:
      • A method can/can't be static
      • The method's name became the helper's name
      • Context, parameters and options are all optionals
      • If context and options are present they must be the first and last arguments of the method
      Instance and static methods will be registered as helpers.
      Specified by:
      registerHelpers in interface com.github.jknack.handlebars.HelperRegistry
      Parameters:
      helperSource - The helper source. Required.
      Returns:
      This handlebars object.
    • registerHelpers

      public HandlebarsViewResolver registerHelpers(Class<?> helperSource)
      Register all the helper methods for the given helper source.

      A helper method looks like:

       public static? CharSequence methodName(context?, parameter*, options?) {
       }
       
      Where:
      • A method can/can't be static
      • The method's name became the helper's name
      • Context, parameters and options are all optionals
      • If context and options are present they must be the first and last arguments of the method
      Only static methods will be registered as helpers.
      Specified by:
      registerHelpers in interface com.github.jknack.handlebars.HelperRegistry
      Parameters:
      helperSource - The helper source. Required.
      Returns:
      This handlebars object.
    • helper

      public <C> com.github.jknack.handlebars.Helper<C> helper(String name)
      Specified by:
      helper in interface com.github.jknack.handlebars.HelperRegistry
    • helpers

      public Set<Map.Entry<String,com.github.jknack.handlebars.Helper<?>>> helpers()
      Specified by:
      helpers in interface com.github.jknack.handlebars.HelperRegistry
    • registerHelper

      public <H> HandlebarsViewResolver registerHelper(String name, com.github.jknack.handlebars.Helper<H> helper)
      Specified by:
      registerHelper in interface com.github.jknack.handlebars.HelperRegistry
    • registerHelperMissing

      public <H> HandlebarsViewResolver registerHelperMissing(com.github.jknack.handlebars.Helper<H> helper)
      Specified by:
      registerHelperMissing in interface com.github.jknack.handlebars.HelperRegistry
    • registerHelpers

      public HandlebarsViewResolver registerHelpers(URI location) throws Exception
      Specified by:
      registerHelpers in interface com.github.jknack.handlebars.HelperRegistry
      Throws:
      Exception
    • registerHelpers

      public HandlebarsViewResolver registerHelpers(File input) throws Exception
      Specified by:
      registerHelpers in interface com.github.jknack.handlebars.HelperRegistry
      Throws:
      Exception
    • registerHelpers

      public HandlebarsViewResolver registerHelpers(String filename, Reader source) throws Exception
      Specified by:
      registerHelpers in interface com.github.jknack.handlebars.HelperRegistry
      Throws:
      Exception
    • registerHelpers

      public HandlebarsViewResolver registerHelpers(String filename, InputStream source) throws Exception
      Specified by:
      registerHelpers in interface com.github.jknack.handlebars.HelperRegistry
      Throws:
      Exception
    • registerHelpers

      public HandlebarsViewResolver registerHelpers(String filename, String source) throws IOException
      Specified by:
      registerHelpers in interface com.github.jknack.handlebars.HelperRegistry
      Throws:
      IOException
    • withoutMessageHelper

      public HandlebarsViewResolver withoutMessageHelper()
      Same as setRegisterMessageHelper(boolean) with a false argument. The message helper wont be registered when you call this method.
      Returns:
      This handlebars view resolver.
    • setRegisterMessageHelper

      public void setRegisterMessageHelper(boolean registerMessageHelper)
      True, if the message helper (based on MessageSource) should be registered. Default is: true.
      Parameters:
      registerMessageHelper - True, if the message helper (based on MessageSource) should be registered. Default is: true.
    • setBindI18nToMessageSource

      public void setBindI18nToMessageSource(boolean bindI18nToMessageSource)
      Parameters:
      bindI18nToMessageSource - If true, the i18n helpers will use a MessageSource instead of a plain ResourceBundle. Default is: false.
    • setDeletePartialAfterMerge

      public void setDeletePartialAfterMerge(boolean deletePartialAfterMerge)
      If true, templates will be deleted once applied. Useful, in some advanced template inheritance use cases. Used by {{#block}} helper. Default is: false. At any time you can override the default setup with:
       {{#block "footer" delete-after-merge=true}}
       
      Parameters:
      deletePartialAfterMerge - True for clearing up templates once they got applied. Used by {{#block}} helper.
    • setCache

      public void setCache(boolean cache)
      Overrides:
      setCache in class org.springframework.web.servlet.view.AbstractCachingViewResolver
    • setTemplateCache

      public void setTemplateCache(com.github.jknack.handlebars.cache.TemplateCache templateCache)
      Parameters:
      templateCache - Set a template cache. Default is: HighConcurrencyTemplateCache.
    • decorator

      public com.github.jknack.handlebars.Decorator decorator(String name)
      Specified by:
      decorator in interface com.github.jknack.handlebars.HelperRegistry
    • registerDecorator

      public HandlebarsViewResolver registerDecorator(String name, com.github.jknack.handlebars.Decorator decorator)
      Specified by:
      registerDecorator in interface com.github.jknack.handlebars.HelperRegistry
    • setCharset

      public HandlebarsViewResolver setCharset(Charset charset)
      Specified by:
      setCharset in interface com.github.jknack.handlebars.HelperRegistry