BlogTutorial:Display Entry
From Aspen Documentation
Now that we've got some basic blog data, we need to display the entries on our website.
Note: This tutorial assumes that we're using a combination of modules which is how the framework is setup by default. Our display code will be written in our front page index.php file, outside of, but using the framework.
Creating A Support Module
Inside of the module directory we need to create a new standalone module class that will allow our separate files access to the framework.
Inside of the module folder, create the following file and open it for editing:
modules/Blog/Blog.php
In this file we need to define a basic class and give it a connection to the framework.
<?php class Blog extends Bootstrap { /** * @abstract Constructor, initializes the module * @access public */ public function __construct($config){ parent::__construct($config); } } ?>
This type of module is setup as an extension to the framework which allows it to act as a bridge between our website files and the framework. For other methods of setting up modules, please see Choosing Your Module.
Once that's complete we're ready to write a method.
Blog Post List Method
We need to write a function that pulls a list of blog entries made within the last thirty days. As we did when writing our admin list function, we'll begin with using model class.
public function recentBlogEntries(){ $this->model->select('blog'); $this->model->inPastXDays('date_posted', 30); $this->model->orderBy('entry_id', 'DESC'); return $this->model->results(); }
Because we're working with the framework directly and not an overlaying application, we leave out the APP object.
The function above simply runs a select query on the blog and sets a special condition for entries within the last thirty days. Then we tell it to sort the results by unique id, descending so the newest are at the top.
Display Code
Now that we have a core function to pull the list of blog entries, we need to display it.
Open the index.php file in the root folder of the framework distribution.
Since this file is already loading another module we don't need to worry about including the system/loader.inc.php file.
After the include on the first line, enter the following:
<?php $blog = load_module('Blog'); ?>
Now, within the page content we need to run the method we wrote previously and display our blog posts.
<?php $entries = $blog->recentBlogEntries(); if($entries['RECORDS']){ foreach($entries['RECORDS']){ } } ?>
We begin by assigning the returned data from our recentBlogEntries, which should be an array of database results from our query.
Just as we did in the admin templates we iterate through the records of the return value.
At this point we're not printing anything. So, let's simply print out our entry title and content.
<?php $entries = $blog->recentBlogEntries(); if($entries['RECORDS']){ foreach($entries['RECORDS'] as $entry){ ?> <strong><?php print $entry['entry_title']; ?></strong> <p><?php print $entry['entry_body']; ?></p> <?php } } ?>
Save your files and refresh your root aspen directory in your browser. You should now see your blog posts!