Class PebbleEngine.Builder

java.lang.Object
io.pebbletemplates.pebble.PebbleEngine.Builder
Enclosing class:
PebbleEngine

public static class PebbleEngine.Builder extends Object
A builder to configure and construct an instance of a PebbleEngine.
  • Field Details

    • loader

      private Loader<?> loader
    • syntax

      private Syntax syntax
    • strictVariables

      private boolean strictVariables
    • enableNewLineTrimming

      private boolean enableNewLineTrimming
    • defaultLocale

      private Locale defaultLocale
    • maxRenderedSize

      private int maxRenderedSize
    • executorService

      private ExecutorService executorService
    • templateCache

      private PebbleCache<Object,PebbleTemplate> templateCache
    • cacheActive

      private boolean cacheActive
    • tagCache

      private PebbleCache<CacheKey,Object> tagCache
    • literalDecimalTreatedAsInteger

      private boolean literalDecimalTreatedAsInteger
    • greedyMatchMethod

      private boolean greedyMatchMethod
    • literalNumbersAsBigDecimals

      private boolean literalNumbersAsBigDecimals
    • methodAccessValidator

      private MethodAccessValidator methodAccessValidator
    • factory

      private final ExtensionRegistryFactory factory
  • Constructor Details

    • Builder

      public Builder()
      Creates the builder.
  • Method Details

    • loader

      public PebbleEngine.Builder loader(Loader<?> loader)
      Sets the loader used to find templates.
      Parameters:
      loader - A template loader
      Returns:
      This builder object
    • extension

      public PebbleEngine.Builder extension(Extension... extensions)
      Adds an extension, can be safely invoked several times to add different extensions.
      Parameters:
      extensions - One or more extensions to add
      Returns:
      This builder object
    • syntax

      public PebbleEngine.Builder syntax(Syntax syntax)
      Sets the syntax to be used.
      Parameters:
      syntax - The syntax to be used
      Returns:
      This builder object
    • strictVariables

      public PebbleEngine.Builder strictVariables(boolean strictVariables)
      Changes the strictVariables setting of the PebbleEngine. The default value of this setting is "false".

      The following examples will all print empty strings if strictVariables is false but will throw exceptions if it is true:

      {{ nonExistingVariable }} {{ nonExistingVariable.attribute }} {{ nullVariable.attribute }} {{ existingVariable.nullAttribute.attribute }} {{ existingVariable.nonExistingAttribute }} {{ array[-1] }}
      Parameters:
      strictVariables - Whether or not strict variables is used
      Returns:
      This builder object
    • newLineTrimming

      public PebbleEngine.Builder newLineTrimming(boolean enableNewLineTrimming)
      Changes the newLineTrimming setting of the PebbleEngine. The default value of this setting is "true".

      By default, Pebble will trim a newline that immediately follows a Pebble tag. For example, {{key1}}\n{{key2}} will have the newline removed.

      If newLineTrimming is set to false, then the first newline following a Pebble tag won't be trimmed. All newlines will be preserved

      Parameters:
      enableNewLineTrimming - Whether or not the newline should be trimmed.
      Returns:
      This builder object
    • defaultLocale

      public PebbleEngine.Builder defaultLocale(Locale defaultLocale)
      Sets the Locale passed to all templates constructed by this PebbleEngine.

      An individual template can always be given a new locale during evaluation.

      Parameters:
      defaultLocale - The default locale
      Returns:
      This builder object
    • maxRenderedSize

      public PebbleEngine.Builder maxRenderedSize(int maxRenderedSize)
      Sets the maximum size of the rendered template to protect against macro bombs. See for example https://github.com/PebbleTemplates/pebble/issues/526. If the rendered template exceeds this limit, then a PebbleException is thrown. The default value is -1 and it means unlimited.
      Parameters:
      maxRenderedSize - The maximum allowed size of the rendered template.
      Returns:
      This builder object.
    • executorService

      public PebbleEngine.Builder executorService(ExecutorService executorService)
      Sets the executor service which is required if using one of Pebble's multithreading features such as the "parallel" tag.
      Parameters:
      executorService - The executor service
      Returns:
      This builder object
    • templateCache

      public PebbleEngine.Builder templateCache(PebbleCache<Object,PebbleTemplate> templateCache)
      Sets the cache used by the engine to store compiled PebbleTemplate instances.
      Parameters:
      templateCache - The template cache
      Returns:
      This builder object
    • tagCache

      public PebbleEngine.Builder tagCache(PebbleCache<CacheKey,Object> tagCache)
      Sets the cache used by the "cache" tag.
      Parameters:
      tagCache - The tag cache
      Returns:
      This builder object
    • autoEscaping

      public PebbleEngine.Builder autoEscaping(boolean autoEscaping)
      Sets whether or not escaping should be performed automatically.
      Parameters:
      autoEscaping - The auto escaping setting
      Returns:
      This builder object
    • allowOverrideCoreOperators

      public PebbleEngine.Builder allowOverrideCoreOperators(boolean allowOverrideCoreOperators)
      Sets whether or not core operators overrides should be allowed.
      Parameters:
      allowOverrideCoreOperators - Whether or not core operators overrides should be allowed.
      Returns:
      This builder object
    • defaultEscapingStrategy

      public PebbleEngine.Builder defaultEscapingStrategy(String strategy)
      Sets the default escaping strategy of the built-in escaper extension.
      Parameters:
      strategy - The name of the default escaping strategy
      Returns:
      This builder object
    • addEscapingStrategy

      public PebbleEngine.Builder addEscapingStrategy(String name, EscapingStrategy strategy)
      Adds an escaping strategy to the built-in escaper extension.
      Parameters:
      name - The name of the escaping strategy
      strategy - The strategy implementation
      Returns:
      This builder object
    • cacheActive

      public PebbleEngine.Builder cacheActive(boolean cacheActive)
      Enable/disable all caches, i.e. cache used by the engine to store compiled PebbleTemplate instances and tags cache
      Parameters:
      cacheActive - toggle to enable/disable all caches
      Returns:
      This builder object
    • methodAccessValidator

      public PebbleEngine.Builder methodAccessValidator(MethodAccessValidator methodAccessValidator)
      Validator that can be used to validate object/method access
      Parameters:
      methodAccessValidator - Validator that can be used to validate object/method access
      Returns:
      This builder object
    • literalDecimalTreatedAsInteger

      public PebbleEngine.Builder literalDecimalTreatedAsInteger(boolean literalDecimalTreatedAsInteger)
      Enable/disable treat literal decimal as Integer. Default is disabled, treated as Long.
      Parameters:
      literalDecimalTreatedAsInteger - toggle to enable/disable literal decimal treated as integer
      Returns:
      This builder object
    • literalNumbersAsBigDecimals

      public PebbleEngine.Builder literalNumbersAsBigDecimals(boolean literalNumbersAsBigDecimals)
      Enable/disable treat literal numbers as BigDecimals. Default is disabled, treated as Long/Double.
      Parameters:
      literalNumbersAsBigDecimals - toggle to enable/disable literal numbers treated as BigDecimals
      Returns:
      This builder object
    • greedyMatchMethod

      public PebbleEngine.Builder greedyMatchMethod(boolean greedyMatchMethod)
      Enable/disable greedy matching mode for finding java method. Default is disabled. If enabled, when can not find perfect method (method name, parameter length and parameter type are all satisfied), reduce the limit of the parameter type, try to find other method which has compatible parameter types. For example,
       {{ obj.number(2) }} 
      the expression can match all of below methods.
         public Long getNumber(Long v) {...}
         public Integer getNumber(Integer v) {...}
         public Short getNumber(Short v) {...}
         public Byte getNumber(Byte v) {...}
         ...
       
      Parameters:
      greedyMatchMethod - toggle to enable/disable greedy match method
      Returns:
      This builder object
      See Also:
    • registerExtensionCustomizer

      public PebbleEngine.Builder registerExtensionCustomizer(Function<Extension,ExtensionCustomizer> customizer)
      Registers an implementation of ExtensionCustomizer to change runtime-behaviour of standard functionality.
      Parameters:
      customizer - The customizer which wraps any non-user-provided extension
      Returns:
      This build object
    • build

      public PebbleEngine build()
      Creates the PebbleEngine instance.
      Returns:
      A PebbleEngine object that can be used to create PebbleTemplate objects.