Complete and flexible blogging platform for ProcessWire

Render Archives

In this lesson, we look at the method renderArchives(), a function that renders your Blog Archives. It follows on from where we left in the lesson about its sister method getArchives()

<?php
 /**
 * Render blog archives for a given year.
 *
 * Used by the /site/templates/blog-archives.php template.
 * Render archives returned by the getArchives() method.
 * Archives links include a year headline followed by a list of months in that year with posts,
 * and the number of posts in each month. 
 *
 * @access public
 * @param array $years as returned by the getArchives() method
 * @return $out string
 *
 */
 public function renderArchives(Array $years)

renderArchives() accepts only one argument, $years, which is an array returned by its sister method getArchives(). It then outputs a chronological list of Blog Archives.

To output Blog Archives for all years is as simple as doing the following.

<?php
//first we call MarkupBlog
$blog = $modules->get('MarkupBlog');

//render a chronological list of blog archives
echo $blog->renderArchives($blog->getArchives()); 

The above code will render a list of Blog Archives similar to the following default output. In the image, the output is of course styled by CSS.

As we've previously learnt, passing the first argument $year to getArchives() will return only Archives for that year. Let's try this with renderArchives().

<?php
//first we call MarkupBlog
$blog = $modules->get('MarkupBlog');

//render a chronological list of blog archives
echo $blog->renderArchives($blog->getArchives(2009)); 

Using the above code will output Archives from 2009 only as illustrated in the image below.

If we wanted to see a few Post Titles rendered in our chronological listing, we need to declare an integer value for the argument $limit in our getArchives() as shown below.

//<?php
//first we call MarkupBlog
$blog = $modules->get('MarkupBlog');

//render a chronological list of blog archives
echo $blog->renderArchives($blog->getArchives('', 3)); 

This code will additionally output a limited number of Post titles (in this case 3) in our chronological listing for each archive month for each year as shown below. Notice the 'View All' link indicating that there are more Posts in the archives than the $limit set. In the demo blog-archive.php template file that ships with Blog, clicking on that link will display summarised Posts for that year and month. 

renderArchives() HTML output

The following markup shows the HTML outpu by renderArchives().

<div class="archive">
 <h3><a href="/pwtests/blog/archives/2014/">2014</a></h3>
  <span class="num-posts">12 posts</span><!-- total number of posts for the year -->
 <ul class="posts-group">
  <li><a href="/pwtests/blog/archives/2014/4/">April</a><span class="num-posts">5 posts</span></li> <!-- total number of posts for the month -->
  <li><a href="/pwtests/blog/archives/2014/5/">May</a><span class="num-posts">4 posts</span></li>
  <li><a href="/pwtests/blog/archives/2014/8/">August</a><span class="num-posts">3 posts</span></li>
 </ul>
</div>

renderArchives() CSS

The CSS attributes related to renderArchives() markup is shown below.

div.archive {}
span.num-posts {}
ul.posts-group {}

Summary

This lesson covered the method renderArchives($years) as used in conjunction with getArchives(). The method makes it very easy to ouput your Blog's Posts' Archives in a chronological order. It accepts only one argument $years which is an array returned by getArchives().