[Radiant] Problem with tag.locals

Adam Salter adam at rokslide.com
Wed Apr 4 06:05:12 CDT 2007


I think I've worked it out...

Basically tag.locals is a "virtual" (don't know correct name) method...
so if you want tag.locals.item.name you should first assign  
tag.locals.item to a variable and then query that variable:
i.e.
item = tag.locals.item
return item.name

you can assign with tag.locals.item =, but not tag.locals.item.name =  
for example.

brackets might work like:
(tag.locals.item).name

haven't tested that.

Cheers,
-Adam

On 04/04/2007, at 7:36 PM, Adam Salter wrote:

> Keith,
>
> This didn't work.
>
> I'm going to work around it by doing:
> <r:anothertag id='1' />
> <r:yetanothertag id='1' />
>
> for the moment... but it's not going to work in the long run... is  
> there a way to force/pass or create your own variables?
>
> e.g
> tag "mytag" do |tag|
>    if tag.attr["id"]
>      tag.locals.gallery = Gallery.find_by_id tag.attr["id"]
>    tag.expand { "id" => "1"}
> end
>
> ...or...
>
> tag "mytag" do |tag|
>    if tag.attr["id"]
>      tag.locals.my_gallery = Gallery.find_by_id tag.attr["id"]
>    tag.expand
> end
>
> ...or...
>
> tag "mytag" do |tag|
>    if tag.attr["id"]
>      tag.globals.gallery = Gallery.find_by_id tag.attr["id"]
>    tag.expand
> end
>
> Thanks in advance,
> -Adam
>
> On 04/04/2007, at 5:11 PM, Keith Bingman wrote:
>
>> You need to call the id in both tags. I know it is a little  
>> counterintuitive, but the id tag is not being passed to the first  
>> tag, just the second. I banged my head on this for a few days, try  
>> it like this:
>>
>> tag "mytag" do |tag|
>>    if tag.attr["id"]
>>      tag.locals.gallery = Gallery.find_by_id tag.attr["id"]
>>    tag.expand
>> end
>>
>> tag "anothertag" do |tag|
>>    gallery = Gallery.find_by_id tag.attr["id"] || tag.locals.gallery
>>    gallery.name
>> end
>>
>> This should work, but I haven't tested it, so I make no promises.  
>> You  may want to add a some sort of error handling in there in  
>> case no id is found.
>>
>> Keith Bingman
>> keith at keithbingman.com
>> Tel: +49-7731-7983830
>>
>>
>>
>> On Apr 4, 2007, at 1:58 AM, Adam Salter wrote:
>>
>>> Hey all,
>>> I'm helping Andrea Franz work on his fantastic Gallery Extension:
>>> http://darcs.bigchieflabs.com/radiant/extensions/gallery/
>>> (download with 'darcs' - http://darcs.net)
>>>
>>>
>>> Anyway I'm trying to add some globally available tags to it:
>>>
>>> There is a Model declared thus:
>>> class Gallery < ActiveRecord::Base
>>>   has_many :items,
>>>     :class_name => 'GalleryItem'
>>> 	<snip>
>>> end
>>>
>>> And I have made a module in the extension /lib called  
>>> gallery_tags.rb which contains this (fake example)...
>>>
>>> module GalleryTags
>>>   #tags available globally, not just on GalleryPages
>>>   include Radiant::Taggable
>>>
>>>   tag "mytag" do |tag|
>>>     if tag.attr["id"]
>>>       tag.locals.gallery = Gallery.find_by_id tag.attr["id"]
>>>     tag.expand
>>>   end
>>>
>>>   tag "anothertag" do |tag|
>>>     tag.locals.gallery.name
>>>   end
>>>
>>>   <snip>
>>> end
>>>
>>> added to the extension with:
>>>   def activate
>>>     admin.tabs.add "Galleries", "/admin/gallery", :after =>  
>>> "Layouts", :visibility => [:all]
>>>     Page.send :include, GalleryTags
>>>   end
>>>
>>> yet when I do this in a page:
>>>
>>> <r:mytag id='1'>
>>> <r:anothertag>
>>> </r:mytag>
>>>
>>> I get this:
>>> You have a nil object when you didn't expect it! The error  
>>> occurred while evaluating nil.name
>>>
>>>
>>> It seems that tag.locals.gallery is being set to nil in between  
>>> the tag calls... I can't work out why because other extensions  
>>> seem to do similar things... it's very confusing.
>>>
>>> Any help much appreciated.
>>>
>>> Thanks,
>>> -Adam
>>> _______________________________________________
>>> Radiant mailing list
>>> Post:   Radiant at lists.radiantcms.org
>>> Search: http://radiantcms.org/mailing-list/search/
>>> Site:   http://lists.radiantcms.org/mailman/listinfo/radiant
>>
>> _______________________________________________
>> Radiant mailing list
>> Post:   Radiant at lists.radiantcms.org
>> Search: http://radiantcms.org/mailing-list/search/
>> Site:   http://lists.radiantcms.org/mailman/listinfo/radiant
>
> _______________________________________________
> Radiant mailing list
> Post:   Radiant at lists.radiantcms.org
> Search: http://radiantcms.org/mailing-list/search/
> Site:   http://lists.radiantcms.org/mailman/listinfo/radiant

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.radiantcms.org/pipermail/radiant/attachments/20070404/8353cde8/attachment.html


More information about the Radiant mailing list