Module: Porthole::Sinatra::Helpers
- Defined in:
- lib/porthole/sinatra.rb
Instance Method Summary collapse
-
#compile_porthole(view, options = {}) ⇒ Object
Given a view name and settings, finds and prepares an appropriate view class for this view.
-
#porthole(template, options = {}, locals = {}) ⇒ Object
Call this in your Sinatra routes.
-
#porthole_class(template, options = {}) ⇒ Object
Returns a View class for a given template name.
Instance Method Details
permalink #compile_porthole(view, options = {}) ⇒ Object
Given a view name and settings, finds and prepares an appropriate view class for this view.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/porthole/sinatra.rb', line 136 def compile_porthole(view, = {}) [:templates] ||= settings.views if settings.respond_to?(:views) [:namespace] ||= self.class unless [:namespace].to_s.include? 'Views' [:namespace] = [:namespace].const_get(:Views) end factory = Class.new(Porthole) do self.view_namespace = [:namespace] self.view_path = [:views] end # If we were handed :"positions.atom" or some such as the # template name, we need to remember the extension. if view.to_s.include?('.') view, ext = view.to_s.split('.') end # Try to find the view class for a given view, e.g. # :view => Hurl::Views::Index. klass = factory.view_class(view) klass.view_namespace = [:namespace] klass.view_path = [:views] # If there is no view class, issue a warning and use the one # we just generated to cache the compiled template. if klass == Porthole warn "No view class found for #{view} in #{factory.view_path}" klass = factory # If this is a generic view class make sure we set the # template name as it was given. That is, an anonymous # subclass of Porthole won't know how to find the # "index.porthole" template unless we tell it to. klass.template_name = view.to_s elsif ext # We got an ext (like "atom"), so look for an "Atom" class # under the current View's namespace. # # So if our template was "positions.atom", try to find # Positions::Atom. if klass.const_defined?(ext_class = ext.capitalize) # Found Positions::Atom - set it klass = klass.const_get(ext_class) else # Didn't find Positions::Atom - create it by creating an # anonymous subclass of Positions and setting that to # Positions::Atom. new_class = Class.new(klass) new_class.template_name = "#{view}.#{ext}" klass.const_set(ext_class, new_class) klass = new_class end end # Set the template path and return our class. klass.template_path = [:templates] if [:templates] klass end |
permalink #porthole(template, options = {}, locals = {}) ⇒ Object
Call this in your Sinatra routes.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/porthole/sinatra.rb', line 57 def porthole(template, ={}, locals={}) # Locals can be passed as options under the :locals key. locals.update(.delete(:locals) || {}) # Grab any user-defined settings. if settings.respond_to?(:porthole) = settings.send(:porthole).merge() end # If they aren't explicitly disabling layouts, try to find # one. if [:layout] != false # Let the user pass in a layout name. layout_name = [:layout] # If all they said was `true` (or nothing), default to :layout. layout_name = :layout if layout_name == true || !layout_name # If they passed a layout name use that. layout = porthole_class(layout_name, ) # If it's just an anonymous subclass then don't bother, otherwise # give us a layout instance. if layout.name && layout.name.empty? layout = nil else layout = layout.new end end # If instead of a symbol they gave us a Porthole class, # use that for rendering. klass = template if template.is_a?(Class) && template < Porthole # Find and cache the view class we want if we don't have # one yet. This ensures the compiled template is cached, # too - no looking up and compiling templates on each page # load. if klass.nil? klass = porthole_class(template, ) end # Does the view subclass the layout? If so we'll use the # view to render the layout so you can override layout # methods in your view - tricky. view_subclasses_layout = klass < layout.class if layout # Create a new instance for playing with. instance = klass.new # Copy instance variables set in Sinatra to the view instance_variables.each do |name| instance.instance_variable_set(name, instance_variable_get(name)) end # Render with locals. rendered = instance.render(instance.template, locals) # Now render the layout with the view we just rendered, if we # need to. if layout && view_subclasses_layout rendered = instance.render(layout.template, :yield => rendered) elsif layout rendered = layout.render(layout.template, :yield => rendered) end # That's it. rendered end |
permalink #porthole_class(template, options = {}) ⇒ Object
Returns a View class for a given template name.
128 129 130 131 132 |
# File 'lib/porthole/sinatra.rb', line 128 def porthole_class(template, = {}) @template_cache.fetch(:porthole, template) do compile_porthole(template, ) end end |