Module: Sinatra::CSS::Helpers
- Defined in:
- lib/sinatra/css.rb
Instance Attribute Summary collapse
-
#sinatra_css_custom_code ⇒ Object
Returns the value of attribute sinatra_css_custom_code.
-
#sinatra_css_custom_files ⇒ Object
Returns the value of attribute sinatra_css_custom_files.
Instance Method Summary collapse
-
#css(path = nil, attrs = {}, &block) ⇒ Object
(also: #stylesheet)
Return stylesheet link tag to path.
-
#css_custom(css = nil) ⇒ Object
Outputs any custom CSS if provided from within a view, helper method and so on.
-
#css_custom_add(css) ⇒ Object
Adds custom CSS to the page load from within a view, helper method and so on.
-
#css_custom_add_file(file, insert_into_html = nil, path = nil) ⇒ Object
Add a custom CSS file to the page load from within a view, helper method and so on.
-
#css_custom_files ⇒ Object
Outputs any custom CSS files that have been included.
-
#css_insert_file(path = '', local = nil) ⇒ Object
Simple convenicence method that takes a path to a CSS file and inserts its content into the current
.erb
file.
Instance Attribute Details
#sinatra_css_custom_code ⇒ Object
Returns the value of attribute sinatra_css_custom_code.
275 276 277 |
# File 'lib/sinatra/css.rb', line 275 def sinatra_css_custom_code @sinatra_css_custom_code end |
#sinatra_css_custom_files ⇒ Object
Returns the value of attribute sinatra_css_custom_files.
275 276 277 |
# File 'lib/sinatra/css.rb', line 275 def sinatra_css_custom_files @sinatra_css_custom_files end |
Instance Method Details
#css(path = nil, attrs = {}, &block) ⇒ Object Also known as: stylesheet
Return stylesheet link tag to path. When a block is passed, a style tag will be created with the yielded value as its contents.
Examples
css do
"body {
color: blue;
}"
end
# => <style>body { ... }</style>
css('/css/style.css', :media => :print) # =>
<link rel="stylesheet" href="/css/style.css" media="print" type="text/css" charset="utf-8">
297 298 299 300 301 302 |
# File 'lib/sinatra/css.rb', line 297 def css(path = nil, attrs = {}, &block) attrs = { :type => 'text/css', :media => "screen" }.merge(attrs) return tag(:style, yield, attrs) if block_given? path = url_for("#{path.sub('.css','')}.css") unless remote_asset?(path) tag(:link, { :rel => 'stylesheet', :charset => "utf-8", :href => path, :newline => true }.merge(attrs)) end |
#css_custom(css = nil) ⇒ Object
Outputs any custom CSS if provided from within a view, helper method and so on
Examples
custom_css() => <style...> custom css </style>
408 409 410 411 412 413 |
# File 'lib/sinatra/css.rb', line 408 def css_custom(css=nil) out = '' out << css unless css.nil? @sinatra_css_custom_code.each { |i| out << " #{i}\n" } unless @sinatra_css_custom_code.nil? out = out.empty? ? '' : %Q[<style type="text/css" media="screen">\n#{out.strip}\n </style>\n] end |
#css_custom_add(css) ⇒ Object
Adds custom CSS to the page load from within a view, helper method and so on
Examples
css_custom_add("body{color: green;}")
=> output within <style>.. tag in the <head> of document
314 315 316 317 |
# File 'lib/sinatra/css.rb', line 314 def css_custom_add(css) @sinatra_css_custom_code ||= [] @sinatra_css_custom_code << css end |
#css_custom_add_file(file, insert_into_html = nil, path = nil) ⇒ Object
Add a custom CSS file to the page load from within a view, helper method and so on
Examples
css_custom_add_file [filename, media]
=> output within <link href..>.. tag in the <head> of document
You can also embed the styles of a .css file into the head element of a page. NB! path is starting from APP_ROOT/public/
css_custom_add_file('home.css',:insert_into_html)
=> <style type="text/css" media="screen"> CSS content </style>
You can even give a file system path to embed the styles of a .css file. NB! make sure the path and .css file works together as a real path. :path should always be a directory without the trailing slash.
css_custom_add_file('home.css',:insert_into_html, '/path/2/some/directory')
=> <style type="text/css" media="screen"> Some Style CSS content </style>
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/sinatra/css.rb', line 341 def css_custom_add_file(file, insert_into_html = nil, path = nil) if insert_into_html.nil? @sinatra_css_custom_files ||= [] file = [file,:screen] unless file.is_a?(Array) @sinatra_css_custom_files << file else # read the file into css_custom_add path_css = path.nil? ? self.class.public : path file_css = "#{path_css}/#{file.sub('.css','')}.css" if test(?f, file_css) css_custom_add(IO.read(file_css)) else err_msg = "ERROR: css_custom_add_file(:insert_into_html) method could NOT find and embed this CSS file=[ #{file_css} ]" if self.respond_to?(:logger) logger.warn(err_msg) else warn(err_msg) end end end end |
#css_custom_files ⇒ Object
Outputs any custom CSS files that have been included
Examples
css_custom_files =>
<!-- custom css files -->
<link href="/css/custom1.css"...>
<link href="/css/custom2.css"...>
<!-- /custom css files -->
427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/sinatra/css.rb', line 427 def css_custom_files unless @sinatra_css_custom_files.nil? out = "<!-- custom css files -->\n" @sinatra_css_custom_files.each do |file| # returns an array out << css(file[0], :media => file[1]) end out << "<!-- /custom css files -->\n" else '' # return empty string, it's better than nil in this case end end |
#css_insert_file(path = '', local = nil) ⇒ Object
Simple convenicence method that takes a path to a CSS file and inserts its content into the current .erb
file
Depends upon the settings of the :css_compiled_files_dir
configuration variable defined inside your application. By default it is set to '//Users/kematzy/.alt/css'
Examples
# in your app's routes configurations
get '/css/screen.css' { }
get('/css/screen.css') do
content_type 'text/css'
erb('css/screen.css'.to_sym, :layout => false)
end
# in views/css/screen.css
css_insert_file('blueprint/grid')
which inserts the CSS code from that file into the output.
389 390 391 392 393 394 395 396 397 398 |
# File 'lib/sinatra/css.rb', line 389 def css_insert_file(path = '', local = nil ) file_path = local.nil? ? "#{self.class.css_shared_source_files_dir}/#{path}" : path file_path = file_path.sub(/\.css$/,'') << ".css" if test(?f, file_path) content = IO.read(file_path) else content = "/* ERROR: the CSS file [#{file_path}] could NOT be found */" end content end |