Sha256: 01c7e4f5feaf1fd06a9b94a3e8f5af2e3acbb42fb88042ef097f68a80b394509

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 KB

Contents

require 'tilt'

module Hanami
  module View
    # A logic-less template.
    #
    # @since 0.1.0
    class Template
      def initialize(template, encoding = Encoding::UTF_8)
        options = { default_encoding: encoding }

        if slim?(template)
          # NOTE disable_escape: true is for Slim compatibility
          # See https://github.com/hanami/assets/issues/36
          options[:disable_escape] = true
        end

        @_template = Tilt.new(template, nil, options)
      end

      # Returns the format that the template handles.
      #
      # @return [Symbol] the format name
      #
      # @since 0.1.0
      #
      # @example
      #   require 'hanami/view'
      #
      #   template = Hanami::View::Template.new('index.html.erb')
      #   template.format # => :html
      def format
        @_template.basename.match(/\.([^.]+)/).captures.first.to_sym
      end

      # Render the template within the context of the given scope.
      #
      # @param scope [Hanami::View::Scope] the rendering scope
      #
      # @return [String] the output of the rendering process
      #
      # @api private
      # @since 0.1.0
      #
      # @see Hanami::View::Scope
      def render(scope, &blk)
        @_template.render(scope, {}, &blk)
      end

      private

      def slim?(template)
        File.extname(template) == ".slim".freeze
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
hanami-view-1.3.3 lib/hanami/view/template.rb
hanami-view-1.3.2 lib/hanami/view/template.rb
hanami-view-1.3.1 lib/hanami/view/template.rb
hanami-view-1.3.0 lib/hanami/view/template.rb
hanami-view-1.2.1 lib/hanami/view/template.rb
hanami-view-1.3.0.beta1 lib/hanami/view/template.rb