class Flame::Render

Helper for render functionality

Public Class Methods

new(controller, path, options = {}) click to toggle source
# File lib/flame/render.rb, line 16
def initialize(controller, path, options = {})
        ## Take options for rendering
        @controller = controller
        @scope = options.delete(:scope) || @controller
        @layout = options.delete(:layout)
        @layout = 'layout.*' if @layout.nil?
        ## And get the rest variables to locals
        @locals = options.merge(options.delete(:locals) || {})
        ## Find filename
        @filename = find_file(path)
        unless @filename
                raise Flame::Errors::TemplateNotFoundError.new(controller, path)
        end
        @layout = nil if File.basename(@filename)[0] == '_'
end

Public Instance Methods

render(cache: true) click to toggle source

Render template @param cache [Boolean] cache compiles or not

# File lib/flame/render.rb, line 34
def render(cache: true)
        @cache = cache
        ## Compile Tilt to instance hash
        return unless @filename
        tilt = compile_file
        ## Render Tilt from instance hash with new options
        layout_render tilt.render(@scope, @locals)
end

Private Instance Methods

clean_paths(paths) click to toggle source
# File lib/flame/render.rb, line 73
def clean_paths(paths)
        paths.map! { |path| Pathname.new(path).cleanpath.to_s }.uniq
end
combine_parts(parts) click to toggle source

Make combinations in order with different sizes @example Make parts for ['project', 'namespace', 'controller']

# => [
       ['project', 'namespace', 'controller'],
       ['project', 'namespace'],
       ['namespace', 'controller'],
       ['namespace']
     ]
# File lib/flame/render.rb, line 111
def combine_parts(parts)
        parts.size.downto(1).with_object([]) do |i, arr|
                arr.push(*parts.combination(i).to_a)
        end
        # variants.uniq!.reject!(&:empty?)
end
compile_file(filename = @filename) click to toggle source

Compile file with Tilt engine @param filename [String] filename

# File lib/flame/render.rb, line 51
def compile_file(filename = @filename)
        cached = @controller.cached_tilts[filename]
        return cached if @cache && cached
        compiled = Tilt.new(filename)
        @controller.cached_tilts[filename] ||= compiled if @cache
        compiled
end
controller_dirs() click to toggle source

Find possible directories for the controller

# File lib/flame/render.rb, line 92
def controller_dirs
        parts = @controller.class.underscore.split('/').map do |part|
                %w[_controller _ctrl]
                        .find { |suffix| part.chomp! suffix }
                part
                ## Alternative, but slower by ~50%:
                # part.sub(/_(controller|ctrl)$/, '')
        end
        combine_parts(parts).map! { |path| path.join('/') }
end
find_file(path) click to toggle source

Find template-file by path

# File lib/flame/render.rb, line 78
def find_file(path)
        find_files(path, controller_dirs).find { |file| Tilt[file] }
end
find_files(path, dirs) click to toggle source

Common method for `find_file` and `find_layouts`

# File lib/flame/render.rb, line 63
def find_files(path, dirs)
        paths = [path]
        paths.push(dirs.last) if path.to_sym == :index
        dirs.push(nil)
        files = Dir[
                File.join(views_dir, "{#{dirs.join(',')}}", "{#{paths.join(',')}}.*")
        ]
        clean_paths files
end
find_layouts(path) click to toggle source

Find layout-files by path

# File lib/flame/render.rb, line 83
def find_layouts(path)
        find_files(path, layout_dirs)
                .select { |file| Tilt[file] }
                .sort! { |a, b| b.split('/').size <=> a.split('/').size }
end
layout_dirs() click to toggle source
# File lib/flame/render.rb, line 118
def layout_dirs
        file_dir = Pathname.new(@filename).dirname
        diff_path = file_dir.relative_path_from Pathname.new(views_dir)
        diff_parts = diff_path.to_s.split('/')
        diff_parts.map.with_index do |_part, ind|
                diff_parts[0..-(ind + 1)].join('/')
        end
end
layout_render(content) click to toggle source

Render the layout with template @param result [String] result of template rendering

# File lib/flame/render.rb, line 129
def layout_render(content)
        return content unless @layout
        layout_files = find_layouts(@layout)
        return content if layout_files.empty?
        layout_files.each_with_object(content.dup) do |layout_file, result|
                layout = compile_file(layout_file)
                result.replace layout.render(@scope, @locals) { result }
        end
end
views_dir() click to toggle source
# File lib/flame/render.rb, line 45
def views_dir
        @controller.config[:views_dir]
end