Sha256: 8ceb3730313617af932e52da20243e176c0fb3aa4934cf11cc9ab635f7bbceac

Contents?: true

Size: 1.15 KB

Versions: 13

Compression:

Stored size: 1.15 KB

Contents

require 'haml'
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

13 entries across 13 versions & 1 rubygems

Version Path
locomotivecms_steam-1.1.2 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.2.0.beta1 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.1.1 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.1.0 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.1.0.rc3 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.1.0.rc2 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.1.0.rc1 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.0.1 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.0.0 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.0.0.rc10 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.0.0.rc9 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.0.0.rc8 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.0.0.rc6 lib/locomotive/steam/decorators/template_decorator.rb