using heredoc string in erb

Discussion in 'Ruby' started by jakemiles, Dec 4, 2009.

  1. jakemiles

    jakemiles Guest

    Hi. I'd like to use heredoc notation in an erb view (I'm using Merb,
    but I believe this is an erb issue, not specific to merb). For
    example, I wrote a helper function that lets me use syntax like that
    below. However, this example produces a parser error:

    can't find string "CODE" anywhere before EOF

    Any ideas how I can get this to work? I posted this on the merb group
    as well, but I suspect it's really an erb issue:

    <%= code "java", <<-CODE

    // create a panel using a GridBagLayout
    JPanel panel = new JPanel (new GridBagLayout());

    // three labels on the first row
    panel.add (new JLabel ("One"));
    panel.add (new JLabel ("Two"));
    panel.add (new JLabel ("Threeeee"));

    CODE %>

    If I can get this to work it would be a really cool technique for
    writing HTML helper functions.
     
    jakemiles, Dec 4, 2009
    #1
    1. Advertising

  2. jakemiles

    Wybo Dekker Guest

    Is this what you need?:

    #!/usr/bin/env ruby
    require 'erb'
    e = ERB.new(DATA.read)
    print e.result
    __END__
    <%= code = <<-CODE.chomp

    // create a panel using a GridBagLayout
    JPanel panel = new JPanel (new GridBagLayout());

    // three labels on the first row
    panel.add (new JLabel ("One"));
    panel.add (new JLabel ("Two"));
    panel.add (new JLabel ("Threeeee"));
    CODE
    %>


    jakemiles wrote:
    > Hi. I'd like to use heredoc notation in an erb view (I'm using Merb,
    > but I believe this is an erb issue, not specific to merb). For
    > example, I wrote a helper function that lets me use syntax like that
    > below. However, this example produces a parser error:
    >
    > can't find string "CODE" anywhere before EOF
    >
    > Any ideas how I can get this to work? I posted this on the merb group
    > as well, but I suspect it's really an erb issue:
    >
    > <%= code "java", <<-CODE
    >
    > // create a panel using a GridBagLayout
    > JPanel panel = new JPanel (new GridBagLayout());
    >
    > // three labels on the first row
    > panel.add (new JLabel ("One"));
    > panel.add (new JLabel ("Two"));
    > panel.add (new JLabel ("Threeeee"));
    >
    > CODE %>
    >
    > If I can get this to work it would be a really cool technique for
    > writing HTML helper functions.
    >


    --
    Wybo
     
    Wybo Dekker, Dec 4, 2009
    #2
    1. Advertising

  3. 2009/12/4 jakemiles <>:
    > Hi. =A0I'd like to use heredoc notation in an erb view (I'm using Merb,
    > but I believe this is an erb issue, not specific to merb). =A0For
    > example, I wrote a helper function that lets me use syntax like that
    > below. =A0However, this example produces a parser error:
    >
    > can't find string "CODE" anywhere before EOF
    >
    > Any ideas how I can get this to work? =A0I posted this on the merb group
    > as well, but I suspect it's really an erb issue:
    >
    > =A0<%=3D code "java", <<-CODE
    >
    > =A0 =A0 =A0 =A0// create a panel using a GridBagLayout
    > =A0 =A0 =A0 =A0JPanel panel =3D new JPanel (new GridBagLayout());
    >
    > =A0 =A0 =A0 =A0// three labels on the first row
    > =A0 =A0 =A0 =A0panel.add (new JLabel ("One"));
    > =A0 =A0 =A0 =A0panel.add (new JLabel ("Two"));
    > =A0 =A0 =A0 =A0panel.add (new JLabel ("Threeeee"));
    >
    > =A0CODE %>


    Make that
    CODE
    %>

    IOW, place the %> on the next line.

    > If I can get this to work it would be a really cool technique for
    > writing HTML helper functions.


    HTML helper functions in Java in an ERB template? How does this work?

    Cheers

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Dec 4, 2009
    #3
  4. jakemiles

    jakemiles Guest

    Thanks! The .chomp fixed it.

    No, the intention isn't to create helper functions in Java. The web
    page happens to be a tutorial on Java. The helper function is a ruby
    helper that appliers google's syntaxhighlighting javascript thing to
    the provided block of text.

    The resulting code in the view, for posterity, is:

    <%= code "java", <<-"CODE".chomp

    // create a panel using a GridBagLayout
    JPanel panel = new JPanel (new GridBagLayout());

    // three labels on the first row
    panel.add (new JLabel ("One"));
    panel.add (new JLabel ("Two"));
    panel.add (new JLabel ("Threeeee"));

    CODE
    %>

    And the helper function it calls is this:

    def code(lang, block)
    "<script type=\"syntaxhighlighter\" class=\"brush: #{lang}\"><!
    [CDATA[#{block}]]></script>"
    end

    This turns the provided block of code into this HTML:

    <script type="syntaxhighlighter" class="brush: java"><![CDATA[
    // create a panel using a GridBagLayout
    JPanel panel = new JPanel (new GridBagLayout());

    // three labels on the first row
    panel.add (new JLabel ("One"));
    panel.add (new JLabel ("Two"));
    panel.add (new JLabel ("Threeeee"));
    ]]></script>

    Which is the syntax for the syntaxhighlighter package that syntax-
    highlights the code in the page andmakes it look quite sharp. The
    helper will also automagically include the right javascript file for
    the language of the formatted code block.



    On Dec 4, 9:51 am, Robert Klemme <> wrote:
    > 2009/12/4 jakemiles <>:
    >
    >
    >
    > > Hi.  I'd like to use heredoc notation in an erb view (I'm using Merb,
    > > but I believe this is an erb issue, not specific to merb).  For
    > > example, I wrote a helper function that lets me use syntax like that
    > > below.  However, this example produces a parser error:

    >
    > > can't find string "CODE" anywhere before EOF

    >
    > > Any ideas how I can get this to work?  I posted this on the merb group
    > > as well, but I suspect it's really an erb issue:

    >
    > >  <%= code "java", <<-CODE

    >
    > >        // create a panel using a GridBagLayout
    > >        JPanel panel = new JPanel (new GridBagLayout());

    >
    > >        // three labels on the first row
    > >        panel.add (new JLabel ("One"));
    > >        panel.add (new JLabel ("Two"));
    > >        panel.add (new JLabel ("Threeeee"));

    >
    > >  CODE %>

    >
    > Make that
    > CODE
    > %>


    >


    > IOW, place the %> on the next line.
    >
    > > If I can get this to work it would be a really cool technique for
    > > writing HTML helper functions.

    >
    > HTML helper functions in Java in an ERB template?  How does this work?
    >
    > Cheers
    >
    > robert
    >
    > --
    > remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/
     
    jakemiles, Dec 4, 2009
    #4
  5. jakemiles wrote:
    > Thanks! The .chomp fixed it.
    >
    > No, the intention isn't to create helper functions in Java. The web
    > page happens to be a tutorial on Java. The helper function is a ruby
    > helper that appliers google's syntaxhighlighting javascript thing to
    > the provided block of text.
    >
    > The resulting code in the view, for posterity, is:
    >
    > <%= code "java", <<-"CODE".chomp
    >
    > // create a panel using a GridBagLayout
    > JPanel panel = new JPanel (new GridBagLayout());
    >
    > // three labels on the first row
    > panel.add (new JLabel ("One"));
    > panel.add (new JLabel ("Two"));
    > panel.add (new JLabel ("Threeeee"));
    >
    > CODE
    > %>
    >

    [...]

    Interesting. Something else you might want to consider: use Haml. It's
    got a number of advantages over ERb, but the relevant one here is that
    you could define a custom filter so you could do

    :java
    // your java code
    // goes here

    and not mess around with here documents.

    Best,
    -- 
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
     
    Marnen Laibow-Koser, Dec 4, 2009
    #5
  6. 2009/12/4 jakemiles <>:
    > Thanks! =A0The .chomp fixed it.


    For me moving the "%>" to the next line fixed it.

    > No, the intention isn't to create helper functions in Java. =A0The web
    > page happens to be a tutorial on Java. =A0The helper function is a ruby
    > helper that appliers google's syntaxhighlighting javascript thing to
    > the provided block of text.
    >
    > The resulting code in the view, for posterity, is:
    >
    > =A0<%=3D code "java", <<-"CODE".chomp
    >
    > =A0 =A0 =A0 =A0// create a panel using a GridBagLayout
    > =A0 =A0 =A0 =A0JPanel panel =3D new JPanel (new GridBagLayout());
    >
    > =A0 =A0 =A0 =A0// three labels on the first row
    > =A0 =A0 =A0 =A0panel.add (new JLabel ("One"));
    > =A0 =A0 =A0 =A0panel.add (new JLabel ("Two"));
    > =A0 =A0 =A0 =A0panel.add (new JLabel ("Threeeee"));
    >
    > CODE
    > %>
    >
    > And the helper function it calls is this:
    >
    > =A0def code(lang, block)
    > =A0 =A0"<script type=3D\"syntaxhighlighter\" class=3D\"brush: #{lang}\"><=

    !
    > [CDATA[#{block}]]></script>"
    > =A0end
    >
    > This turns the provided block of code into this HTML:
    >
    > =A0<script type=3D"syntaxhighlighter" class=3D"brush: java"><![CDATA[
    > =A0 =A0 =A0 =A0// create a panel using a GridBagLayout
    > =A0 =A0 =A0 =A0JPanel panel =3D new JPanel (new GridBagLayout());
    >
    > =A0 =A0 =A0 =A0// three labels on the first row
    > =A0 =A0 =A0 =A0panel.add (new JLabel ("One"));
    > =A0 =A0 =A0 =A0panel.add (new JLabel ("Two"));
    > =A0 =A0 =A0 =A0panel.add (new JLabel ("Threeeee"));
    > ]]></script>
    >
    > Which is the syntax for the syntaxhighlighter package that syntax-
    > highlights the code in the page andmakes it look quite sharp. =A0The
    > helper will also automagically include the right javascript file for
    > the language of the formatted code block.


    Erm, why do you need a helper function for this? Why not directly
    place this in the ERB template

    <script type=3D"syntaxhighlighter" class=3D"brush: java"><![CDATA[
    // create a panel using a GridBagLayout
    JPanel panel =3D new JPanel (new GridBagLayout());

    // three labels on the first row
    panel.add (new JLabel ("One"));
    panel.add (new JLabel ("Two"));
    panel.add (new JLabel ("Threeeee"));
    ]]></script>


    Or even do

    <%=3D CODE_INTRO_JAVA %>
    // create a panel using a GridBagLayout
    JPanel panel =3D new JPanel (new GridBagLayout());

    // three labels on the first row
    panel.add (new JLabel ("One"));
    panel.add (new JLabel ("Two"));
    panel.add (new JLabel ("Threeeee"));
    <%=3D CODE_END %>

    with proper definitions of both constants? What am I missing?

    Kind regards

    robert


    PS: Please do not top post.


    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Dec 4, 2009
    #6
  7. jakemiles

    jakemiles Guest

    On 4 Dez., 12:27, Robert Klemme <> wrote:
    > 2009/12/4 jakemiles <>:
    >
    > > Thanks!  The .chomp fixed it.

    >
    > For me moving the "%>" to the next line fixed it.
    >
    >
    >
    > > No, the intention isn't to create helper functions in Java.  The web
    > > page happens to be a tutorial on Java.  The helper function is a ruby
    > > helper that appliers google's syntaxhighlighting javascript thing to
    > > the provided block of text.

    >
    > > The resulting code in the view, for posterity, is:

    >
    > >  <%= code "java", <<-"CODE".chomp

    >
    > >        // create a panel using a GridBagLayout
    > >        JPanel panel = new JPanel (new GridBagLayout());

    >
    > >        // three labels on the first row
    > >        panel.add (new JLabel ("One"));
    > >        panel.add (new JLabel ("Two"));
    > >        panel.add (new JLabel ("Threeeee"));

    >
    > > CODE
    > > %>

    >
    > > And the helper function it calls is this:

    >
    > >  def code(lang, block)
    > >    "<script type=\"syntaxhighlighter\" class=\"brush: #{lang}\"><!
    > > [CDATA[#{block}]]></script>"
    > >  end

    >
    > > This turns the provided block of code into this HTML:

    >
    > >  <script type="syntaxhighlighter" class="brush: java"><![CDATA[
    > >        // create a panel using a GridBagLayout
    > >        JPanel panel = new JPanel (new GridBagLayout());

    >
    > >        // three labels on the first row
    > >        panel.add (new JLabel ("One"));
    > >        panel.add (new JLabel ("Two"));
    > >        panel.add (new JLabel ("Threeeee"));
    > > ]]></script>

    >
    > > Which is the syntax for the syntaxhighlighter package that syntax-
    > > highlights the code in the page andmakes it look quite sharp.  The
    > > helper will also automagically include the right javascript file for
    > > the language of the formatted code block.

    >
    > Erm, why do you need a helper function for this? Why not directly
    > place this in the ERB template
    >
    >  <script type="syntaxhighlighter" class="brush: java"><![CDATA[
    >        // create a panel using a GridBagLayout
    >        JPanel panel = new JPanel (new GridBagLayout());
    >
    >        // three labels on the first row
    >        panel.add (new JLabel ("One"));
    >        panel.add (new JLabel ("Two"));
    >        panel.add (new JLabel ("Threeeee"));
    > ]]></script>
    >
    > Or even do
    >
    > <%= CODE_INTRO_JAVA %>
    >        // create a panel using a GridBagLayout
    >        JPanel panel = new JPanel (new GridBagLayout());
    >
    >        // three labels on the first row
    >        panel.add (new JLabel ("One"));
    >        panel.add (new JLabel ("Two"));
    >        panel.add (new JLabel ("Threeeee"));
    > <%= CODE_END %>
    >
    > with proper definitions of both constants?  What am I missing?
    >
    > Kind regards
    >
    > robert
    >
    > PS: Please do not top post.
    >
    > --
    > remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/


    Ah - thanks for all the suggestions.

    1) you're right, it was moving the %> to its own line that did it.
    The chomp isn't necessary.

    2) I'll look at the HAML approach, which seems great.

    3) I didn't think of using a constant, but I wouldn't. I also always
    opt for functional abstraction over a variable or constant, in case I
    want to extend the behavior later without having to change all the
    instances of usage.

    For example, I did end up extending the functionality. It now takes a
    hash of options that it passes to syntaxhighlighter, and other options
    that do my own custom stuff and wrap additional html around it.

    The resulting code, much cleaner than the full syntaxhighlighter html,
    is this:

    <%= code :brush => "java",
    :code => <<-CODE

    // create a panel using a GridBagLayout
    JPanel panel = new JPanel (new GridBagLayout());

    // three labels on the first row
    panel.add (new JLabel ("One"));
    panel.add (new JLabel ("Two"));
    panel.add (new JLabel ("Threeeee"));

    CODE
    %>

    And I was able to easily extend it with an option that places an image
    floating to the right of the code (and changes the css class of the
    syntaxhighlighter thing itself so it doesn't fill 100% of the page
    width):

    <%= code :brush => "java",
    :image => "http://jakemiles.smugmug.com/photos/
    702110106_Wtyxg-S.jpg",
    :code => <<-CODE

    // create a panel using a GridBagLayout
    JPanel panel = new JPanel (new GridBagLayout());

    // three labels on the first row
    panel.add (new JLabel ("One"));
    panel.add (new JLabel ("Two"));
    panel.add (new JLabel ("Threeeee"));

    CODE
    %>
     
    jakemiles, Dec 5, 2009
    #7
  8. I wonder if you could do something along the lines of Rails' capture
    helper? Then you could write

    <%= code :brush=>"java" do %>
    Java code goes here
    <% end %>

    The Java code would still be subject to erb expansion though.

    If you're using ERb outside of Rails, you should just be able to copy
    what Rails does. See
    http://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/capture_helper.rb

    # The capture method allows you to extract part of a template into
    a
    # variable. You can then use this variable anywhere in your
    templates or layout.
    #
    # ==== Examples
    # The capture method can be used in ERb templates...
    #
    # <% @greeting = capture do %>
    # Welcome to my shiny new web page! The date and time is
    # <%= Time.now %>
    # <% end %>
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Dec 6, 2009
    #8
  9. Brian Candler wrote:
    > I wonder if you could do something along the lines of Rails' capture
    > helper? Then you could write
    >
    > <%= code :brush=>"java" do %>
    > Java code goes here
    > <% end %>


    Incidentally, it looks like this is what webby does with its coderay and
    uv filters.
    http://webby.rubyforge.org/reference/
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Dec 9, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. flupke

    heredoc and variables

    flupke, Jun 4, 2004, in forum: Python
    Replies:
    7
    Views:
    3,365
    =?ISO-8859-1?Q?Xavier_Mart=EDnez?=
    Jun 7, 2004
  2. Paul Vudmaska

    lil problem w/heredoc

    Paul Vudmaska, Apr 7, 2004, in forum: Ruby
    Replies:
    2
    Views:
    142
    Paul Vudmaska
    Apr 7, 2004
  3. David Tran
    Replies:
    6
    Views:
    831
    Eric Hodel
    Mar 17, 2005
  4. Steve V
    Replies:
    6
    Views:
    256
    Steve V
    Apr 20, 2005
  5. Dan Thomas

    link to .erb file, from .erb file

    Dan Thomas, Jan 26, 2011, in forum: Ruby
    Replies:
    4
    Views:
    184
    Dan Thomas
    Jan 26, 2011
Loading...

Share This Page