lib/tenjin.rb in tenjin-0.6.0 vs lib/tenjin.rb in tenjin-0.6.1

- old
+ new

@@ -1,7 +1,7 @@ ## -## copyright(c) 2007 kuwata-lab all rights reserved. +## copyright(c) 2007-2008 kuwata-lab.com all rights reserved. ## ## Permission is hereby granted, free of charge, to any person obtaining ## a copy of this software and associated documentation files (the ## "Software"), to deal in the Software without restriction, including ## without limitation the rights to use, copy, modify, merge, publish, @@ -22,17 +22,17 @@ ## ## ## Tenjin module ## -## $Rev: 59 $ -## $Release: 0.6.0 $ +## $Rev: 65 $ +## $Release: 0.6.1 $ ## module Tenjin - RELEASE = ('$Release: 0.6.0 $' =~ /[\d.]+/) && $& + RELEASE = ('$Release: 0.6.1 $' =~ /[\d.]+/) && $& ## ## helper module for Context class ## @@ -102,10 +102,15 @@ ## module ContextHelper attr_accessor :_buf, :_engine, :_layout + ## escape value. this method should be overrided in subclass. + def escape(val) + return val + end + ## include template. 'template_name' can be filename or short name. def import(template_name, _append_to_buf=true) _buf = self._buf output = self._engine.render(template_name, context=self, layout=false) _buf << output if _append_to_buf @@ -238,29 +243,34 @@ def []=(key, val) instance_variable_set("@#{key}", val) end - def escape(val) - return val - end - def update(hash) hash.each do |key, val| self[key] = val end end def key?(key) return self.instance_variables.include?("@#{key}") end + if Object.respond_to?('instance_variable_defined?') + def key?(key) + return self.instance_variable_defined?("@#{key}") + end + end + alias has_key? key? + def each() instance_variables().each do |name| - val = instance_variable_get(name) - key = name[1..-1] - yield([key, val]) if name != '@_buf' && name != '@_engine' + if name != '@_buf' && name != '@_engine' + val = instance_variable_get(name) + key = name[1..-1] + yield([key, val]) + end end end end @@ -607,14 +617,14 @@ return "''" end ## evaluate converted ruby code and return it. ## argument '_context' should be a Hash object or Context object. - def render(context=Context.new) - context = Context.new(context) if context.is_a?(Hash) + def render(_context=Context.new) + _context = Context.new(_context) if _context.is_a?(Hash) @proc ||= _render() - return context.instance_eval(&@proc) + return _context.instance_eval(&@proc) end end @@ -927,18 +937,18 @@ while true template = get_template(template_name, context) # context is passed only for preprocessor _buf = context._buf output = template.render(context) context._buf = _buf - unless context['_layout'].nil? - layout = context['_layout'] - context['_layout'] = nil + unless context._layout.nil? + layout = context._layout + context._layout = nil end layout = @layout if layout == true or layout.nil? break unless layout template_name = layout layout = false - context['_content'] = output + context.instance_variable_set('@_content', output) end return output end def hook_context(context)