# Parent tag?

**URL:** <https://www.ruby-forum.com/t/parent-tag/100619>\
**Category:** Radiant CMS\
**Created:** [July 30, 2006, 11:10pm UTC](https://www.ruby-forum.com/t/parent-tag/100619 "2006-07-30T23:10:43Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [July 30, 2006, 11:10pm UTC](https://www.ruby-forum.com/t/parent-tag/100619/1 "2006-07-30T23:10:43Z")

</div>

Any reason this shouldn’t be part of page\_context? It’s incredibly  
handy for nav.

```
define_tag 'parent' do |tag|
  tag.locals.page = page.parent
  tag.expand
end
```

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [July 31, 2006, 12:58am UTC](https://www.ruby-forum.com/t/parent-tag/100619/2 "2006-07-31T00:58:02Z")

</div>

You should keep the current page so you can come back to it afterwards,  
to  
allow showing more data from the starting page.

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [July 31, 2006, 3:04pm UTC](https://www.ruby-forum.com/t/parent-tag/100619/3 "2006-07-31T15:04:14Z")

</div>

Jay L. wrote:

> Any reason this shouldn’t be part of page\_context? It’s incredibly  
> handy for nav.
> 
> ```
> define_tag 'parent' do |tag|
> tag.locals.page = page.parent
> tag.expand
> end
> 
> ```

This is a great idea. Please submit a patch.

–  
John L.  
[http://wiseheartdesign.com](http://wiseheartdesign.com)

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [August 3, 2006, 1:12pm UTC](https://www.ruby-forum.com/t/parent-tag/100619/4 "2006-08-03T13:12:25Z")

</div>

Andrew H. wrote:

> It needs this tweak:
> 
> define\_tag ‘parent’ do |tag|  
> tag.locals.page = page.parent || page  
> tag.expand  
> end
> 
> Note the “|| page” on the second line.

Instead of taking the approach above, let’s do this:

define\_tag ‘parent’ do |tag|  
parent = tag.locals.page.parent  
tag.locals.page = parent  
tag.expand unless parent.nil?  
end

This way the tag will not be rendered if the parent doesn’t exist. The  
above also makes the tag context sensitive (so you can use it inside a  
children:each tag).

We should also add the if\_parent and unless\_parent tags:

define\_tag ‘if\_parent’ do |tag|  
tag.expand unless tag.locals.page.parent.nil?  
end

define\_tag ‘unless\_parent’ do |tag|  
tag.expand if tag.locals.parent.nil?  
end

If someone can get me a unit tested patch for this I’ll apply it.

–  
John L.  
[http://wiseheartdesign.com](http://wiseheartdesign.com)

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [August 2, 2006, 8:29pm UTC](https://www.ruby-forum.com/t/parent-tag/100619/5 "2006-08-02T20:29:51Z")

</div>

John W. Long wrote:

> This is a great idea. Please submit a patch.

Agreed. I can now do the following to have a heading that is itself a  
link to the parent page, in the manner of many Wiki engines:

# 

However, this breaks horribly on the root page and in so doing reveals a  
small bug in the parent tag code. It needs this tweak:

define\_tag ‘parent’ do |tag|  
tag.locals.page = page.parent || page  
tag.expand  
end

Note the “|| page” on the second line.

Things get a bit hideous if we want to avoid having a link on the Home  
page in the heading, though, assuming you’re using the construct above  
in a layout. Indenting things a bit to make it more legible, I’ve got  
this right now:

\<r:unless\_url matches="^/$"\>  
\<r:parent\>\<r:link\>  
\<r:page\>\<r:title /\>\</r:page\>  
\</r:link\>\</r:parent\>  
\</r:unless\_url\>  
\<r:if\_url matches="^/$"\>  
\<r:title /\>  
\</r:if\_url\>

Is there a more elegant approach?

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [September 15, 2006, 12:42am UTC](https://www.ruby-forum.com/t/parent-tag/100619/6 "2006-09-15T00:42:42Z")

</div>

Jay L. wrote:

> I’m in the process of moving but will submit something this week with  
> tests unless beats me to it…
> 
> Jay

Has this been submitted? I couldn’t find it after a cursory search.

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [August 6, 2006, 4:55pm UTC](https://www.ruby-forum.com/t/parent-tag/100619/7 "2006-08-06T16:55:53Z")

</div>

John W. Long wrote:

> Instead of taking the approach above, let’s do this:
> 
> define\_tag ‘parent’ do |tag|  
> parent = tag.locals.page.parent  
> tag.locals.page = parent  
> tag.expand unless parent.nil?  
> end

I like that much better - a parent tag should never return self! This  
makes it consistent with child behavior.

I’m in the process of moving but will submit something this week with  
tests unless beats me to it…

Jay

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [September 15, 2006, 1:02am UTC](https://www.ruby-forum.com/t/parent-tag/100619/8 "2006-09-15T01:02:47Z")

</div>

Dave C. wrote:

> > 
> 
> There’s something about parents tags at
> 
> [http://dev.radiantcms.org/radiant/wiki/HowToDefineGlobalTags](http://dev.radiantcms.org/radiant/wiki/HowToDefineGlobalTags)

Yes, I submitted that addition to the page. I use those tag definitions  
right now on [seancribbs.com](http://seancribbs.com).

Sean

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [September 15, 2006, 12:52am UTC](https://www.ruby-forum.com/t/parent-tag/100619/9 "2006-09-15T00:52:45Z")

</div>

On 14/09/06, Joseph B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Jay L. wrote:
> 
> > I’m in the process of moving but will submit something this week with  
> > tests unless beats me to it…
> > 
> > Jay
> 
> Has this been submitted? I couldn’t find it after a cursory search.

There’s something about parents tags at

[http://dev.radiantcms.org/radiant/wiki/HowToDefineGlobalTags](http://dev.radiantcms.org/radiant/wiki/HowToDefineGlobalTags)

–  
Regards,  
Dave

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [November 12, 2006, 3:41am UTC](https://www.ruby-forum.com/t/parent-tag/100619/10 "2006-11-12T03:41:10Z")

</div>

Actually, the parent tag is in trunk as of [162]. Here’s the patch if  
you’re interested:

[http://dev.radiantcms.org/radiant/attachment/ticket/451/parent\_tag.diff](http://dev.radiantcms.org/radiant/attachment/ticket/451/parent_tag.diff)

Jesse

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [November 12, 2006, 5:01am UTC](https://www.ruby-forum.com/t/parent-tag/100619/11 "2006-11-12T05:01:55Z")

</div>

Jesse Newland wrote:

> Actually, the parent tag is in trunk as of [162]. Here’s the patch if  
> you’re interested:
> 
> [http://dev.radiantcms.org/radiant/attachment/ticket/451/parent\_tag.diff](http://dev.radiantcms.org/radiant/attachment/ticket/451/parent_tag.diff)
> 
> Jesse

argh. Guess I didn’t restart radiant after updating and assumed it  
wasn’t applied yet.

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [November 11, 2006, 10:15pm UTC](https://www.ruby-forum.com/t/parent-tag/100619/12 "2006-11-11T22:15:53Z")

</div>

Dave C. wrote:

> On 14/09/06, Joseph B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Jay L. wrote:
> > 
> > > I’m in the process of moving but will submit something this week with  
> > > tests unless beats me to it…
> > > 
> > > Jay
> > 
> > Has this been submitted? I couldn’t find it after a cursory search.
> 
> There’s something about parents tags at
> 
> [http://dev.radiantcms.org/radiant/wiki/HowToDefineGlobalTags](http://dev.radiantcms.org/radiant/wiki/HowToDefineGlobalTags)
> 
> –  
> Regards,  
> Dave

That probably shouldn’t be on that page because there is no parent tag  
unless you create one yourself.
