Class: Bones::Template
Overview
Template - loads template file based on request path information and compiles it using ERB
Instance Attribute Summary collapse
-
#layout(arg = -1)) ⇒ Object
Gets/sets layout If no argument is passed, the layout is returned; otherwise, sets the layout (use false or nil to turn off the layout).
-
#options ⇒ Object
Returns the value of attribute options.
-
#path ⇒ Object
Returns the value of attribute path.
-
#request ⇒ Object
Returns the value of attribute request.
Class Method Summary collapse
-
.compile(*args) ⇒ Object
Short-hand for compiling a template.
-
.helpers_to_include ⇒ Object
Returns array of all helper files that should be included.
-
.include_helpers ⇒ Object
Load all available helpers.
- .template_for_request(request) ⇒ Object
Instance Method Summary collapse
-
#compile ⇒ Object
Compiles the template (along with the layout if necessary).
-
#filename ⇒ Object
Full path to template file.
-
#initialize(path, layout = -1,, options = {}) ⇒ Template
constructor
Initialize template with path and optional layout.
- #inspect ⇒ Object
-
#layout_filename ⇒ Object
Full path to layout file.
-
#local_assigns_source ⇒ Object
Generates source for local variable assignments.
-
#pages ⇒ Object
Returns array of pages.
-
#partial(name, options = {}) ⇒ Object
Renders partial template - an underscore is automatically added to the passed name, so <%= partial ‘footer’ %> will render the ‘_footer.html.erb’ template.
- #source ⇒ Object
Constructor Details
#initialize(path, layout = -1,, options = {}) ⇒ Template
Initialize template with path and optional layout
37 38 39 40 41 42 43 |
# File 'lib/bones/template.rb', line 37 def initialize(path, layout=-1, ={}) @path = path.gsub(/\.html|\.html\.erb/, '') @layout = layout == -1 ? 'application' : layout @options = self.class.include_helpers end |
Instance Attribute Details
#layout(arg = -1)) ⇒ Object
Gets/sets layout If no argument is passed, the layout is returned; otherwise, sets the layout (use false or nil to turn off the layout)
75 76 77 |
# File 'lib/bones/template.rb', line 75 def layout @layout end |
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/bones/template.rb', line 8 def @options end |
#path ⇒ Object
Returns the value of attribute path.
6 7 8 |
# File 'lib/bones/template.rb', line 6 def path @path end |
#request ⇒ Object
Returns the value of attribute request.
9 10 11 |
# File 'lib/bones/template.rb', line 9 def request @request end |
Class Method Details
.compile(*args) ⇒ Object
Short-hand for compiling a template
119 120 121 |
# File 'lib/bones/template.rb', line 119 def self.compile(*args) Template.new(*args).compile end |
.helpers_to_include ⇒ Object
Returns array of all helper files that should be included
13 14 15 16 17 18 |
# File 'lib/bones/template.rb', line 13 def self.helpers_to_include files = [ Dir.glob(Bones.system_path / 'helpers/*_helper.rb'), Dir.glob(Bones.root / 'helpers/*_helper.rb') ].flatten end |
.include_helpers ⇒ Object
Load all available helpers
21 22 23 24 25 26 27 |
# File 'lib/bones/template.rb', line 21 def self.include_helpers helpers_to_include.each do |filename| klass = File.basename(filename, '.rb').camelize force_load klass => filename include klass.constantize end end |
.template_for_request(request) ⇒ Object
29 30 31 |
# File 'lib/bones/template.rb', line 29 def self.template_for_request(request) request.path_info end |
Instance Method Details
#compile ⇒ Object
Compiles the template (along with the layout if necessary)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/bones/template.rb', line 91 def compile # go away, favicon requests return String.new if File.basename(filename) =~ /^favicon.ico/ @content_for_layout = eval(source) if layout && File.file?(layout_filename) erb = ERB.new(File.read(layout_filename)) eval(erb.src) do |*keys| key = keys.first key = :layout if key.blank? instance_variable_get(:"@content_for_#{key}") end else @content_for_layout end end |
#filename ⇒ Object
Full path to template file
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/bones/template.rb', line 50 def filename if self.path =~ /raw$/ layout false Bones.system_path / 'pages' / 'directory.html.erb' else path = Bones.pages_path / @path path /= 'index' if File.directory?(path) or path.ends_with?('/') path += '.html.erb' end end |
#inspect ⇒ Object
45 46 47 |
# File 'lib/bones/template.rb', line 45 def inspect '#<Bones::Template @path="%s" @layout="%s">' % [path, layout] end |
#layout_filename ⇒ Object
Full path to layout file
67 68 69 |
# File 'lib/bones/template.rb', line 67 def layout_filename Bones.layouts_path / layout.to_s + '.html.erb' end |
#local_assigns_source ⇒ Object
Generates source for local variable assignments
110 111 112 113 114 115 116 |
# File 'lib/bones/template.rb', line 110 def local_assigns_source src = [] ([:locals] || {}).each do |key, value| src << "#{key} = #{value.inspect};\n" end src.join end |
#pages ⇒ Object
Returns array of pages
62 63 64 |
# File 'lib/bones/template.rb', line 62 def pages Bones.pages end |
#partial(name, options = {}) ⇒ Object
Renders partial template - an underscore is automatically added to the passed name, so <%= partial ‘footer’ %> will render the ‘_footer.html.erb’ template.
127 128 129 130 131 132 133 134 |
# File 'lib/bones/template.rb', line 127 def partial(name, ={}) path = name.to_s.split('/') path.last.gsub!(/^([^_])/, '_\1') name = path.join('/') template = Template.new(name, false, ) template.request = request template.compile end |
#source ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/bones/template.rb', line 80 def source unless File.exist?(filename) raise "Template missing\n#{filename}" end src = ERB.new(File.read(filename)).src src = (local_assigns_source || '') + (src || '') end |