@@ -16,7 +16,10 @@ class Engine
1616 #
1717 # @param lazy [true, false] immediately loads and initializes the JavaScript
1818 # environment.
19- def initialize ( lazy : false )
19+ # @param path [String, nil] the path to the version of Handlebars to load.
20+ # If `nil`, the contents of `Handlebars::Source.bundled_path` is loaded.
21+ def initialize ( lazy : false , path : nil )
22+ @path = path
2023 init! unless lazy
2124 end
2225
@@ -60,14 +63,29 @@ def template(*args)
6063
6164 # Registers helpers accessible by any template in the environment.
6265 #
66+ # The function can be either a proc or a string:
67+ # * When the function is a proc, it can be either passed in as a normal
68+ # parameter or as a block.
69+ # * When the function is a string, it is interpreted as a JavaScript
70+ # function.
71+ #
6372 # @param name [String, Symbol] the name of the helper
73+ # @param function [Proc, String] the helper function
6474 # @yieldparam context [Hash] the current context
6575 # @yieldparam arguments [Object] the arguments (optional)
6676 # @yieldparam options [Hash] the options hash (optional)
6777 # @see https://handlebarsjs.com/api-reference/runtime.html#handlebars-registerhelper-name-helper
68- def register_helper ( name , &block )
69- attach ( name , &block )
70- call ( :registerHelper , [ name . to_s , name . to_sym ] , eval : true )
78+ def register_helper ( name = nil , function = nil , **helpers , &block )
79+ helpers [ name ] = block || function if name
80+ helpers . each do |n , f |
81+ case f
82+ when Proc
83+ attach ( n , &f )
84+ evaluate ( "registerHelper('#{ n } ', #{ n } )" )
85+ when String , Symbol
86+ evaluate ( "Handlebars.registerHelper('#{ n } ', #{ f } )" )
87+ end
88+ end
7189 end
7290
7391 # Unregisters a previously registered helper.
@@ -204,21 +222,14 @@ def init!
204222 return if @init
205223
206224 @context = MiniRacer ::Context . new
207- @context . load ( ::Handlebars ::Source . bundled_path )
225+ @context . load ( @path || ::Handlebars ::Source . bundled_path )
208226 @context . load ( File . absolute_path ( "engine/init.js" , __dir__ ) )
209227
210228 @init = true
211229 end
212230
213231 def js_args ( args )
214- args . map { |arg |
215- case arg
216- when Symbol
217- arg
218- else
219- JSON . generate ( arg )
220- end
221- }
232+ args . map { |arg | JSON . generate ( arg ) }
222233 end
223234 end
224235end
0 commit comments