Module: Renee::Render

Defined in:
lib/renee_render.rb,
lib/renee_render/version.rb

Overview

This module is responsible for handling the rendering of templates using Tilt supporting all included template engines.

Defined Under Namespace

Modules: ClassMethods Classes: TemplateNotFound

Constant Summary collapse

VERSION =

The current version of Renee::Render

Renee::Core::VERSION

Instance Method Summary collapse

Instance Method Details

#inline(data, engine, options = nil, &block) ⇒ String

Renders a string given the engine and the content.

it will be detected based on the extension of the file.

Examples:

inline "%p test", :haml # => "<p>test</p>"

Parameters:

  • data (String)

    The string data to render.

  • engine (Symbol)

    The name of the engine to use to render the content. If this isn't specified

  • options (Hash) (defaults to: nil)

    The options to pass in for rendering.

Returns:

  • (String)

    The result of rendering the data with specified engine.



114
115
116
117
118
119
120
121
122
123
# File 'lib/renee_render.rb', line 114

def inline(data, engine, options = nil, &block)
  options, engine = engine, nil if engine.is_a?(Hash)
  call_data = options && options.delete(:_caller) || Callsite.parse(caller.first)
  render_setup(engine, options, block) do |view_options, views|
    body = data.is_a?(Proc) ? data : Proc.new { data }
    template = Tilt[engine]
    raise "Template engine not found: #{engine}" if template.nil?
    template.new(call_data.filename, call_data.line, view_options, &body)
  end
end

#inline!(data, engine, options = {}, &blk) ⇒ String

Same as inline but automatically halts.

Parameters:

  • data (String)

    The string data to render.

  • engine (Symbol)

    The name of the engine to use to render the content. If this isn't specified

  • options (Hash) (defaults to: {})

    The options to pass in for rendering.

Returns:

  • (String)

    The result of rendering the data with specified engine.

See Also:



64
65
66
67
# File 'lib/renee_render.rb', line 64

def inline!(data, engine, options = {}, &blk)
  options[:_caller] = Callsite.parse(caller.first)
  halt inline(data, engine, options, &blk)
end

#partial(template, options = {}) ⇒ String

Render a partials with collections support

Examples:

partial 'photo/item', :object => @photo
partial 'photo/item', :collection => @photos
partial 'photo/item', :locals => { :foo => :bar }

Returns:

  • (String)

    The html generated from this partial.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/renee_render.rb', line 135

def partial(template, options={})
  options = { :locals => {}, :layout => false }.merge(options)
  path            = template.to_s.split(File::SEPARATOR)
  object_name     = path[-1].to_sym
  path[-1]        = "_#{path[-1]}"
  template_path   = File.join(path).to_sym
  raise 'Partial collection specified but is nil' if options.has_key?(:collection) && options[:collection].nil?
  if collection = options.delete(:collection)
    options.delete(:object)
    counter = 0
    collection.map { |member|
      counter += 1
      options[:locals].merge!(object_name => member, "#{object_name}_counter".to_sym => counter)
      render(template_path, options.dup)
    }.join("\n")
  else
    if member = options.delete(:object)
      options[:locals].merge!(object_name => member)
    end
    render(template_path, options.dup)
  end
end

#render(file, engine = nil, options = nil, &block) ⇒ String

Renders a file given the engine and the content.

it will be detected based on the extension of the file.

Examples:

render "index", :haml   # => "<p>test</p>"
render "index"          # => "<p>test</p>"

Parameters:

  • file (String)

    The path to the file to render

  • engine (Symbol) (defaults to: nil)

    The name of the engine to use to render the content. If this isn't specified

  • options (Hash) (defaults to: nil)

    The options to pass in for rendering.

Returns:

  • (String)

    The result of rendering the data with specified engine.



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/renee_render.rb', line 85

def render(file, engine = nil, options = nil, &block)
  options, engine = engine, nil if engine.is_a?(Hash)
  render_setup(engine, options, block) do |view_options, views|
    template_cache.fetch(engine, file, view_options) do
      file_path, found_engine = find_template(views, file, engine)
      template = Tilt[found_engine]
      raise TemplateNotFound, "Template engine not found: #{found_engine.inspect}" unless template
      raise TemplateNotFound, "Template #{file.inspect} (with engine #{engine.inspect}) not found in #{views.inspect}!" unless file_path
      # TODO suppress errors for layouts?
      template.new(file_path, 1, view_options)
    end
  end
end

#render!(file, engine = nil, options = nil, &blk) ⇒ String

Same as render but automatically halts.

Parameters:

  • file (String)

    The path to the file to render

  • engine (Symbol) (defaults to: nil)

    The name of the engine to use to render the content. If this isn't specified

  • options (Hash) (defaults to: nil)

    The options to pass in for rendering.

Returns:

  • (String)

    The result of rendering the data with specified engine.

See Also:



56
57
58
# File 'lib/renee_render.rb', line 56

def render!(file, engine = nil, options = nil, &blk)
  halt render(file, engine, options, &blk)
end