class Rad::Template::Context include Tilt::CompileSite, Tilt::ContextExt # # System # attr_accessor :_tilt_template def initialize instance_variables = nil set_instance_variables! instance_variables if instance_variables end # for saving :format, :current_dir and others attr_accessor :scope_variables # # Helpers # inject( workspace: :workspace, router: :router, template: :template, logger: :logger ) delegate :render, to: :template delegate :url_for, to: :router def j obj obj.to_s.json_escape end def blank? obj obj.is_a?(String) ? !!(obj =~ /\A\s*\z/) : obj.blank? end def present? obj !blank?(obj) end # # Content Variables # attr_accessor :content_block def content_variables; @content_variables ||= {}.to_openobject end def content_for name, content = nil, &block content ||= capture(&block) (content_variables[name.to_s] ||= "") << content nil end def prepend_to name, content = nil, &block content ||= capture(&block) (content_variables[name.to_s] ||= "").insert 0, content nil end def wrap_content_for name, &block block.must_be.defined content = capture((content_variables[name.to_s] || ""), &block) content_variables[name.to_s] ||= content nil end def has_content_for? name content_variables.include? name.to_s end def find_relative_template *args self.class.find_relative_template *args end protected def set_instance_variables! instance_variables instance_variables = [instance_variables] unless instance_variables.is_a? Array instance_variables.each do |container| if container.is_a? Hash container.each do |name, value| instance_variable_set("@#{name}", value) end else container.instance_variables.each do |ivname| iv = container.instance_variable_get(ivname) instance_variable_set(ivname, iv) end end end end end