Parse and render radius within a tag

Hello,

First of all, let me say that I’m liking the extension system. It seems
to work pretty well for adding little bits of functionality. One of the
extensions that I’ve been working on is an events calendar. My event
table has a “body” field where the user can type a long description
about the event. I would like for them to be able to use some radius
tags within that field. In other words, within my definition of the
“event:body” tag I would like to be able to parse the text for any
radius tags and have them rendered appropriately. Is there an easy way
to get at Radiant’s radius context and parser from within a tag
definition? Or do I need to instantiate my own radius context and
parser? If I have to instantiate my own, is there an easy way for me to
use tags that have already defined in radiant? Sorry if all of this is
confusing. Just curious if anybody else has run into something similar.

Steve

I don’t understand well what you mean. I believe that you want
something like:

<event>
     <date date="..." />
     <title> .... </title>
     <body>
           .....
     </body>
 </event>

 And be able to "parse" the sub-tags an do something useful with 

that.

 I don't believe that it's easy to do your own parsing with Radius,

maybe it’s not even possible. But that does not matter. IMHO, the way
to
handle this is to expand the tag, which will expand the
sub-tags.
In the sub-tags you bind data to the tag, so when the expanding
ends
at the tag it has all the information and can render whatever
HTML
you want and return it.

 The point here is to realize that expanding a tag does not mean 

that
you have to return the HTML you get back, you can return whatever you
want.
In the case explained above, the sub-tags will not even return any HTML,
just add data to the parent tag.

I found this doc nice for understanding the Radius tags from a 

developer
point of view:

http://radius.rubyforge.org/files/QUICKSTART.html

 /AITOR

Thanks Aitor. I probably wasn’t very clear in my first post. Sorry
about that. Let me see if I can explain it again.

I already have <r:event></r:event>, <r:event:date format="" />,
<r:event:title />, <r:event:body />. The problem is that the text in my
body tag also contains radius content. For example, I wanted to define
a radius tag for creating links, or creating an inline google map or
some simple formatting. In order to let my users be able to use radius
tags in the “body” field of my event in a similar manner as the body
field of a page, I need to have some way of parsing the body text. One
of the things I was considering doing was just creating my own radius
context and parser. The body tag definition would then look something
like the following (psuedo-code)

tag “event:body” do |tag|
context = Radius::Context.new()
parser = Radius::Parser(context, :tag_prefix => ‘r’)
#some code to define tags
parser.parse(tag.locals.event.body)
end

I was trying to avoid creating my own context and parser and instead use
the already existing one that Radiant uses. that would I wouldn’t have
to figure out how to redefine all of the existing page tags, but maybe
I’ll just end up going down that route.

Steve

In order to let my users be able to use radius
tags in the “body” field of my event in a similar manner as the
body
field of a page, I need to have some way of parsing the body text.

 You don't need to do any parsing.  If you expand the <r:event:body>

tag, Radius will take care of rendering whatever is inside, including
any
other tag. Then you get the result in the <r:event> tag and do whatever
you
want.

 This will not work if you want some kind of fine control on how the

tags inside <r:event:body> expand, but as i understand this is not what
you
are looking for.

 Take a look to the code of the <children:each> tag for an example 

of
expanding the tag and accumulating the result:

http://dev.radiantcms.org/radiant/browser/trunk/radiant/app/models/standard_tags.rb#L88

 <random> is also nice simple example:

http://dev.radiantcms.org/radiant/browser/trunk/radiant/app/models/standard_tags.rb#L431

  /AITOR

OK, now i see the problem, the data is in the database.

In this case the code you posted before is the way to go.  But 

instead
of creating a new context i would suggest using the context you
inherited
from the tag.

The API docs say that the context is "tag.context", but also notes 

“The
Context http://radius.rubyforge.org/classes/Radius/Context.html that
the
TagBinding http://radius.rubyforge.org/classes/Radius/TagBinding.html
is
associated with. Used internally. Try not to use this object directly.”

 /AITOR

Aitor,

I believe what you’re suggesting would work if the body text was
actually stored on the Page. The problem is that the body text for the
event isn’t stored on the page. It is stored in the body field of the
event table. As a result, when that text gets included in a page (via
the <r:event:body /> tag), the radius tags used in the text event.body
field will simply be treated as plain text.

Steve

I just thought I would report back and say that this ended up working
great. If anybody else needs to do this here’s the code I’m using

tag “event:body” do |tag|
parser = Radius::Parser.new(tag.context, :tag_prefix => ‘r’)
parser.parse(tag.locals.event.body)
end

Thanks aitor for your help.

Perfect. tag.context That’s exactly what I was looking for. I’ll try
it when I get home tonight and see if it works. Thanks for the help.

Steve

Steve E. wrote:

I just thought I would report back and say that this ended up working
great. If anybody else needs to do this here’s the code I’m using

tag “event:body” do |tag|
parser = Radius::Parser.new(tag.context, :tag_prefix => ‘r’)
parser.parse(tag.locals.event.body)
end

Thanks aitor for your help.

You might also be able to use Page#render_snippet in a way that is
similar to the snippet tag:

http://dev.radiantcms.org/radiant/browser/trunk/radiant/app/models/standard_tags.rb#L389


John L.
http://wiseheartdesign.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs