Sha256: e3ca985601170d05d266c2c50c6a05c5e56c3b4cc186033b144a4430fe595b6b

Contents?: true

Size: 1.67 KB

Versions: 8

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module Handlebarsjs
  # API for interacting with Handlebars.js while providing native Ruby helpers
  class Handlebars < Handlebarsjs::Javascript
    def initialize
      super

      add_libraries
      add_configured_helpers
    end

    class << self
      def register_helper_script(name)
        <<-JAVASCRIPT
          Handlebars.registerHelper('#{name}', ruby_#{name})
        JAVASCRIPT
      end

      def register_safe_string_helper_script(name, parameter_names)
        parameters = (parameter_names + ['_opts']).join(', ')

        <<-JAVASCRIPT
          Handlebars.registerHelper('#{name}', function (#{parameters}) {
            return new Handlebars.SafeString(ruby_#{name}(#{parameters}));
          })
        JAVASCRIPT
      end
    end

    def process_template(template, options = {})
      # TODO: process template function may be improved with some type of caching
      context.call('process_template', template, options)
    end

    private

    def add_libraries
      # Handlebars 4.7.7
      gem_path = Gem.loaded_specs['handlebarsjs'].full_gem_path

      handlebars_lib_path = File.join(gem_path, Handlebarsjs::HANDLEBARS_LIBRARY_PATH)
      handlebars_api_path = File.join(gem_path, Handlebarsjs::HANDLEBARS_API_PATH)

      handlebars_snapshot.add_library('handlebars', path: handlebars_lib_path)

      # Support functions for working with
      handlebars_snapshot.add_library('handlebars-api', path: handlebars_api_path)
    end

    def add_configured_helpers
      KConfig.configuration.handlebars.helpers.each do |helper_config|
        handlebars_snapshot.add_helper(helper_config.name, helper_config.helper)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
handlebarsjs-0.12.0 lib/handlebarsjs/handlebars.rb
handlebarsjs-0.11.3 lib/handlebarsjs/handlebars.rb
handlebarsjs-0.11.2 lib/handlebarsjs/handlebars.rb
handlebarsjs-0.11.1 lib/handlebarsjs/handlebars.rb
handlebarsjs-0.11.0 lib/handlebarsjs/handlebars.rb
handlebarsjs-0.10.0 lib/handlebarsjs/handlebars.rb
handlebarsjs-0.9.0 lib/handlebarsjs/handlebars.rb
handlebarsjs-0.8.0 lib/handlebarsjs/handlebars.rb