-
Notifications
You must be signed in to change notification settings - Fork 3
/
defining-methods-for-erb-templates.html
26 lines (26 loc) · 4.13 KB
/
defining-methods-for-erb-templates.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE html><html><head><meta charset="utf-8" /><title>Defining methods for ERB templates - The Terminal Programmer</title><meta content="2006-11-12T00:00:00-08:00" name="DCTERMS.created" /><meta content="2010-04-26T00:00:00-07:00" name="DCTERMS.modified" /><meta content="Suraj N. Kurapati" name="author" /><meta content="ruby, template, embedding" name="keywords" /><meta content="width=device-width, initial-scale=1" name="viewport" /><meta content="Readably https://github.com/sunaku/readably" name="generator" /><link href="style.css" rel="stylesheet" type="text/css" /><link href="index.atom" rel="alternate" title="feed" type="application/atom+xml" /><script src="js/jquery.slim.min.js"></script></head><body><article data-entry-id="defining-methods-for-erb-templates" id="body"><header><div class="navigation"><a class="rootlink" href="index.html#defining-methods-for-erb-templates" title="The Terminal Programmer"><span>The Terminal Programmer</span></a></div><h1 class="title">Defining methods for ERB templates</h1><div class="author">Suraj N. Kurapati</div><time class="date" datetime="2006-11-12T00:00:00-08:00">12 November 2006</time><br /><time class="date" datetime="2010-04-26T00:00:00-07:00"><a href="#updates" title="1 update">26 April 2010</a></time></header><hr /><div class="description"></div><div class="content"><p>While converting <a href="http://ruby-vpi.rubyforge.org/doc/">ruby-vpi documentation</a> from DocBook-XML to RedCloth, I
wanted an easy way to pass a bunch of text to my own methods, in a fashion
similar to if-statements and iterators. This would allow me to make
admonitions easily by writing code like the following in my ERB template.</p>
<div class="highlight"><pre class="highlight erb"><code><span class="cp"><%</span> <span class="n">caution</span> <span class="k">do</span> <span class="cp">%></span>
Nothing is as it seems!
<span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span>
</code></pre></div><p>At first glance, it seems that the content between the <code>do</code> and <code>end</code>
keywords is just passed as a string inside the block argument to the
method. However, in truth, the block argument contains an expression that
appends the content between <code>do</code> and <code>end</code> to the <code>_erbout</code> variable,
like this:</p>
<div class="highlight"><pre class="highlight ruby"><code><span class="n">_erbout</span> <span class="o"><<</span> <span class="s2">"Nothing is as it seems!"</span>
</code></pre></div><p>ERB allows us to specify an alternate name for <code>_erbout</code> (its evaluation
buffer) in its constructor. We can use this ability to capture the content
passed from an ERB template to a Ruby method as follows.</p>
<p>TODO: finish this article!</p>
</div><hr /><h1 id="updates">Updates<a class="permalink" href="#updates" title="Permalink"></a></h1><aside class="update"><dl><dt class="title"><time datetime="2010-04-26T00:00:00-07:00">26 April 2010: </time></dt><dd class="content"><p>I finally solved this problem in a portable manner by injecting <a href="guiltless-monkeypatching-with-uuid.html">a special
backdoor variable</a> that keeps track
of the name of the eRuby template evaluation result variable’s name in <a href="http://snk.tuxfamily.org/lib/ember/">Ember
0.3.0</a>.</p>
</dd></dl></aside><div class="comments" id="comments"><script>var disqus_container_id = 'comments';
var disqus_title = "Defining methods for ERB templates";
var disqus_url = "https://sunaku.github.io/defining-methods-for-erb-templates.html";</script><script async="" src="https://theterminalprogrammer.disqus.com/embed.js"></script></div><hr /><footer><p class="copyright">© 2006–2010 Suraj N. Kurapati</p><p class="credits"><a href="https://github.com/sunaku/readably">Readably</a> written, <a href="https://github.com/sainnhe/everforest">Everforest</a> colored. </p><p>Like my work? 👍 Please <a href="vegan-for-life.html">spare a life</a> today as
thanks! 🐮🐷🐔🐟🙊✌💞</p>
</footer><!--[if lt IE 9]><script src="js/html5shiv.min.js"></script><script src="js/html5shiv-printshiv.min.js"></script><![endif]--><script src="index.js"></script></article></body></html>