The trick here is that you don't want the objects included in the
context from which you're calling Haml::Engine#render... you want them
in the context in which the template is being rendered. Handily, the
render method takes an object to use as the scope as the first argument.
So what you want to do is
base = ActiveRecord::Base.new('path/to/views', {}, controller)
Haml::Engine.new(text).render(base)
And that should work just peachily.
- Nathan
[EMAIL PROTECTED] wrote:
> Well, not exactly. Observe:
>
>
>>> Haml::Engine.new("%h1 header").render
>>>
> => "<h1>header</h1>\n"
>
> Awesome, now how about
>
>
>>> Haml::Engine.new("=link_to 'link', ''").render
>>>
> NoMethodError: undefined method `link_to' for #<Object:0x25417b4>
>
> Well crap. Fortunately I can do this:
>
>
>>> include ActionView::Helpers::UrlHelper
>>>
> => Object
>
>>> Haml::Engine.new("=link_to 'link', ''").render
>>>
> => "<a href=\"\">link</a>\n"
>
> Great! Now I can load any helper I want. Super. Let's try this:
>
>
>>> include ActionView::Helpers::AssetTagHelper
>>>
> => Object
>
>>> include ActionView::Helpers::TagHelper
>>>
> => Object
>
>>> Haml::Engine.new("=image_tag 'butts.jpg'").render
>>>
> NoMethodError: You have a nil object when you didn't expect it!
> The error occurred while evaluating nil.request
> from (haml):1
> from ./script/../config/../config/../vendor/rails/actionpack/
> lib/action_view/helpers/asset_tag_helper.rb:207:in
> `compute_public_path'
>
> So that helper is using @controller variable that i don't have
> instantiated. What do I do about that? I ended up commenting that line
> out. (I know, bad. But it didn't break anything)
>
> Now I have one thing to take care of. I need to be able to use my
> ApplicationHelper... So
>
>
>>> include ApplicationHelper
>>>
> => Object
>
>>> Haml::Engine.new("= expedition_dates_table(8)").render
>>>
> NoMethodError: undefined method `expedition_dates_table' for #<Object:
> 0x24bc8c0>
>
> Well shit, Carl! Shit!
>
> Last thing I need is to have partials working. Like
>
>
>>> Haml::Engine.new("= render :partial => 'main/hallo'").render
>>>
> NoMethodError: undefined method `render' for #<Object:0x2568634>
>
> Well, that's even worse as render is part of ActionController::Base
> and that's not something I can include.
>
> Obviously I'm missing something. I normally never get so deep into
> Rails' guts. But this time I kinda need to. Can somebody be so kind to
> push me in the right direction. Greatly appreciated.
>
>
> >
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Haml" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/haml?hl=en
-~----------~----~----~----~------~----~------~--~---