Module: Sinatra::Ratpack
- Defined in:
- lib/sinatra/ratpack.rb
Instance Method Summary collapse
-
#content_tag(name, content, options = {}) ⇒ Object
Just like Rails’ content_tag.
-
#image_tag(src, options = {}) ⇒ Object
Accepts a full URL, an image filename, or a path underneath /public/images/.
-
#javascript_include_tag(string_or_array, *args) ⇒ Object
Accepts a single filename or an array of filenames (with or without .js extension) Assumes javascripts are in public/javascripts.
-
#link_to(content, href = nil, options = {}) ⇒ Object
Works like link_to, but href is optional.
-
#stylesheet_link_tag(string_or_array, *args) ⇒ Object
Accepts a single filename or an array of filenames (with or without .css extension) Assumes stylesheets are in public/stylesheets.
-
#tag(name, options = {}) ⇒ Object
Just like Rails’ tag.
-
#url_for(url_fragment, mode = :path_only) ⇒ Object
Construct a link to
url_fragment, which should be given relative to the base of this Sinatra app.
Instance Method Details
#content_tag(name, content, options = {}) ⇒ Object
Just like Rails’ content_tag
content_tag :div, "hello", :id => "foo" # <div id="foo">hello</div>
73 74 75 |
# File 'lib/sinatra/ratpack.rb', line 73 def content_tag(name,content,={}) "<#{name} #{options.to_html_attrs}>#{content}</#{name}>" end |
#image_tag(src, options = {}) ⇒ Object
Accepts a full URL, an image filename, or a path underneath /public/images/
image_tag "pony.png" # <image src="/images/pony.png" />
image_tag "http://foo.com/pony.png" # <image src="http://foo.com/pony.png" />
53 54 55 56 57 |
# File 'lib/sinatra/ratpack.rb', line 53 def image_tag(src, ={}) src = "/images/#{src}" unless src.include? "://" # Add images directory to path if not a full URL [:src] = url_for(src) tag(:img, ) end |
#javascript_include_tag(string_or_array, *args) ⇒ Object
Accepts a single filename or an array of filenames (with or without .js extension) Assumes javascripts are in public/javascripts
javascript_include_tag "jquery.min" # <script charset="utf-8" src="/javascripts/jquery.min.js" type="text/javascript"></script>
javascript_include_tag "jquery.min.js" # <script charset="utf-8" src="/javascripts/jquery.min.js" type="text/javascript"></script>
javascript_include_tag %w(day night) # <script charset="utf-8" src="/javascripts/day.js" type="text/javascript"></script>\n<script charset="utf-8" src="/javascripts/night.js" type="text/javascript"></script>
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/sinatra/ratpack.rb', line 12 def javascript_include_tag(string_or_array, *args) files = string_or_array.is_a?(Array) ? string_or_array : [string_or_array] = { :charset => "utf-8", :type => "text/javascript", }.merge(args.) files.map do |file| path = "#{ file.gsub(/\.js/i, "") }.js" # Append .js if needed path = "/javascripts/#{path}" unless path.include? "://" # Add stylesheets directory to path if not a full URL [:src] = url_for(path) content_tag(:script, "", ) end.join("\n") end |
#link_to(content, href = nil, options = {}) ⇒ Object
Works like link_to, but href is optional. If no href supplied, content is used as href
link_to "grub", "/food", :class => "eats" # <a href="/food" class="eats">grub</a>
link_to "http://foo.com" # <a href="http://foo.com">http://foo.com</a>
link_to "home" # <a href="/home">home</a>
64 65 66 67 68 |
# File 'lib/sinatra/ratpack.rb', line 64 def link_to(content,href=nil,={}) href ||= content .update :href => url_for(href) content_tag :a, content, end |
#stylesheet_link_tag(string_or_array, *args) ⇒ Object
Accepts a single filename or an array of filenames (with or without .css extension) Assumes stylesheets are in public/stylesheets
stylesheet_link_tag "styles", :media => "print" # <link charset="utf-8" href="/stylesheets/styles.css" media="print" rel="stylesheet" type="text/css" />
stylesheet_link_tag %w(winter summer) # <link charset="utf-8" href="/stylesheets/winter.css" media="projection" rel="stylesheet" type="text/css" />\n<link charset="utf-8" href="/stylesheets/summer.css" media="projection" rel="stylesheet" type="text/css" />
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/sinatra/ratpack.rb', line 32 def stylesheet_link_tag(string_or_array, *args) files = string_or_array.is_a?(Array) ? string_or_array : [string_or_array] = { :charset => "utf-8", :media => "screen, projection", :rel => "stylesheet", :type => "text/css", }.merge(args.) files.map do |file| path = "#{ file.gsub(/\.css/i, "") }.css" # Append .css if needed path = "/stylesheets/#{path}" unless path.include? "://" # Add stylesheets directory to path if not a full URL [:href] = url_for(path) tag(:link, ) end.join("\n") end |
#tag(name, options = {}) ⇒ Object
Just like Rails’ tag
tag :br, :class => "foo" # <br class="foo" />
80 81 82 |
# File 'lib/sinatra/ratpack.rb', line 80 def tag(name,={}) "<#{name} #{options.to_html_attrs} />" end |
#url_for(url_fragment, mode = :path_only) ⇒ Object
Construct a link to url_fragment, which should be given relative to the base of this Sinatra app. The mode should be either :path_only, which will generate an absolute path within the current domain (the default), or :full, which will include the site name and port number. (The latter is typically necessary for links in RSS feeds.) Example usage:
url_for "/" # "/myapp/"
url_for "/foo" # "/myapp/foo"
url_for "/foo", :full # "http://example.com/myapp/foo"
url_for "http://bar.com" # "http://bar.com"
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/sinatra/ratpack.rb', line 95 def url_for url_fragment, mode=:path_only return url_fragment if url_fragment.include? "://" url_fragment = "/#{url_fragment}" unless url_fragment.starts_with? "/" case mode when :path_only base = request.script_name when :full scheme = request.scheme if (scheme == 'http' && request.port == 80 || scheme == 'https' && request.port == 443) port = "" else port = ":#{request.port}" end base = "#{scheme}://#{request.host}#{port}#{request.script_name}" else raise TypeError, "Unknown url_for mode #{mode}" end "#{base}#{url_fragment}" end |