Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8449173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:38:02+00:00 2026-06-10T10:38:02+00:00

Is it possible to render an Inversion template from a hash? And if not,

  • 0

Is it possible to render an Inversion template from a hash? And if not, how does one construct a complex template corresponding to a hash such as:

{ :a => [ { :b => 'foo', :c => 'bar' }, { :d => 'blah', :e => 'blubb'} ] }

Unfortunately the user guide doesn’t show such examples.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:38:03+00:00Added an answer on June 10, 2026 at 10:38 am

    It depends on how you want to render the hash. Assuming the code:

    require 'inversion'
    
    the_hash = { :a => [
        { :b => 'foo', :c => 'bar' },
        { :d => 'blah', :e => 'blubb'}
      ] }
    

    You can render it as-is, of course:

    <!-- prettyprint.tmpl -->
    <code><?pp the_hash ?></code>
    

    and then:

    tmpl = Inversion::Template.load( 'prettyprint.tmpl' )
    tmpl.the_hash = the_hash
    puts tmpl.render
    

    which will render as:

    <!-- prettyprint.tmpl -->
    <code>{:a=&gt;[{:b=&gt;"foo", :c=&gt;"bar"}, {:d=&gt;"blah", :e=&gt;"blubb"}]}</code>
    

    Assuming you want to do something more complex with the hash, like render some of its members:

    <!-- members.tmpl -->
    A's first B is: <?call the_hash[:a].first[:b] ?> 
    A's first C is: <?call the_hash[:a].first[:c] ?> 
    A's second D is: <?call the_hash[:a][1][:d] ?>   
    A's second E is: <?call the_hash[:a][1][:e] ?>   
    

    which (with the same code as the previous example except loading ‘members.tmpl’ instead) will render like so:

    <!-- members.tmpl -->
    A's first B is: foo 
    A's first C is: bar 
    A's second D is: blah   
    A's second E is: blubb   
    

    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:

    tmpl.users = User.all
    tmpl.company = "ACME Widgets, Pty"
    tmpl.render
    

    This also has the advantage of being more amenable to mocking:

    # (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" )
    

    This removes the template contents from the test entirely, and just focuses on what messages the controller should be sending to the view.

    There’s a fairly full-featured example in the manual, along with the code that renders it.

    The library (and its documentation) are fairly new, so thanks for bringing this up. I have a pending patch to merge the manual with the API docs, which should help, but in the meantime it’s probably the best place to find examples.

    Hope this helps. I’m happy to answer more in-depth questions personally (ged at FaerieMUD.org), or accept patches via pull request on Github or Bitbucket.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure if it is possible to render an asp.net control from a
Is it possible to render a view from a class, which is not controller?
Is it possible to render another page instead of one currently being processed? For
Is it possible to render a template into a string? I would like to
I am wondering does anyone know if it is possible to render html in
Is it possible to render fragments of Tilt templates (i.e., without using a template
Is it possible to render a Grid with one row selected as default (set
I know the mapkit does not support street view but it is possible to
Is it possible to render a partial from inside a ruby script or from
Is it possible to have rails render html into a hash instead of to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.