Permalink
Browse files

Merge branch 'master' into context_miss

  • Loading branch information...
2 parents 1c7604c + a9371fe commit 2e77eafddffa388f6f89cc02f0f933c876a1ebcd @defunkt defunkt committed Oct 27, 2009
View
@@ -9,6 +9,7 @@
* Unclosed sections now throw a helpful error message
* Report line numbers on unclosed section errors
+* Added Rack::Bug panel
## 0.3.2 (2009-10-19)
View
@@ -366,6 +366,23 @@ An example Sinatra application is also provided:
<http://github.com/defunkt/mustache-sinatra-example>
+[Rack::Bug][4]
+---------
+
+Mustache also ships with a `Rack::Bug` panel. In your `config.ru` add
+the following code:
+
+ require 'rack/bug/panels/mustache_panel'
+ use Rack::Bug::MustachePanel
+
+Using Rails? Add this to your initializer or environment file:
+
+ require 'rack/bug/panels/mustache_panel'
+ config.middleware.use "Rack::Bug::MustachePanel"
+
+[![Rack::Bug](http://img.skitch.com/20091027-xyf4h1yxnefpp7usyddrcmc7dn.png)][5]
+
+
Vim
---
@@ -407,3 +424,5 @@ Meta
[1]: http://code.google.com/p/google-ctemplate/
[2]: http://www.ivan.fomichev.name/2008/05/erlang-template-engine-prototype.html
[3]: http://google-ctemplate.googlecode.com/svn/trunk/doc/howto.html
+[4]: http://github.com/brynary/rack-bug/
+[5]: http://img.skitch.com/20091027-n8pxwwx8r61tc318a15q1n6m14.png
@@ -0,0 +1,58 @@
+module Rack
+ module Bug
+ class MustachePanel < Panel
+ require "rack/bug/panels/mustache_panel/mustache_extension"
+
+ class View < Mustache
+ self.path = ::File.dirname(__FILE__) + '/mustache_panel'
+
+ def times
+ MustachePanel.times.map do |key, value|
+ { :key => key, :value => value }
+ end
+ end
+
+ def variables
+ vars = MustachePanel.variables.sort_by { |key, _| key.to_s }
+ vars.map do |key, value|
+ if value.is_a?(Array) && value.size > 10
+ size = value.size
+ value = value.first(10)
+ value << "...and #{size - 10} more"
+ end
+ { :key => key, :value => value.inspect }
+ end
+ end
+ end
+
+ def self.reset
+ Thread.current["rack.bug.mustache.times"] = {}
+ Thread.current["rack.bug.mustache.vars"] = {}
+ end
+
+ def self.times
+ Thread.current["rack.bug.mustache.times"] ||= {}
+ end
+
+ def self.variables
+ Thread.current["rack.bug.mustache.vars"] ||= {}
+ end
+
+ def name
+ "mustache"
+ end
+
+ def heading
+ "{{%.2fms}}" % self.class.times.values.inject(0.0) do |sum, obj|
+ sum + obj
+ end
+ end
+
+ def content
+ View.render
+ ensure
+ self.class.reset
+ end
+ end
+ end
+end
@@ -0,0 +1,25 @@
+if defined? Mustache
+ Mustache.class_eval do
+ alias_method :real_render, :render
+
+ def render(*args, &block)
+ out = ''
+ Rack::Bug::MustachePanel.times[self.class.name] = Benchmark.realtime do
+ out = real_render(*args, &block)
+ end
+ out
+ end
+
+ alias_method :to_html, :render
+ alias_method :to_text, :render
+ end
+
+ Mustache::Context.class_eval do
+ alias_method :real_get, :[]
+
+ def [](name)
+ return real_get(name) if name == :yield || !@mustache.respond_to?(name)
+ Rack::Bug::MustachePanel.variables[name] = real_get(name)
+ end
+ end
+end
@@ -0,0 +1,32 @@
+<h3>Render Times</h3>
+
+<table>
+ <tr>
+ <th>View</th>
+ <th>Render Time</th>
+ </tr>
+
+ {{# times }}
+ <tr>
+ <td>{{ key }}</td>
+ <td>{{ value }}</td>
+ </tr>
+ {{/ times }}
+</table>
+
+<h3>Variables</h3>
+
+<table>
+ <tr>
+ <th>Name</th>
+ <th>Value</th>
+ </tr>
+
+ {{# variables }}
+ <tr>
+ <td>{{ key }}</td>
+ <td>{{ value }}</td>
+ </tr>
+ {{/ variables }}
+</table>
+

0 comments on commit 2e77eaf

Please sign in to comment.