Class: Lux::View::Helper

Inherits:
Object show all
Defined in:
lib/lux/view/helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(instance, *list) ⇒ Helper

create helper object that cah be used in template render



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/lux/view/helper.rb', line 8

def initialize instance, *list
  extend ApplicationHelper

  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 Method Details

#cache(*args, &block) ⇒ Object



90
91
92
93
94
# File 'lib/lux/view/helper.rb', line 90

def cache *args, &block
  ttl = args.last.class == Hash ? args.pop[:ttl] : nil
  key  = 'view:'+Lux.cache.generate_key(args)+block.source_location.join(':')
  Lux.cache.fetch(key, ttl) { yield }
end

#content(name = nil) ⇒ Object

  • @foo = content do …

@foo

  • content :foo do …

content :foo



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lux/view/helper.rb', line 40

def content name=nil
  ivar = '@content_%s' % name

  if block_given?
    yield.tap do |data|
      instance_variable_set(ivar, data) if name
    end
  else
    name ? instance_variable_get(ivar) : nil
  end
end

#error(msg) ⇒ Object



96
97
98
# File 'lib/lux/view/helper.rb', line 96

def error msg
  %[<pre style="color:red; background:#eee; padding:10px; font-family:'Lucida Console'; line-height:14pt; font-size:10pt;">#{msg}</pre>]
end

#function(&block) ⇒ Object

foo = function do |list| … foo.call @list



54
55
56
# File 'lib/lux/view/helper.rb', line 54

def function &block
  block
end

#helper(*names) ⇒ Object

helper(:main).method



109
110
111
# File 'lib/lux/view/helper.rb', line 109

def helper *names
  Lux::View::Helper.new(self, *names)
end

#no_white_spaceObject



32
33
34
# File 'lib/lux/view/helper.rb', line 32

def no_white_space
  yield.gsub(/>\s+</,'><')
end

#once(id = nil) ⇒ Object



113
114
115
116
117
# File 'lib/lux/view/helper.rb', line 113

def once id=nil
  Lux.current.once("template-#{id || caller[0]}") do
    block_given? ? yield : true
  end
end

#render(name, locals = {}) ⇒ Object

renders just template but it is called

render :_link, link

render ‘main/links/_link’, link



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lux/view/helper.rb', line 61

def render name, locals={}
  if name.is_array?
    return name.map { |b| render(b) }.join("\n")
  elsif name.respond_to?(:db_schema)
    path = Thread.current[:lux][:last_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}"
  else
    name = name.to_s
    name = "#{Thread.current[:lux][:last_template_path]}/#{name}" unless name.index('/')
  end

  Lux.current.files_in_use name

  for k, v in locals
    instance_variable_set("@_#{k}", v)
  end

  if block_given?
    name = "#{name}/layout" unless name.index('/')

    Lux::View.new(name, self).render_part { yield }
  else
    Lux::View.new(name, self).render_part
  end
end

#tag(name = nil, opts = {}, data = nil) ⇒ Object

tag :div, { ‘class’=>‘iform’ } do



101
102
103
104
105
106
# File 'lib/lux/view/helper.rb', line 101

def tag name=nil, opts={}, data=nil
  return HtmlTagBuilder unless name

  data = yield(opts) if block_given?
  HtmlTagBuilder.tag name, opts, data
end