| Module | Sinatra::Templates |
| In: |
lib/sinatra/base.rb
|
Template rendering methods. Each method takes the name of a template to render as a Symbol and returns a String with the rendered output, as well as an optional hash with additional options.
`template` is either the name or path of the template as symbol (Use `:’subdir/myview’` for views in subdirectories), or a string that will be rendered.
Possible options are:
:content_type The content type to use, same arguments as content_type.
:layout If set to false, no layout is rendered, otherwise
the specified layout is used (Ignored for `sass` and `less`)
:layout_engine Engine to use for rendering the layout.
:locals A hash with local variables that should be available
in the template
:scope If set, template is evaluate with the binding of the given
object rather than the application instance.
:views Views directory to use.
# File lib/sinatra/base.rb, line 461
461: def initialize
462: super
463: @default_layout = :layout
464: end
# File lib/sinatra/base.rb, line 493
493: def builder(template=nil, options={}, locals={}, &block)
494: options[:default_content_type] = :xml
495: render_ruby(:builder, template, options, locals, &block)
496: end
# File lib/sinatra/base.rb, line 522
522: def coffee(template, options={}, locals={})
523: options.merge! :layout => false, :default_content_type => :js
524: render :coffee, template, options, locals
525: end
# File lib/sinatra/base.rb, line 466
466: def erb(template, options={}, locals={})
467: render :erb, template, options, locals
468: end
# File lib/sinatra/base.rb, line 470
470: def erubis(template, options={}, locals={})
471: render :erubis, template, options, locals
472: end
Calls the given block for every possible template file in views, named name.ext, where ext is registered on engine.
# File lib/sinatra/base.rb, line 538
538: def find_template(views, name, engine)
539: yield ::File.join(views, "#{name}.#{@preferred_extension}")
540: Tilt.mappings.each do |ext, engines|
541: next unless ext != @preferred_extension and Array(engines).include? engine
542: yield ::File.join(views, "#{name}.#{ext}")
543: end
544: end
# File lib/sinatra/base.rb, line 474
474: def haml(template, options={}, locals={})
475: render :haml, template, options, locals
476: end
# File lib/sinatra/base.rb, line 488
488: def less(template, options={}, locals={})
489: options.merge! :layout => false, :default_content_type => :css
490: render :less, template, options, locals
491: end
# File lib/sinatra/base.rb, line 498
498: def liquid(template, options={}, locals={})
499: render :liquid, template, options, locals
500: end
# File lib/sinatra/base.rb, line 518
518: def markaby(template=nil, options={}, locals={}, &block)
519: render_ruby(:mab, template, options, locals, &block)
520: end
# File lib/sinatra/base.rb, line 502
502: def markdown(template, options={}, locals={})
503: render :markdown, template, options, locals
504: end
# File lib/sinatra/base.rb, line 527
527: def nokogiri(template=nil, options={}, locals={}, &block)
528: options[:default_content_type] = :xml
529: render_ruby(:nokogiri, template, options, locals, &block)
530: end
# File lib/sinatra/base.rb, line 514
514: def radius(template, options={}, locals={})
515: render :radius, template, options, locals
516: end
# File lib/sinatra/base.rb, line 510
510: def rdoc(template, options={}, locals={})
511: render :rdoc, template, options, locals
512: end
# File lib/sinatra/base.rb, line 478
478: def sass(template, options={}, locals={})
479: options.merge! :layout => false, :default_content_type => :css
480: render :sass, template, options, locals
481: end
# File lib/sinatra/base.rb, line 483
483: def scss(template, options={}, locals={})
484: options.merge! :layout => false, :default_content_type => :css
485: render :scss, template, options, locals
486: end
# File lib/sinatra/base.rb, line 532
532: def slim(template, options={}, locals={})
533: render :slim, template, options, locals
534: end