# main class for erubis template rendering require 'erubis' class ErubisTemplate < BaseDrop include UrlFilters include DropFilters include CoreFilters def initialize(site) @site_source = site end def liquify(*records, &block) self.class.liquify(@context, *records, &block) end def render(section, layout, template, assigns ={}, controller = nil) @layout = layout @template = template @controller = controller @assigns = assigns # psq-TODO: assigns contains mode, site, articles, section (more or less depending on mode, # consider using missing method to expose all of them to templates) # "section" would be nicer than # "@assigns['section']" @context = ::Liquid::Context.new(assigns, {}, false) # assigns, register, rethrow error @mode = assigns['mode'] @archive_date = assigns['archive_date'] @articles = assigns['articles'] @articles.each { |article| article.context = @context } if (@articles) @article = assigns['article'] @article.context = @context if @article # psq-TODO can't see how this has a value @submitted = @context['submitted'] || {} @submitted.each{ |k, v| @submitted[k] = CGI::escapeHTML(v) } @site = @site_source.to_liquid @site.context = @context if (section) @section = section.to_liquid @section.context = @context end to_html end # entry point for rendering layout and main_template to html # not reentrant at this point because of the use of @ouput and @binding def to_html @binding = get_binding #use the same binding throughout do_include(@layout) end # # in layout, include content using chosen template # <% main_content %> # def main_content do_include(@template) end # include template # <% include "template name" %> def include(template) do_include(@site_source.find_preferred_template(:page, template+".erubis")) end protected def do_include(erubis_template) # need to save/restore _buf (somewhat internal to how Eruby generates the template) since everything is using the same binding. # psq-TODO: if page caching is not working well enough, keeping a compiled version of the template could help # see ActionView::CompiledTemplates begin eval "_context.push(_buf)", @binding result = Erubis::Eruby.new(erubis_template.read.to_s).result @binding eval "_buf = _context.pop", @binding result rescue raise "Erubis Error: #{$!}" end end def get_binding _buf = '' _context = [] binding end end