Class: Lux::Template::Helper
Instance Attribute Summary collapse
-
#_source_object ⇒ Object
readonly
Returns the value of attribute _source_object.
Instance Method Summary collapse
- #cache(name = nil, opts = {}, &block) ⇒ Object
- #capture_proc ⇒ Object
-
#content(key) ⇒ Object
content :foo do …
- #flash ⇒ Object
-
#helper(*names) ⇒ Object
helper(:main).method.
-
#initialize(instance, *list) ⇒ Helper
constructor
create helper object that cah be used in template render.
- #no_white_space ⇒ Object
- #once(id = nil) ⇒ Object
- #render(name = nil, locals = {}) ⇒ Object
Constructor Details
#initialize(instance, *list) ⇒ Helper
create helper object that cah be used in template render
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/lux/template/helper.rb', line 9 def initialize instance, *list extend ApplicationHelper @_source_object = instance list.flatten.compact.each do |el| el = el.to_s.classify+'Helper' extend el.constantize end local_vars = instance.class == Hash ? instance : instance.instance_variables_hash # locals overide globals for k, v in local_vars instance_variable_set("@#{k.to_s.sub('@','')}", v) end # helper.instance_exec &block if block end |
Instance Attribute Details
#_source_object ⇒ Object (readonly)
Returns the value of attribute _source_object.
6 7 8 |
# File 'lib/lux/template/helper.rb', line 6 def _source_object @_source_object end |
Instance Method Details
#cache(name = nil, opts = {}, &block) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/lux/template/helper.rb', line 98 def cache name = nil, opts = {}, &block if opts.class == Integer opts = { ttl: opts } elsif name.is_a?(Hash) opts = name name = '' else name = Lux.cache.generate_key(name) end opts[:ttl] ||= 1.hour key = 'view:'+name+block.source_location.join(':')+Lux.config..to_s Lux.cache.fetch(key, opts) { yield } end |
#capture_proc ⇒ Object
60 61 62 |
# File 'lib/lux/template/helper.rb', line 60 def capture_proc proc { |*args| "#{yield(*args)}" } end |
#content(key) ⇒ Object
content :foo do … # define
content :foo? ? true : false # ceheck existance
content :foo # get content
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/lux/template/helper.rb', line 42 def content key name = 'haml_content_%s' % key if name.end_with?('?') haz = !!Lux.current.var[name.sub(/\?$/, '')] if block_given? haz ? "#{yield}" : '' else haz end elsif block_given? Lux.current.var[name] = "#{yield}" nil else Lux.current.var[name] end end |
#flash ⇒ Object
124 125 126 |
# File 'lib/lux/template/helper.rb', line 124 def flash Lux.current.response.flash end |
#helper(*names) ⇒ Object
helper(:main).method
114 115 116 |
# File 'lib/lux/template/helper.rb', line 114 def helper *names Lux::Template::Helper.new(self, *names) end |
#no_white_space ⇒ Object
35 36 37 |
# File 'lib/lux/template/helper.rb', line 35 def no_white_space yield.gsub(/>\s+</,'><') end |
#once(id = nil) ⇒ Object
118 119 120 121 122 |
# File 'lib/lux/template/helper.rb', line 118 def once id = nil Lux.current.once("template-#{id || caller[0]}") do block_given? ? yield : true end end |
#render(name = nil, locals = {}) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/lux/template/helper.rb', line 67 def render name = nil, locals = {} if !name return InlineRenderProxy.new(self) elsif name.is_array? return name.map { |b| render(b) }.join("\n") elsif name.respond_to?(:db_schema) raise 'not supported' path = Lux.current.var.root_template_path.split('/')[1] table_name = name.class.name.tableize locals[table_name.singularize.to_sym] = name eval "@_#{table_name.singularize} = name" name = "#{path}/#{table_name}/_#{table_name.singularize}" elsif !name.to_s.start_with?('./') template_path = Lux.current.var.root_template_path || './app/views' name = Pathname.new(template_path).join(name.to_s).to_s name = './app/views' + name if name.starts_with?('/') end for k, v in locals instance_variable_set("@_#{k}", v) end if block_given? name = "#{name}/layout" unless name.index('/') Lux::Template.render(self, name) { yield() } else Lux::Template.render(self, name) end end |