@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]

<h1><%= @my_tag_data.name -%></h1>
<p>Value from my instance variable: <%= @my_tag_data.value -%></p>


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


Casper Fabricius wrote:
> 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 Fabricius
>
> On 17/01/2008, at 13:18, Alexandro Bernardi wrote:
>
>   
>> Chris Parrish 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 = "<!--partial-->[render]=" + tag.attr['partial'] +
>> "<!--partial-->"
>>    end
>>    end
>>    parser = Radius::Parser.new(context, :tag_prefix => 'r')
>>    return parser.parse('<p>hello <r:render
>> partial="partials/google_adsense" /></p>')
>> end
>> end
>>
>> View:
>>
>> <div class="body">
>> <% @content.body.split("<!--partial-->").each do |body| %>
>>  <% if body.include?("[render]=") %>
>>  <%= render :partial=>body.split("[render]=")[1] %>
>>  <% else %>
>>  <%= markdown(body) %>
>>  <% end %>
>> <% end %>
>> </div>
>>
>>
>> Maybe i could help you
>> -- 
>> Posted via http://www.ruby-forum.com/.
>> _______________________________________________
>> Radiant mailing list
>> Post:   [email protected]
>> Search: http://radiantcms.org/mailing-list/search/
>> Site:   http://lists.radiantcms.org/mailman/listinfo/radiant
>>     
>
> _______________________________________________
> Radiant mailing list
> Post:   [email protected]
> Search: http://radiantcms.org/mailing-list/search/
> Site:   http://lists.radiantcms.org/mailman/listinfo/radiant
>   

_______________________________________________
Radiant mailing list
Post:   [email protected]
Search: http://radiantcms.org/mailing-list/search/
Site:   http://lists.radiantcms.org/mailman/listinfo/radiant

Reply via email to