Skip to content

Commit

Permalink
embedded_partial and self_embedded_partial
Browse files Browse the repository at this point in the history
There kinds of partials extract HTML to be processed from original
template element:

embedded_partial allows to render any element of document with specified
Lilu partial instruction (_name_of_partial.lilu):

update('#header-from').with embedded_partial('#header-from',"header_from",:locals => { :name => @name})

self_embedded_partial renders selected element:

update('#header-from').with self_embedded_partial("header_from",:locals => { :name => @name})

it also works with multiple elements:

update(:all,'.header-from').with self_embedded_partial("header_from",:locals => { :name => @name})
  • Loading branch information
Yurii Rashkovskii committed Jan 8, 2008
1 parent 85c23d1 commit e7b5f76
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
4 changes: 3 additions & 1 deletion lib/actions/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def with(arg=nil,&block)
when Hash
arg.each_pair do |path,value|
break if value.nil?
value = value.to_proc(element) if value.is_a?(ElementRelative)
value = value.to_proc(element) if value.kind_of?(ElementRelative)
value = value.call if value.is_a?(Proc)
case path
when OptionalElementAt
Expand Down Expand Up @@ -44,6 +44,8 @@ def with(arg=nil,&block)
end
when Proc
with arg.call
when ElementRelative
with arg.to_proc(element).call
when nil
if block_given?
with renderer.scope.instance_eval(&block)
Expand Down
28 changes: 26 additions & 2 deletions lib/lilu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ def initialize(doc,element)
class ElementRelative
end

class SelfEmbeddedPartial < ElementRelative
def initialize(renderer,name,opts={})
@renderer, @name, @opts = renderer, name, opts
end
def to_proc(element)
lambda do
additional_opts = @opts.clone
additional_opts.merge!({ :partial => @name, :locals => { :___embedded_html___ => element.inner_html }.merge(@opts[:locals]||{}) })
@renderer.view.instance_eval { render(additional_opts) }
end
end
end

class ElementAt < ElementRelative
attr_reader :path

Expand Down Expand Up @@ -165,6 +178,18 @@ def partial(name,opts={})
renderer.view.instance_eval { render({:partial => name}.merge(opts)) }
# renderer.controller.instance_eval { render({:partial => name}.merge(opts)) }
end

# Helper for embedded partials
def embedded_partial(path,name,opts={})
additional_opts = opts
additional_opts.merge!({ :partial => name, :locals => { :___embedded_html___ => element_at(path).inner_html }.merge(opts[:locals]||{}) })
renderer.view.instance_eval { render(additional_opts) }
end

def self_embedded_partial(name,opts={})
SelfEmbeddedPartial.new(renderer,name,opts)
end

# Helper for element_at
def element_at(path) ; @renderer.doc.at(path) ; end

Expand All @@ -174,8 +199,7 @@ def replacing(element) ; Replacing.new(renderer.doc,element) ; end
# Helper for ElementAt
def at(element) ; ElementAt.new(element) ; end

# Helper for OptionalElementAt

# Helper for OptionalElementAt
def optionally_at(element) ; OptionalElementAt.new(element) ; end


Expand Down
23 changes: 14 additions & 9 deletions lib/lilu_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,28 @@ def render(template, local_assigns = {})
@view.instance_eval do
local_assigns.merge!("content_for_layout" => @content_for_layout,"___view" => self)
end

template_extname = File.extname(file_path)
begin
html_file_path = file_path.gsub(template_extname,'.html')
html_template = IO.read(html_file_path)
rescue
erb_template = IO.read(file_path.gsub(template_extname,'.erb.html')) rescue IO.read(file_path.gsub(template_extname,'.erb')) rescue IO.read(file_path.gsub(template_extname,'.rhtml'))
html_template = @view.render(:type => :erb, :inline => erb_template, :locals => local_assigns)
if local_assigns[:___embedded_html___]
html_template = local_assigns[:___embedded_html___]
html_file_path = ""
else
begin
html_file_path = file_path.gsub(template_extname,'.html')
html_template = IO.read(html_file_path)
rescue
erb_template = IO.read(file_path.gsub(template_extname,'.erb.html')) rescue IO.read(file_path.gsub(template_extname,'.erb')) rescue IO.read(file_path.gsub(template_extname,'.rhtml'))
html_template = @view.render(:type => :erb, :inline => erb_template, :locals => local_assigns)
end
end

#let the error from the erb.html file bubble up
html_template = @view.render(:type => :erb, :inline => html_template, :locals => local_assigns) if html_file_path.include?(".erb.")

if html_file_path.include?(".erb.")
lilu_file_path = file_path.gsub(".erb"+template_extname,'.lilu')
lilu_file_path = file_path.gsub(".erb"+template_extname,'.lilu')
else
lilu_file_path = file_path.gsub(template_extname,'.lilu')
lilu_file_path = file_path.gsub(template_extname,'.lilu')
end
lilu_template = IO.read(lilu_file_path) rescue lilu_template = ''
@view.instance_eval do
Expand Down

0 comments on commit e7b5f76

Please sign in to comment.