Display a list of subpages
Here is the example of the header snippet:
<ul>
<li><a href="<?php echo BASE_URL; ?>">home</a></li>
<?php foreach($this->find('/')->children() as $menu): ?>
<li><?php echo $menu->link(); ?></li>
<?php endforeach; ?>
</ul>
The first LI is added to link the Home Page. BASE_URL is a constant that Frog uses to display the link() or whole URL in the application. You can use it without any problem.
Then you have the $this->find('/') method that will return the page at the URL that you send it to, in this case, the Home Page.
The you have the children() method that will return all the subpages of the page already found. (You can do $this->children() and you will get all subpages of the current page.)
Then you have a “foreach” loop that will pass each subpage one by one and execute every line between this line and the <?php endforeach; ?>, in this case the line that will be repeated is: <li><?php echo $menu->link(); ?></li>. (You can see more about making links below.)
If you take a look at the code source of the page header you will see something like this:
<ul>
<li><a href="http://localhost/frog/?">Home</a></li>
<li><a href="http://localhost/frog/?about_us" title="About us">About us</a></li>
<li><a href="http://localhost/frog/?articles" title="Articles">Articles</a></li>
</ul>
What else can I do with $this->children()
- You can decide to add a condition to the search (be careful with this one!)
- You can limit the number of results returned (limit)
- You can change the offset to be returned (offset)
- You can determine the order (by created_on, by position, ASC ascending, DESC descending …)
- That’s enough power I think!
Example 1:
if you want to display the last 5 results only, here is how to do it:
<?php $last_result = $this->children(array('limit' => 5, 'order' => 'created_on DESC')); ?>
Then you need to loop those results like the example in the previous HOW-TO.
Example 2:
if you only want results 3 and 4, ordered by the position (by default):
<?php $last_result = $this->children(array('offset' => 2, 'limit' => 2)); ?>
This might appear weird to you but … the offset starts at 0 not at 1.
