Sha256: bc5985f0d3e04b2593e83b57cf3b24406617f57eeb01eae8ee7eeaa1c4808b7d

Contents?: true

Size: 1.14 KB

Versions: 5

Compression:

Stored size: 1.14 KB

Contents

require_relative 'i18n_decorator'

module Locomotive
  module Steam
    module Decorators

      class TemplateDecorator < I18nDecorator

        def liquid_source
          if respond_to?(:template_path) && template_path
            source_from_template_file
          else
            self.source.blank? ? source_in_default_locale : self.source
          end
        end

        private

        def source_in_default_locale
          self.__with_default_locale__ { self.source }
        end

        def source_from_template_file
          source = File.read(template_path).force_encoding('utf-8')

          if match = source.match(FRONTMATTER_REGEXP)
            source = match[:template]
          end

          if template_path.ends_with?('.haml')
            render_haml(source, template_path)
          else
            source
          end
        end

        def render_haml(source, template_path)
          begin
            Haml::Engine.new(source).render
          rescue Haml::SyntaxError => e
            raise Steam::RenderError.new(e.message, template_path, source, e.line, e.backtrace)
          end
        end

      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
locomotivecms_steam-1.2.1 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.2.0 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.2.0.rc3 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.2.0.rc2 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.2.0.rc1 lib/locomotive/steam/decorators/template_decorator.rb