How To Define Global Tags
Introduction
Global tags are Radius rags like<r:link> that work on all pages and do not require specifying a Behavior for the page.
If you have deployed Radiant with the --unpack switch, you can edit app/models/page_context.rb directly.
Alternatively you can write an extension, as covered in Creating Radiant Extensions.
Examples
If Children Exist
To create a new tag that only renders its content when the current page has children, use this code
tag "if_children" do |tag|
tag.expand if tag.locals.page.has_children?
end
Then in your page write
<r:if_children>
<ul>
<r:children:each by="title" order="asc">
<li><r:link /></li>
</r:children:each>
</ul>
</r:if_children>
This will create a unordered list of links to the children of the page, sorted by ascending title, if any exist.
Random Child Page
tag "children:random" do |tag|
children = tag.locals.children
if children.size > 0
index = rand(children.size)
tag.locals.page = children[index]
tag.expand
end
end
Then in your page, write
<r:children:random /><r:title /></r:children:random>
Or just
<r:children:random:title />
Note that this will only select one child page, and needs modification to select more than one.
Refer to the Parent Page
tag "parent" do |tag|
tag.locals.page = tag.locals.page.parent
tag.expand if tag.locals.page.has_parent?
end
tag "if_parent" do |tag|
tag.expand if tag.locals.page.has_parent?
end
tag "unless_parent" do |tag|
tag.expand unless tag.locals.page.has_parent?
end
In your page, write this:
<r:parent><r:link>Go up</r:link></r:parent>
This will create a link to the parent page that says “Go up”.