Module: Ichiban::Helpers

Included in:
Ichiban::HTMLCompiler::Context
Defined in:
lib/ichiban/helpers.rb

Instance Method Summary collapse

Instance Method Details

#_limit_options(hash, keys = []) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/ichiban/helpers.rb', line 43

def _limit_options(hash, keys = [])
  keys = keys.collect(&:to_s)
  hash.inject({}) do |result, (key, value)|
    result[key] = value if (keys.include?(key.to_s) or (block_given? and yield(key, value)))
    result
  end
end

#captureObject



3
4
5
6
7
8
9
10
# File 'lib/ichiban/helpers.rb', line 3

def capture
  before_buffering = @_erb_out.dup
  @_erb_out.replace('')
  yield
  captured_output = @_erb_out.dup
  @_erb_out.replace(before_buffering)
  captured_output
end

#concat(str) ⇒ Object



12
13
14
# File 'lib/ichiban/helpers.rb', line 12

def concat(str)
  @_erb_out << str
end

#content_tag(*args) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/ichiban/helpers.rb', line 16

def (*args)
  options = args.extract_options!
  name = args.shift
  content = block_given? ? yield : args.shift
  output = '<' + name
  output << tag_attrs(options) << ">#{content}</#{name}>"
end

#current_pathObject

Returns the path relative to site root. Includes leading and trailing slash.



25
26
27
# File 'lib/ichiban/helpers.rb', line 25

def current_path
  @_current_path
end

#javascript_include_tag(js_file) ⇒ Object

h is provided by ERB::Util.



31
32
33
34
35
# File 'lib/ichiban/helpers.rb', line 31

def javascript_include_tag(js_file)
  js_file = js_file + '.js' unless js_file.end_with?('.js')
  path = normalize_path(File.join('/js', js_file))
   'script', 'type' => 'text/javascript', 'src' => path
end

#layout(*stack) ⇒ Object Also known as: layouts



37
38
39
# File 'lib/ichiban/helpers.rb', line 37

def layout(*stack)
  @_layout_stack = stack
end


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ichiban/helpers.rb', line 51

def link_to(*args)
  options = args.extract_options!
  if args.length == 1
    text = url = args[0]
  else
    text = args[0]
    url = args[1]
  end
  if url.is_a?(String)
    url = normalize_path(url)
  else
    url = url.to_param
  end
   'a', text, options.merge('href' => url)
end

#normalize_path(path) ⇒ Object

If the path has a leading slash, it will be made absolute using relative_url_root. Otherwise, it will remain relative.



69
70
71
72
73
74
75
# File 'lib/ichiban/helpers.rb', line 69

def normalize_path(path)
  if path.start_with?('/')
    File.join(relative_url_root, path)
  else
    path
  end
end

#partial(path, options = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/ichiban/helpers.rb', line 84

def partial(path, options = {})
  options = {ivars: {}}.merge(options)
  file = Ichiban::PartialHTMLFile.new(
    File.join('html', path)
  )
  compiler = Ichiban::HTMLCompiler.new(file)
  # to_hash is inherited from Erubis::Context. It's a hash of the instance variables.
  compiler.ivars = to_hash.merge(options[:ivars])
  compiler.compile_to_str
end

#path_with_slashes(path) ⇒ Object

Adds leading and trailing slashes if none are present



78
79
80
81
82
# File 'lib/ichiban/helpers.rb', line 78

def path_with_slashes(path)
  path = '/' + path unless path.start_with?('/')
  path << '/' unless path.end_with?('/')
  path
end

#relative_url_rootObject



95
96
97
# File 'lib/ichiban/helpers.rb', line 95

def relative_url_root
  Ichiban.config.relative_url_root
end


99
100
101
102
103
# File 'lib/ichiban/helpers.rb', line 99

def stylesheet_link_tag(css_file, media = 'screen')
  css_file = css_file + '.css' unless css_file.end_with?('.css')
  href = normalize_path(File.join('/css', css_file))
  tag 'link', 'href' => href, 'type' => 'text/css', 'rel' => 'stylesheet', 'media' => media
end

#tag(*args) ⇒ Object



105
106
107
108
109
110
# File 'lib/ichiban/helpers.rb', line 105

def tag(*args)
  name = args.shift
  options = args.extract_options!
  open = args.shift
  "<#{name}#{tag_attrs(options)}#{open ? '>' : '/>'}"
end

#tag_attrs(attrs) ⇒ Object



112
113
114
115
116
# File 'lib/ichiban/helpers.rb', line 112

def tag_attrs(attrs)
  attrs.inject('') do |result, (key, value)|
    result + " #{key}=\"#{h(value)}\""
  end
end