Sha256: 43d3c73a9ba7e6f5f2ff1254cb576726e880976ce76c2be37bdd639c4f8dd4ea

Contents?: true

Size: 1.86 KB

Versions: 3

Compression:

Stored size: 1.86 KB

Contents

module Cutaneous
  # Manages a set of Loaders that render templates
  class Engine
    attr_reader   :roots
    attr_accessor :loader_class, :default_format

    def initialize(template_roots, syntax = Cutaneous::FirstPassSyntax, default_format = "html")
      @roots          = Array(template_roots)
      @syntax    = syntax
      @loader_class   = FileLoader
      @default_format = default_format
    end

    def render_file(path, context, format = default_format)
      file_loader(format).render(path, context)
    end

    alias_method :render, :render_file

    def render_string(template_string, context, format = default_format)
      string_loader(format).render(template_string, context)
    end

    # Create and cache a file loader on a per-format basis
    def file_loader(format)
      file_loader_instance(format.to_s).tap do |loader|
        loader.syntax = @syntax
      end
    end

    # Not worth caching string templates as they are most likely to be one-off
    # instances & not repeated in the lifetime of the engine.
    def string_loader(format)
      StringLoader.new(file_loader(format))
    end

    def template_exists?(root, relative_path, format)
      file_loader(format).exists?(root, relative_path)
    end

    protected

    def file_loader_instance(format)
      loader_class.new(@roots, format)
    end
  end

  # A caching version of the default Engine implementation
  class CachingEngine < Engine
    attr_writer :write_compiled_scripts

    def initialize(template_roots, syntax, default_format = "html")
      super
      @loader_class = CachedFileLoader
      @loaders      = {}
      @write_compiled_scripts = true
    end

    def file_loader_instance(format)
      @loaders[format] ||= super.tap do |loader|
        loader.write_compiled_scripts = @write_compiled_scripts if loader.respond_to?(:write_compiled_scripts=)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cutaneous-0.1.4 lib/cutaneous/engine.rb
cutaneous-0.1.3 lib/cutaneous/engine.rb
cutaneous-0.1.2 lib/cutaneous/engine.rb