From ae5cd024e07960002f4f9bbae23b6ccb3b73b3eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Thu, 16 Jun 2011 17:33:41 +0200 Subject: enable view and layout usage --- examples/sample.ru | 17 +++++++++++------ lib/zorglub/config.rb | 19 ++++++++++++------- lib/zorglub/node.rb | 12 +++++++----- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/examples/sample.ru b/examples/sample.ru index b318f86..009eaae 100644 --- a/examples/sample.ru +++ b/examples/sample.ru @@ -4,14 +4,19 @@ $LOAD_PATH << File.join(File.dirname( File.absolute_path(__FILE__)), '..', 'lib' # require 'zorglub' # -Zorglub::Config.register_engine 'my-engine', 'my-ext' -Zorglub::Config.register_engine 'temp-engine', 'tmp' +require 'haml' +HAML_PROC = Proc.new { |path,obj| Haml::Engine.new( File.open(path,'r').read ).render(obj) } +Zorglub::Config.register_engine 'haml', 'haml', HAML_PROC +Zorglub::Config.register_engine 'temp-engine', 'haml', HAML_PROC +# +Zorglub::Config.engine = 'haml' Zorglub::Config.root = File.dirname( File.absolute_path(__FILE__) ) # class Node1 < Zorglub::Node # def index a1, *a2 - "Node1:index

a1 : #{a1.inspect}

a2 : #{a2.inspect}

#{html}" + @title='Node2' + #"Node1:index

a1 : #{a1.inspect}

a2 : #{a2.inspect}

#{html}" end # def alt *args @@ -27,11 +32,11 @@ end class Node2 < Zorglub::Node # map APP, '/url2' - engine 'my-engine' - layout 'my-layout' + engine 'my-engine' # not available + layout 'my-layout' # not available # def index *args - "Node2#{html}" + "Node2:alt#{html}" end # def alt *args diff --git a/lib/zorglub/config.rb b/lib/zorglub/config.rb index 8b5ae37..5c43a1b 100644 --- a/lib/zorglub/config.rb +++ b/lib/zorglub/config.rb @@ -5,14 +5,12 @@ module Zorglub class Config @options = { :root => '.', - :engine => 'haml', + :engine => nil, :view_dir => 'view', :layout_dir => 'layout', :default_layout => 'default' } - @engines = { - 'haml' => 'haml' - } + @engines = { } class << self # def [] k @@ -39,14 +37,21 @@ module Zorglub end end # - def register_engine name, ext + def register_engine name, ext, proc return unless name and ext - @engines[name]=ext + @engines[name]=[ ext, proc ] end # def engine_ext engine - @engines[engine] + e = @engines[engine] + ( e.nil? ? '' : e[0] ) + end + # + def engine_proc engine + e = @engines[engine] + ( e.nil? ? nil : e[1] ) end + # end # def self.method_missing m, *args, &block diff --git a/lib/zorglub/node.rb b/lib/zorglub/node.rb index 99d6c5d..08214a3 100644 --- a/lib/zorglub/node.rb +++ b/lib/zorglub/node.rb @@ -55,11 +55,13 @@ module Zorglub end # def realize - # TODO - # - use view - # - use layout - r = self.send @action[:method], *@action[:args] - response.write r + @content = self.send @action[:method], *@action[:args] + e = Config.engine_proc @action[:engine] + v = view + l = layout + @content = e.call v, self if e and File.exists? v + @content = e.call l, self if e and File.exists? l + response.write @content response.finish end # -- cgit v1.1-2-g2b99