Class: Pixiurge::Middleware::Tilt
- Inherits:
-
Object
- Object
- Pixiurge::Middleware::Tilt
- Defined in:
- lib/pixiurge/config_ru.rb
Overview
The Tilt middleware serves Tilt (template) directories. See https://github.com/rtomayko/tilt for the list of supported template engines.
Constant Summary
- MIME_TYPE_EXTENSION_MAP =
{ }
Instance Method Summary collapse
-
#call(env) ⇒ Object
The Rack .call method for middleware.
-
#initialize(app, options = {}) ⇒ Tilt
constructor
Constructor.
Constructor Details
#initialize(app, options = {}) ⇒ Tilt
Constructor. If you supply a list of engines, you'll need to make sure the appropriate gems are included for them. For instance if you supply "haml" as an engine, make sure to include the "haml" gem for your game.
This can be used to serve Ruby template files like Erb or Haml, but also Markdown, CoffeeScript, TypeScript, LiveScript, Less, Sass/Scss and more with the right gems. See https://github.com/rtomayko/tilt for the list of supported template engines.
For templating libraries like Erb that can handle any file type, this middleware will strip extensions. That means something ending in ".html.erb" will only be served if you ask for the same with ".html". You can do the same with something like CoffeeScript, so you could name your files whatever_filename.js.coffee if you want them to be converted to JS and served with a .js extension. But the middleware will also try to use sane extensions (convert .coffee to .js, etc) where it can. If you have trouble, try manually using the "stripped" extension trick (e.g. my_file.js.coffee or my_file.html.haml).
Note that multiple instances don't keep the extensions separate, so it may be hard to keep from serving Markdown files out of your Haml directory... Don't keep files in your asset directories if you're worried about your users seeing them, in general.
In production it will be faster to "bake" these directories into static files of the appropriate type instead of converting on the server.
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/pixiurge/config_ru.rb', line 311 def initialize(app, = {}) @app = app @urls = [([:urls] || "/tmx")].flatten @root = [:root] || Dir.pwd @tilt_scope = [:scope] || Object.new @tilt_locals = [:locals] || {} engines = [] if [:engines] engines = [[:engines]].flatten else engines = [ "erubis" ] end engines.each { |engine_name| require engine_name } bad_engines = engines.select { |eng| ::Tilt[eng].nil? } raise("No Tilt templating engine for engine(s): #{bad_engines.inspect}!") unless bad_engines.empty? tilt_engines = engines.map { |engine_name| ::Tilt[engine_name] } # We just want the list of extensions. We're going to use Tilt's # default preference priorities for a given extension. If # somebody wants different, they're allowed to call Tilt.prefer, # likely from config.ru. @extensions = tilt_engines.flat_map { |eng| ::Tilt.default_mapping.extensions_for(eng) }.uniq @mapped_extensions = {} tilt_engines.each do |engine| engine.default_mime_type end @encoding = [:encoding] || 'utf8' end |
Instance Method Details
#call(env) ⇒ Object
The Rack .call method for middleware.
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/pixiurge/config_ru.rb', line 348 def call(env) # If no Tilt path is matched, forward the call to the next middleware call_root = @urls.detect { |u| env["PATH_INFO"].index(u) == 0 } return @app.call(env) unless call_root # A directory was matched. Let's see if we have any file which, # when we strip the extension, would give us the requested # file. local_path = File.join(@root, env["PATH_INFO"]) local_extension = @extensions.detect { |ext| File.exist?(local_path + "." + ext) } # No match? 404. unless local_extension return [404, {}, [""]] end local_template_file = local_path + "." + local_extension # Caching these is faster than not. You know what's faster yet? # Rendering them into a nice static file and serving that via # NGinX. The easiest way to do that for nontrivial cases is # probably using 'curl' to get these from the server. template = ::Tilt.new local_template_file return [200, {}, [ template.render(@tilt_scope, @tilt_locals) ]] end |