Sha256: dbbdd889d69d6eca69d591a78f708ef89f736857c6f928613aa51ec1e3728f38

Contents?: true

Size: 1.13 KB

Versions: 21

Compression:

Stored size: 1.13 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, escape_attrs: false).render
          rescue Haml::SyntaxError => e
            raise Steam::RenderError.new(e, template_path, source)
          end
        end

      end

    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
locomotivecms_steam-1.7.1 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.7.0 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.6.1 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.6.0 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.6.0.rc1 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.6.0.beta1 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.5.3 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.5.2 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.5.1 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.5.0 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.5.0.rc1 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.5.0.rc0 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.5.0.beta3 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.5.0.beta2 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.5.0.beta1 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.4.1 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.4.0 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.4.0.rc2 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.4.0.rc1 lib/locomotive/steam/decorators/template_decorator.rb
locomotivecms_steam-1.4.0.pre.rc.1 lib/locomotive/steam/decorators/template_decorator.rb