Module: Ichiban::Helpers
- Included in:
- Ichiban::HTMLCompiler::Context
- Defined in:
- lib/ichiban/helpers.rb
Instance Method Summary collapse
- #_limit_options(hash, keys = []) ⇒ Object
- #capture ⇒ Object
- #concat(str) ⇒ Object
- #content_tag(*args) ⇒ Object
-
#current_path ⇒ Object
Returns the path relative to site root.
-
#javascript_include_tag(js_file) ⇒ Object
h is provided by ERB::Util.
- #layout(*stack) ⇒ Object (also: #layouts)
- #link_to(*args) ⇒ Object
-
#normalize_path(path) ⇒ Object
If the path has a leading slash, it will be made absolute using relative_url_root.
- #partial(path, options = {}) ⇒ Object
-
#path_with_slashes(path) ⇒ Object
Adds leading and trailing slashes if none are present.
- #relative_url_root ⇒ Object
- #stylesheet_link_tag(css_file, media = 'screen') ⇒ Object
- #tag(*args) ⇒ Object
- #tag_attrs(attrs) ⇒ Object
Instance Method Details
#_limit_options(hash, keys = []) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/ichiban/helpers.rb', line 43 def (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 |
#capture ⇒ Object
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 content_tag(*args) = args. name = args.shift content = block_given? ? yield : args.shift output = '<' + name output << tag_attrs() << ">#{content}</#{name}>" end |
#current_path ⇒ Object
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)) content_tag '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 |
#link_to(*args) ⇒ Object
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) = args. 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 content_tag 'a', text, .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, = {}) = {ivars: {}}.merge() 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([: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_root ⇒ Object
95 96 97 |
# File 'lib/ichiban/helpers.rb', line 95 def relative_url_root Ichiban.config.relative_url_root end |
#stylesheet_link_tag(css_file, media = 'screen') ⇒ Object
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 = args. open = args.shift "<#{name}#{tag_attrs()}#{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 |