<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>rel=me &#187; erb</title>
	<atom:link href="http://rel.me/t/erb/feed/" rel="self" type="application/rss+xml" />
	<link>http://rel.me</link>
	<description>programming, objective-c, cocoa, iphone, c</description>
	<lastBuildDate>Wed, 01 Feb 2012 07:26:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Markup, CSS and Helper cataloging (Part deux)</title>
		<link>http://rel.me/2007/06/25/markup-css-and-helper-cataloging-part-deux/</link>
		<comments>http://rel.me/2007/06/25/markup-css-and-helper-cataloging-part-deux/#comments</comments>
		<pubDate>Mon, 25 Jun 2007 15:37:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[erb]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">/2007/09/03/markup-css-and-helper-cataloging-part-deux</guid>
		<description><![CDATA[I should be using ERB instead of eval. But because you can&#8217;t use &#60;% inside a string block in your view (that I can figure out; &#60;&#60;EOF didn&#8217;t seem to be working in a template). And if you don&#8217;t want to specify the code string in your controller you can use [% ... %]. Here [...]]]></description>
			<content:encoded><![CDATA[<p>I should be using ERB instead of eval. But because you can&#8217;t use &lt;% inside a string block in your view (that I can figure out; &lt;&lt;EOF didn&#8217;t seem to be working in a template). And if you don&#8217;t want to specify the code string in your controller you can use [% ...  %]. Here is the source I am using for our code helper now:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt>13<tt>
</tt>14<tt>
</tt>15<tt>
</tt>16<tt>
</tt>17<tt>
</tt>18<tt>
</tt>19<tt>
</tt><strong>20</strong><tt>
</tt>21<tt>
</tt>22<tt>
</tt>23<tt>
</tt>24<tt>
</tt>25<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  <span class="r">def</span> <span class="fu">erb_code</span>(code) <tt>
</tt>    <span class="co">ERB</span>.new(code).result(binding)    <tt>
</tt>  <span class="r">end</span><tt>
</tt>  <tt>
</tt>  <span class="r">def</span> <span class="fu">render_code</span>(code, options = {}) <tt>
</tt>    %{ &lt;pre <span class="r">class</span>=<span class="s"><span class="dl">&quot;</span><span class="k">helper</span><span class="dl">&quot;</span></span>&gt;<span class="er">\</span>n&lt;code <span class="r">class</span>=<span class="s"><span class="dl">&quot;</span><span class="k">ruby</span><span class="dl">&quot;</span></span>&gt;<span class="er">\</span>n<span class="c">#{h(code)}\n&lt;/code&gt;\n&lt;/pre&gt; }</span><tt>
</tt>  <span class="r">end</span><tt>
</tt>  <tt>
</tt>  <span class="r">def</span> <span class="fu">render_markup</span>(html)<tt>
</tt>    doc = <span class="co">REXML</span>::<span class="co">Document</span>.new(html)<tt>
</tt>    markup = <span class="s"><span class="dl">&quot;</span><span class="dl">&quot;</span></span><tt>
</tt>    doc.write(markup, <span class="i">2</span>)<tt>
</tt>    <tt>
</tt>    <span class="s"><span class="dl">%{</span><span class="k"> &lt;pre class=&quot;markup&quot;&gt;</span><span class="ch">\n</span><span class="k">&lt;code class=&quot;html&quot;&gt;</span><span class="ch">\n</span><span class="il"><span class="idl">#{</span>h(markup)<span class="idl">}</span></span><span class="ch">\n</span><span class="k">&lt;/code&gt;</span><span class="ch">\n</span><span class="k">&lt;/pre&gt;</span><span class="ch">\n</span><span class="k"> </span><span class="dl">}</span></span>    <tt>
</tt>  <span class="r">end</span><tt>
</tt>  <tt>
</tt>  <span class="r">def</span> <span class="fu">render_eval</span>(html)<tt>
</tt>    <span class="s"><span class="dl">%{</span><span class="k"> &lt;div class=&quot;eval&quot;&gt;</span><span class="il"><span class="idl">#{</span>html<span class="idl">}</span></span><span class="k">&lt;/div&gt; </span><span class="dl">}</span></span><tt>
</tt>  <span class="r">end</span><tt>
</tt>    <tt>
</tt>  <span class="r">def</span> <span class="fu">code_helper</span>(code)<tt>
</tt>    code.gsub!(<span class="rx"><span class="dl">/</span><span class="ch">\[</span><span class="k">%</span><span class="dl">/</span></span>, <span class="s"><span class="dl">&quot;</span><span class="k">&lt;%</span><span class="dl">&quot;</span></span>).gsub!(<span class="rx"><span class="dl">/</span><span class="k">%</span><span class="ch">\]</span><span class="dl">/</span></span>, <span class="s"><span class="dl">&quot;</span><span class="k">%&gt;</span><span class="dl">&quot;</span></span>)    <tt>
</tt>    html = erb_code(code)<tt>
</tt>    render_code(code) + render_markup(html) + render_eval(html)<tt>
</tt>  <span class="r">end</span></pre>
</td>
</tr>
</table>
<p>Then you can do things like:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  &lt;<span class="s"><span class="dl">%=</span><span class="k"> code_helper %{ [%</span><span class="dl">=</span></span> rating_field(<span class="sy">:rating</span>, <span class="sy">:effectiveness</span>) %] } %&gt;</pre>
</td>
</tr>
</table>
<p>and you could also dump it in a helper:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="r">def</span> <span class="fu">code_rating_form_field</span> <tt>
</tt>  &lt;&lt;-<span class="co">EOF</span><tt>
</tt>   &lt;<span class="s"><span class="dl">% </span><span class="k">form_for</span><span class="dl"> </span></span><span class="sy">:rating</span>, <span class="iv">@rating2</span> <span class="r">do</span> |f| <span class="s"><span class="dl">%&gt;</span><span class="k"><tt>
</tt>     &lt;%= f.rating_field(:effectiveness) %</span><span class="dl">&gt;</span></span><tt>
</tt>   &lt;<span class="s"><span class="dl">% </span><span class="k">end</span><span class="dl"> </span></span>%&gt;<tt>
</tt>  <span class="co">EOF</span><tt>
</tt><span class="r">end</span><tt>
</tt><tt>
</tt>  <span class="rx"><span class="dl">/</span><span class="dl">/</span></span> <span class="co">And</span> <span class="r">in</span> your view: &lt;<span class="s"><span class="dl">%=</span><span class="k"> code_helper(code_rating_form_field) %&gt;</span></span></pre>
</td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://rel.me/2007/06/25/markup-css-and-helper-cataloging-part-deux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

