Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Restore "%" ERb/Erubis Trim Mode #7033

Merged
merged 1 commit into from

5 participants

@kron4eg

Fixed conflict from #5915 pull request

@JEG2

Sweet. Thanks!

@steveklabnik
Collaborator

This will need a rebase.

@steveklabnik
Collaborator

Still says it can't be merged. :/

@kron4eg kron4eg Restoring the '%' trim mode for ERb templates, allowing for a leading…
… percent sign on a line to indicate non-inserted Ruby code.
e82ffea
@kron4eg

CHANGELOG.md gets updated very often, causing conflict. rebased again.

@spastorino spastorino merged commit 46b8bce into rails:master
@dhh
Owner
dhh commented

I'm not a fan of this at all. The resulting view code looks like a mess. Going to revert.

@dhh dhh referenced this pull request from a commit
@dhh dhh Revert "Merge pull request #7033 from kron4eg/master". Not a a fan at…
… all of what this makes ERB files look like.

This reverts commit 46b8bce, reversing
changes made to 2f58795.
666d3fd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 7, 2012
  1. @kron4eg

    Restoring the '%' trim mode for ERb templates, allowing for a leading…

    kron4eg authored
    … percent sign on a line to indicate non-inserted Ruby code.
This page is out of date. Refresh to see the latest.
View
12 actionpack/CHANGELOG.md
@@ -1,5 +1,17 @@
## Rails 4.0.0 (unreleased) ##
+* Restored support for the "%" ERb/Erubis _trim mode_. This can be activated with:
+
+ config.action_view.erb_trim_mode = "%" # or "%-" whitespace trim
+
+ With that mode active, you can use a single percent sign at the beginning of a line to engage Ruby mode (without inserting results). It allows for template code like this:
+
+ % if current_user.try(:admin?)
+ <%= render "edit_links" %>
+ % end
+
+ *James Edward Gray II*
+
* `javascript_include_tag :all` will now not include `application.js` if the file does not exists. *Prem Sichanugrist*
* Send an empty response body when call `head` with status between 100 and 199, 204, 205 or 304.
View
14 actionpack/lib/action_view/template/handlers/erb.rb
@@ -37,6 +37,10 @@ def add_postamble(src)
end
end
+ class ErubisWithPercentLine < Erubis
+ include ::Erubis::PercentLineEnhancer
+ end
+
class ERB
# Specify trim mode for the ERB compiler. Defaults to '-'.
# See ERB documentation for suitable values.
@@ -76,10 +80,12 @@ def call(template)
# Always make sure we return a String in the default_internal
erb.encode!
- self.class.erb_implementation.new(
- erb,
- :trim => (self.class.erb_trim_mode == "-")
- ).src
+ mode = self.class.erb_trim_mode.to_s
+ implementation = self.class.erb_implementation
+ if mode.include? "%" and implementation == Erubis
+ implementation = ErubisWithPercentLine
+ end
+ implementation.new(erb, :trim => mode.include?("-")).src
end
private
View
54 actionpack/test/template/erb/handlers_test.rb
@@ -0,0 +1,54 @@
+require "abstract_unit"
+
+class HandlersTest < ActiveSupport::TestCase
+ HANDLER = ActionView::Template::Handlers::ERB
+ Template = Struct.new(:source)
+
+ extend ActiveSupport::Testing::Declarative
+
+ test "content is not trimmed without a trim mode" do
+ with_erb_trim_mode nil do
+ assert_equal(" \ntest", render(" <% 'IGNORED' %> \ntest"))
+ end
+ end
+
+ test "content around tags is trimmed if the trim mode includes a dash" do
+ with_erb_trim_mode '-' do
+ assert_equal("test", render(" <% 'IGNORED' %> \ntest"))
+ end
+ end
+
+ test "percent lines are normal content without a trim mode" do
+ with_erb_trim_mode nil do
+ assert_equal( "% if false\noops\n% end\n",
+ render("% if false\noops\n% end\n") )
+ end
+ end
+
+ test "percent lines count as ruby if trim mode includes a percent" do
+ with_erb_trim_mode "%" do
+ assert_equal("", render("% if false\noops\n% end\n"))
+ end
+ end
+
+ test "both trim modes can be used at the same time" do
+ with_erb_trim_mode "%-" do
+ assert_equal( "test", render( "% if false\noops\n% end\n" +
+ " <% 'IGNORED' %> \ntest" ) )
+ end
+ end
+
+ private
+
+ def with_erb_trim_mode(mode)
+ @old_erb_trim_mode = HANDLER.erb_trim_mode
+ HANDLER.erb_trim_mode = mode
+ yield
+ ensure
+ HANDLER.erb_trim_mode = @old_erb_trim_mode
+ end
+
+ def render(template)
+ eval("output_buffer = nil; " + HANDLER.call(Template.new(template)))
+ end
+end
Something went wrong with that request. Please try again.