lib/flutterby/view.rb in flutterby-0.5.2 vs lib/flutterby/view.rb in flutterby-0.6.0
- old
+ new
@@ -1,64 +1,42 @@
require 'benchmark'
require 'flutterby/layout'
module Flutterby
class View
- attr_reader :node, :opts
+ attr_reader :node, :locals
alias_method :page, :node
# Include ERB::Util from ActiveSupport. This will provide
# html_escape, h, and json_escape helpers.
#
# http://api.rubyonrails.org/classes/ERB/Util.html
#
include ERB::Util
- def initialize(node, opts = {})
+ def initialize(node, locals: {})
@node = node
- @opts = opts
+ @locals = locals
end
- def render!
- output = node.source.try(:html_safe)
-
- time = Benchmark.realtime do
- output = Filters.apply!(output, view: self)
-
- # Apply layouts
- if opts[:layout] && node.page?
- output = Layout.apply!(output, view: self)
- end
- end
-
- # Log rendering times using different colors based on duration
- color = if time > 1
- :red
- elsif time > 0.25
- :yellow
- else
- :green
- end
-
- logger.debug "Rendered #{node.url.colorize(:blue)} in #{sprintf("%.1fms", time * 1000).colorize(color)}"
-
- output
- end
-
def date_format(date, fmt)
date.strftime(fmt)
end
def raw(str)
str.html_safe
end
- def render(expr, *args)
- if expr.is_a?(Node)
- expr.render(*args)
- else
- find(expr).render(*args)
+ def render(expr, as: nil, locals: {}, **args)
+ node = expr.is_a?(Node) ? expr : find(expr)
+
+ # Resolve rendering "as" specific things
+ if as
+ locals[as] = node
+ node = node.find!("./_#{as}.#{self.node.ext}")
end
+
+ node.render(**args, locals: locals.with_indifferent_access, **args)
end
def find(*args)
node.find(*args)
end