[Radiant] Access to request object from within tags (answer?)

Kaleb Walton kalebwalton at gmail.com
Thu Jan 25 20:15:21 CST 2007


Well, I spoke 5 minutes too soon. I tried a few more things and found
out how to get it to work - now this may be completely wrong on a
number of levels but this got me the request within snippets that
previously had a nil request.

Does anyone know any ramifications of doing this? I haven't yet
checked if it will break caching... we'll see about that.

I notated the lines I changed or added with a <----

In page_context.rb

  def initialize(page, request)
    super()

    @page = page
    globals.request = request <----
    globals.page = @page

... ...

  define_tag 'snippet' do |tag|
    if name = tag.attr['name']
      if snippet = Snippet.find_by_name(name.strip)
        page = tag.locals.page
        page.behavior.request = tag.globals.request <----
        page.behavior.render_snippet(snippet)
      else
        raise TagError.new('snippet not found')
      end
    else
      raise TagError.new("`snippet' tag must contain `name' attribute")
    end
  end


behavior.rb

  def lazy_initialize_parser_and_context
    unless @context and @parser
      @context = PageContext.new(@page, @request) <----
      self.class.additional_tag_definition_blocks.each { |block|
instance_eval &block }
      add_tags_from_parent_to_context
      @parser = Radius::Parser.new(@context, :tag_prefix => 'r')
    end
  end



More information about the Radiant mailing list