Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It depends on how you want to render the hash. Assuming the code:</p> <pre><code>require 'inversion' the_hash = { :a =&gt; [ { :b =&gt; 'foo', :c =&gt; 'bar' }, { :d =&gt; 'blah', :e =&gt; 'blubb'} ] } </code></pre> <p>You can render it as-is, of course:</p> <pre><code>&lt;!-- prettyprint.tmpl --&gt; &lt;code&gt;&lt;?pp the_hash ?&gt;&lt;/code&gt; </code></pre> <p>and then:</p> <pre><code>tmpl = Inversion::Template.load( 'prettyprint.tmpl' ) tmpl.the_hash = the_hash puts tmpl.render </code></pre> <p>which will render as:</p> <pre><code>&lt;!-- prettyprint.tmpl --&gt; &lt;code&gt;{:a=&amp;gt;[{:b=&amp;gt;"foo", :c=&amp;gt;"bar"}, {:d=&amp;gt;"blah", :e=&amp;gt;"blubb"}]}&lt;/code&gt; </code></pre> <p>Assuming you want to do something more complex with the hash, like render some of its members:</p> <pre><code>&lt;!-- members.tmpl --&gt; A's first B is: &lt;?call the_hash[:a].first[:b] ?&gt; A's first C is: &lt;?call the_hash[:a].first[:c] ?&gt; A's second D is: &lt;?call the_hash[:a][1][:d] ?&gt; A's second E is: &lt;?call the_hash[:a][1][:e] ?&gt; </code></pre> <p>which (with the same code as the previous example except loading 'members.tmpl' instead) will render like so:</p> <pre><code>&lt;!-- members.tmpl --&gt; A's first B is: foo A's first C is: bar A's second D is: blah A's second E is: blubb </code></pre> <p>But building a big complex data structure like your hash, and then later merging it with a template is not really how Inversion is intended to be used. The idea is really that loading the template gives you a Ruby object with an API, suitable for passing around and incrementally adding stuff to before rendering it. Instead of building a hash, just pass the template object itself around and call accessors on it, then let it pull out what it needs to build the view:</p> <pre><code>tmpl.users = User.all tmpl.company = "ACME Widgets, Pty" tmpl.render </code></pre> <p>This also has the advantage of being more amenable to mocking:</p> <pre><code># (rspec) tmpl = mock( "members template" ) tmpl.should_receive( :users ).with( User.all ) tmpl.should_receive( :company ).with( company_name ) tmpl.should_receive( :render ).and_return( "the rendered stuff" ) </code></pre> <p>This removes the template contents from the test entirely, and just focuses on what messages the controller should be sending to the view.</p> <p>There's a fairly full-featured example <a href="http://deveiate.org/code/Inversion-manual/examples.html" rel="nofollow">in the manual</a>, along with <a href="https://github.com/ged/inversion/blob/master/experiments/demo.rb" rel="nofollow">the code that renders it</a>. </p> <p>The library (and its documentation) are fairly new, so thanks for bringing this up. I have a pending patch to merge <a href="http://deveiate.org/code/Inversion-manual/" rel="nofollow">the manual</a> with the API docs, which should help, but in the meantime it's probably the best place to find examples.</p> <p>Hope this helps. I'm happy to answer more in-depth questions personally (ged at FaerieMUD.org), or accept patches via pull request on <a href="https://github.com/ged/inversion" rel="nofollow">Github</a> or <a href="https://bitbucket.org/ged/inversion" rel="nofollow">Bitbucket</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload