# Check for children

**URL:** <https://www.ruby-forum.com/t/check-for-children/69825>\
**Category:** Radiant CMS\
**Created:** [September 24, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/check-for-children/69825 "2006-09-24T16:49:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steven\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9bd4f/32.png) [@Steven\_N](https://www.ruby-forum.com/u/Steven_N)\
**Post date:** [September 24, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/check-for-children/69825/1 "2006-09-24T16:49:55Z")

</div>

In my base template, I want to output a

 only if some children  
of the current page exist. How can I check for children of a page?

-- Steven N. http://outerthought.org/ Outerthought Open Source Java & XML stevenn at outerthought.org stevenn at apache.org

---

<div class="post-metadata">

**Author:** ![Steven\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9bd4f/32.png) [@Steven\_N](https://www.ruby-forum.com/u/Steven_N)\
**Post date:** [September 24, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/check-for-children/69825/2 "2006-09-24T17:52:01Z")

</div>

On 24/09/06, Steven N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In my base template, I want to output a
> 
> only if some children  
> of the current page exist. How can I check for children of a page?

You need to define a new tag like \<r:if\_children\> that would work like  
this:

- 

This is explained in more details on the wiki - to which I hope you’ll  
contribute your experiences with! 🙂

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

–  
Regards,  
Dave

---

<div class="post-metadata">

**Author:** ![Steven\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9bd4f/32.png) [@Steven\_N](https://www.ruby-forum.com/u/Steven_N)\
**Post date:** [September 24, 2006, 6:52pm UTC](https://www.ruby-forum.com/t/check-for-children/69825/3 "2006-09-24T18:52:09Z")

</div>

Dave C. wrote:

> -

Perhaps, but the children:each tag will only work if children exist. So  
in a sense it encapsulates the if\_children idea. If you give us a  
little more context or detail, I might be able to come up with some code  
that will do the trick.

Sean C.  
[seancribbs.com](http://seancribbs.com)

---

<div class="post-metadata">

**Author:** ![Steven\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9bd4f/32.png) [@Steven\_N](https://www.ruby-forum.com/u/Steven_N)\
**Post date:** [September 24, 2006, 9:06pm UTC](https://www.ruby-forum.com/t/check-for-children/69825/4 "2006-09-24T21:06:24Z")

</div>

On 24 Sep 2006, at 18:49, Sean C. wrote:

> exist. So in a sense it encapsulates the if\_children idea. If you  
> give us a little more context or detail, I might be able to come up  
> with some code that will do the trick.

I have a

 containing a secondary navigation that should only be  
inserted when there’s underlying children.

Something like:

\<r:if\_children\>

### More information

would do the trick.

-- Steven N. http://outerthought.org/ Outerthought Open Source Java & XML stevenn at outerthought.org stevenn at apache.org

---

<div class="post-metadata">

**Author:** ![Steven\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9bd4f/32.png) [@Steven\_N](https://www.ruby-forum.com/u/Steven_N)\
**Post date:** [September 24, 2006, 9:36pm UTC](https://www.ruby-forum.com/t/check-for-children/69825/5 "2006-09-24T21:36:27Z")

</div>

On 24 Sep 2006, at 17:50, Dave C. wrote:

> \</r:children:each\>
> 
> This is explained in more details on the wiki - to which I hope you’ll  
> contribute your experiences with! 🙂

I added this to my --unpack’ed version of app/models/page\_context.rb :

```
 #
 # <r:if_children>...</r:if_children>
 #
 #
 define_tag 'if_children' do |tag|
   tag.expand if tag.locals.page.has_children?
 end

```

en that indeed did the trick. No idea however if this is the best way  
to do this. I can provide a (obviously) very simple patch if people  
are interested.

-- Steven N. http://outerthought.org/ Outerthought Open Source Java & XML stevenn at outerthought.org stevenn at apache.org

---

<div class="post-metadata">

**Author:** ![Steven\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9bd4f/32.png) [@Steven\_N](https://www.ruby-forum.com/u/Steven_N)\
**Post date:** [September 25, 2006, 7:31am UTC](https://www.ruby-forum.com/t/check-for-children/69825/6 "2006-09-25T07:31:28Z")

</div>

On 24 Sep 2006, at 22:24, Dave C. wrote:

> If you could provide it as a plugin, that would be awesome! 🙂

Creating a plugin for a two-line patch for a totally trivial addition  
would be overkill, no?

# Index: app/models/page\_context.rb

— app/models/page\_context.rb (revision 122)  
+++ app/models/page\_context.rb (working copy)  
@@ -40,6 +40,15 @@  
end

```
  #

```

- 
# \<r:if\_children\>…\</r:if\_children\>
- 
# 
- 
# Checks for existing children.
- 
# 
- define\_tag ‘if\_children’ do |tag|
- 

```
 tag.expand if tag.locals.page.has_children?

```

- end
- 
- 
# 

# \<r:children:count /\>

# 

# Renders the total number of children.

-- Steven N. http://outerthought.org/ Outerthought Open Source Java & XML stevenn at outerthought.org stevenn at apache.org

---

<div class="post-metadata">

**Author:** ![Steven\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9bd4f/32.png) [@Steven\_N](https://www.ruby-forum.com/u/Steven_N)\
**Post date:** [September 25, 2006, 1:05pm UTC](https://www.ruby-forum.com/t/check-for-children/69825/7 "2006-09-25T13:05:58Z")

</div>

On 25/09/06, Steven N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 24 Sep 2006, at 22:24, Dave C. wrote:
> 
> > If you could provide it as a plugin, that would be awesome! 🙂
> 
> Creating a plugin for a two-line patch for a totally trivial addition  
> would be overkill, no?

Sure, I’m just thinking of people who didn’t install with --unpack  
that’s all 🙂

–  
Regards,  
Dave

---

<div class="post-metadata">

**Author:** ![Steven\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9bd4f/32.png) [@Steven\_N](https://www.ruby-forum.com/u/Steven_N)\
**Post date:** [September 24, 2006, 10:24pm UTC](https://www.ruby-forum.com/t/check-for-children/69825/8 "2006-09-24T22:24:33Z")

</div>

On 24/09/06, Steven N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> en that indeed did the trick.

🙂

> No idea however if this is the best way  
> to do this.

This is exactely the way I have done it.

> I can provide a (obviously) very simple patch if people  
> are interested.

If you could provide it as a plugin, that would be awesome! 🙂

–  
Regards,  
Dave

---

<div class="post-metadata">

**Author:** ![Steven\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b9bd4f/32.png) [@Steven\_N](https://www.ruby-forum.com/u/Steven_N)\
**Post date:** [September 25, 2006, 2:42pm UTC](https://www.ruby-forum.com/t/check-for-children/69825/9 "2006-09-25T14:42:13Z")

</div>

Actually, there are a score of “global tags” that I’m thinking of  
collecting  
and putting into a patch, although most people could put them in a  
plugin in  
the meantime like I demonstrated in the How To.

Sean C.  
[seancribbs.com](http://seancribbs.com)
