# Rendering Partials within a Radius Tag

**URL:** <https://www.ruby-forum.com/t/rendering-partials-within-a-radius-tag/122057>\
**Category:** Radiant CMS\
**Created:** [December 4, 2007, 7:48am UTC](https://www.ruby-forum.com/t/rendering-partials-within-a-radius-tag/122057 "2007-12-04T07:48:01Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [December 4, 2007, 7:48am UTC](https://www.ruby-forum.com/t/rendering-partials-within-a-radius-tag/122057/1 "2007-12-04T07:48:01Z")

</div>

In an extension I’m working on, I’d like to define some medium to large  
blocks of dynamic html to be inserted via a single radius tag. Because  
this html isn’t just a line or two and is tied to models and their data,  
I’d love to be able to create a true rails partial and tell the snippet  
to behave as a pseudo-controller and render the view in place of the  
radius tag.

I’m trying to keep a separation of concerns here and keep my code clean  
(and make use of all the html/erb sugar already built into rails).

I don’t suppose that there’s any way to have a tag call ‘render :partial  
=\> …’ much less get around the rails double-render when the  
SiteController tries to render. Clever thoughts anyone?

-Chris

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [January 17, 2008, 1:18pm UTC](https://www.ruby-forum.com/t/rendering-partials-within-a-radius-tag/122057/2 "2008-01-17T13:18:02Z")

</div>

Chris P. wrote:

> In an extension I’m working on, I’d like to define some medium to large  
> blocks of dynamic html to be inserted via a single radius tag. Because  
> this html isn’t just a line or two and is tied to models and their data,  
> I’d love to be able to create a true rails partial and tell the snippet  
> to behave as a pseudo-controller and render the view in place of the  
> radius tag.
> 
> I’m trying to keep a separation of concerns here and keep my code clean  
> (and make use of all the html/erb sugar already built into rails).
> 
> I don’t suppose that there’s any way to have a tag call ‘render :partial  
> =\> …’ much less get around the rails double-render when the  
> SiteController tries to render. Clever thoughts anyone?
> 
> -Chris

Hi Chris,

I’ve a simple example how you can do this:

Model:

require ‘radius’  
class Content \< ActiveRecord::Base  
def body  
context = Radius::Context.new do |c|  
c.define\_tag ‘render’ do |tag|  
content = “[render]=” + tag.attr[‘partial’] +  
“”  
end  
end  
parser = Radius::Parser.new(context, :tag\_prefix =\> ‘r’)  
return parser.parse(‘

hello \<r:render  
partial=“partials/google\_adsense” /\>

’)  
end  
end

View:

\<% @content.body.split("").each do |body| %\> \<% if body.include?("[render]=") %\> \<%= render :partial=\>body.split("[render]=")[1] %\> \<% else %\> \<%= markdown(body) %\> \<% end %\> \<% end %\>

Maybe i could help you

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [January 17, 2008, 2:43pm UTC](https://www.ruby-forum.com/t/rendering-partials-within-a-radius-tag/122057/3 "2008-01-17T14:43:42Z")

</div>

Hi Chris,

Now that Alexandro brings this up again, I have managed to add the  
ability to truly render a partial inside a tag, model or anywhere else  
you’d need to do it. It is, however, part of an extension with loads  
of other stuff in it, and it requires some ugly hacks. Amongst other  
things, Radiant has to be hacked to set the active controller, request  
and response on tags.globals, so they are available to initialize an  
ActiveView::Base instance to render a partial inside the tag.

If you are interested, I could provide the required code for in a  
pastie, but you might want to stick with Alexandros solution which  
seems less hacky, albeit not bullet proof. (How do you for instance  
pass data to the partial with this approach?)

Cheers,  
Casper F.

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [January 17, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/rendering-partials-within-a-radius-tag/122057/4 "2008-01-17T17:20:23Z")

</div>

@Alexlandros - when I get some free time in the next couple of days, I’m  
going to take a look at your solution. It’s not exactly the direction I  
need (I have tags like \<r:my\_tag name=“some\_name”\> which spits out a big  
block of html with data from my a model. But I’m not trying to let the  
user render a partial by name. But the foundation is still the same so  
I’d like to understand it better - maybe we’ll talk off the list.

@Casper - I’m not into hacks much but I would be interested in any  
suggestions/improvements. Also, I didn’t feel like I needed another  
controller since it would only be finding and creating a model to pass  
to the view (pretty simple). But that doesn’t mean being able to trigger  
another controller wouldn’t be useful (I’ve often wanted something sort  
of like the now deprecated Rails ‘components.’ They were bad practice  
there but in this environment where there’s a SiteController overseeing  
all, being able to chain a 2nd controller into the mix would be nice).  
If you’d be willing to email me a pastie directly I’d appreciate it.

Thanks both of you.

And, for the record, I came up with a solution that I was ok with (and  
light on hacks). Here’s what I did:

[In the Model]

tag “my\_tag” do |tag|  
@my\_tag\_data = MyModel.find\_by\_name(tag.attr[‘name’])  
parse\_template ‘path\_for/\_my\_template’  
end  
end

private

def parse\_template(filename)  
require ‘erb’  
template = ‘’  
File.open("#{MyExtensionNameExtension.root}/app/views/" + filename +  
‘.html.erb’, ‘r’) { |f|  
template = f.read  
}  
ERB.new(template).result(binding)  
end

[In  
/vendor/extensions/my\_extension\_name/app/views/path\_for/\_my\_template.html.erb]

# \<%= @my\_tag\_data.name -%\>

Value from my instance variable: \<%= @my\_tag\_data.value -%\>

So you can see that I chose to follow the Rails 2.0 template naming  
rules of ‘\*.html.erb’ (since John’s been making progress towards a 2.0  
capable Radiant- yay!). I used ERB with binding so I was able to pass  
instance variables to the template (something I needed).

This obviously isn’t full-on rails parsing so it doesn’t auto-mixin  
helpers or anything fancy like that (I suppose I could require  
ActiveView::Base or some such thing) but I didn’t need that much  
capability.

-Chris

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [November 21, 2008, 8:30pm UTC](https://www.ruby-forum.com/t/rendering-partials-within-a-radius-tag/122057/5 "2008-11-21T20:30:56Z")

</div>

Chris P.'s solution to rendering partials within a Radius tag works  
great. It’s been almost a year since he posted it though, and I was  
wondering if this functionality should be done a different way now –  
i.e. does Radiant has a built-in way to handle this now?

Thanks,

- Dave

Chris P. wrote:

> [In the Model]
> 
> tag “my\_tag” do |tag|  
> @my\_tag\_data = MyModel.find\_by\_name(tag.attr[‘name’])  
> parse\_template ‘path\_for/\_my\_template’  
> end  
> end
> 
> private
> 
> def parse\_template(filename)  
> require ‘erb’  
> template = ‘’  
> File.open("#{MyExtensionNameExtension.root}/app/views/" + filename +  
> ‘.html.erb’, ‘r’) { |f|  
> template = f.read  
> }  
> ERB.new(template).result(binding)  
> end
> 
> [In  
> /vendor/extensions/my\_extension\_name/app/views/path\_for/\_my\_template.html.erb]
> 
> # \<%= @my\_tag\_data.name -%\>
> 
> Value from my instance variable: \<%= @my\_tag\_data.value -%\>
> 
> This obviously isn’t full-on rails parsing so it doesn’t auto-mixin  
> helpers or anything fancy like that (I suppose I could require  
> ActiveView::Base or some such thing) but I didn’t need that much  
> capability.
> 
> -Chris

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [November 21, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/rendering-partials-within-a-radius-tag/122057/6 "2008-11-21T22:46:34Z")

</div>

David, I still use that code in my extensions today (though I’d be  
interested to hear if anyone out there has come up with something  
better).

Now that Haml is built into Radiant, it would also be safe to convert to  
using that for your templates (it might be even easier to do the binding  
bit but I haven’t tried yet).

-Chris

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [January 18, 2008, 9:55am UTC](https://www.ruby-forum.com/t/rendering-partials-within-a-radius-tag/122057/7 "2008-01-18T09:55:37Z")

</div>

Chris, what can I say, your solution is better and cleaner than mine.  
My solution lets the partial render in the context of the  
SiteController, with the exact same possibilites as when rendered the  
standard way, but I think I’ll switch to yours anyway and get rid of  
some my ugly hacks and overrides.

Thanks for providing your solution - I’d provide mine, if I thought it  
would actually be a help to anyone, but I don’t 😉

/Casper

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [November 24, 2008, 6:13pm UTC](https://www.ruby-forum.com/t/rendering-partials-within-a-radius-tag/122057/8 "2008-11-24T18:13:37Z")

</div>

Chris – any idea why I can’t use the h() method in my partials?

NoMethodError in SiteController#show\_page  
undefined method `h’ for #MyControllerName:0x3336ca8

- Dave

Chris P. wrote:

> David, I still use that code in my extensions today (though I’d be  
> interested to hear if anyone out there has come up with something  
> better).

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [November 22, 2008, 12:05am UTC](https://www.ruby-forum.com/t/rendering-partials-within-a-radius-tag/122057/9 "2008-11-22T00:05:43Z")

</div>

That pattern seems like a sufficiently good paradigm that some sort of  
convention should be created to do this. Perhaps

tag “my\_tag” do |tag|  
@my\_tag\_data = MyModel.find\_by\_name(tag.attr[‘name’])  
tag.render  
end  
end

> 

would automatically use view/tags/my\_tag.haml.html or something similar.

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [March 2, 2009, 1:51pm UTC](https://www.ruby-forum.com/t/rendering-partials-within-a-radius-tag/122057/10 "2009-03-02T13:51:51Z")

</div>

Chris P. wrote:

> David, I still use that code in my extensions today (though I’d be  
> interested to hear if anyone out there has come up with something  
> better).
> 
> Now that Haml is built into Radiant, it would also be safe to convert to  
> using that for your templates (it might be even easier to do the binding  
> bit but I haven’t tried yet).

Here’s a HAML version that works, up to a point. It’s not really much  
use, though: out of context like this you can render haml and  
interpolate variables but that’s about it. You can’t render another  
partial, for example, or call any helper methods. Perhaps someone more  
up on scopes and evaluation can make it work?

def parse\_template(filename, locals = {})  
require ‘haml/engine’  
haml\_engine =  
Haml::Engine.new(File.read("#{SomeExtension.root}/app/views/" + filename

- ‘.html.haml’))  
haml\_engine.to\_html(Object.new, locals)  
end

best,

will

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [August 17, 2010, 12:45am UTC](https://www.ruby-forum.com/t/rendering-partials-within-a-radius-tag/122057/11 "2010-08-17T00:45:08Z")

</div>

Is anyone aware of any progress that has been made in this area? I find  
myself needing to do the same thing.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [May 25, 2009, 1:31pm UTC](https://www.ruby-forum.com/t/rendering-partials-within-a-radius-tag/122057/12 "2009-05-25T13:31:38Z")

</div>

William Ross wrote:

> Here’s a HAML version that works, up to a point. It’s not really much  
> use, though: out of context like this you can render haml and  
> interpolate variables but that’s about it. You can’t render another  
> partial, for example, or call any helper methods. Perhaps someone more  
> up on scopes and evaluation can make it work?

There is another possibility, that allows using @variables and render  
:partials:

params = {:id =\> 15}

```
env = {
  'REQUEST_METHOD' => 'GET',
  'REQUEST_URI' => '/search/details',
  'action_controller.request.request_parameters' => params
}
r = SearchController.new
r.process(ActionController::Request.new(env), 

```

ActionController::Response.new, :details).body

and then implement search\_controller as usual, but it looks like you  
need to call render :action =\> ‘details’ explicitly.

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [October 5, 2010, 7:11pm UTC](https://www.ruby-forum.com/t/rendering-partials-within-a-radius-tag/122057/13 "2010-10-05T19:11:48Z")

</div>

I ended up using Cells ([GitHub - trailblazer/cells: View components for Ruby and Rails.](http://github.com/apotonick/cells)) to handle  
this.

In my xxx\_extension.rb, file I needed to do the following in order to  
allow the PageController to handle cells:

#Modify view paths for ::Cell::Base to include local cell view  
directories  
::Cell::Base.view\_paths += [CELL\_PATH, “#{CELL\_PATH}/layouts”]

#Add CELL\_PATH to Rails $LOAD\_PATH  
$LOAD\_PATH.unshift CELL\_PATH

#Require each Cell class  
CELL\_FILES.each {|f| require f}

and I added a custom “cell” tag to handle rendering of cells:

desc %{  
For use with projects that use the Cells gem  
([GitHub - trailblazer/cells: View components for Ruby and Rails.](http://github.com/apotonick/cells))

```
  Renders the cell specified in the @name@ attribute _outside_ of 

```

the context of a page. The cell name and view are specified in the  
appropriate attributes,  
and local data required for rendering the view may be provided via  
additional tags, which will be passed through to cell in its @opts hash.

```
  *Usage:*

  <pre><code><r:cell name="cell_name" view="view_name" 

```

data\_param\_1=“x” data\_param\_2=“y”/\>  
}  
tag “cell” do |tag|  
if tag.attr[‘name’] && tag.attr[‘view’]  
name, view = tag.attr.delete(‘name’), tag.attr.delete(‘view’)  
tag.locals.page.response.template.controller.render\_cell(name,  
view, tag.attr)  
else  
raise TagError.new(“`cell' tag must contain `name’ and `view’  
attributes”)  
end  
end
