BlogTutorial:List Page

From Aspen Documentation

After installing the framework into your development environment, visit the http://yourdomain.com/admin directory and login with the account you created during installation.

The first thing we need to do before we may begin editing is create a new module for our blog. Using modadmin, run the following command:

$ php modules/modadmin.php add blog admin

You should see a success message:

A new module called "Blog" has been created successfully.

Files we'll be editing in this step:

  • modules/Blog/Blog_Admin.php
  • modules/Blog/templates/index.tpl.php

Contents

Create DB Table

Before we begin working with data we need to create a new table for our blog. Since this is a tutorial, we'll keep it simple.

CREATE TABLE `blog` (
`entry_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`entry_title` TEXT NOT NULL ,
`entry_body` LONGTEXT NOT NULL ,
`date_posted` DATETIME NOT NULL ,
PRIMARY KEY ( `entry_id` )
) ENGINE = MYISAM;

Run this query in your aspen framework database and you'll have a new table called "blog".

Editing Module Class

Now that you've got an empty table, we can prepare for writing list, add, edit, and delete functions.

Open for Editing: modules/Blog/Blog_Admin.php

By default (if you used modadmin your class file core should look like:

<?php
 
/**
 * @abstract Blog_Admin class
 */
class Blog_Admin {
 
	/**
	 * @var object $APP Holds a reference to our application
	 * @access private
	 */
	private $APP;
 
	/**
	 * @abstract Constructor, initializes the module
	 * @access public
	 */
	public function __construct(){ $this->APP = get_instance(); }
 
	/**
	 * @abstract Displays the default template for this module.
	 * @access public
	 */
	public function view(){
 
		$this->APP->template->addView($this->APP->template->getTemplateDir().DS . 'header.tpl.php');
		$this->APP->template->addView($this->APP->template->getModuleTemplateDir().DS . 'index.tpl.php');
		$this->APP->template->addView($this->APP->template->getTemplateDir().DS . 'footer.tpl.php');
		$this->APP->template->display();
 
	}
}
?>

Let's add in some empty functions which will act as our add, edit, and delete functions. Place these new functions after the view function. We'll be setting the edit and delete functions up with an $id parameter since they'll need to reference a record ID.

	public function add(){ }
 
	public function edit($id = false){ }
 
	public function delete($id = false){ }

Now let's add some basic code to the view function that will pull a list of blog entries and pass it to our template.

List Entry Query

Our goal inside of this function is to run a query in which we pull a list of all blog entries. We then need to pass that data to our templates so they can handle displaying it.

	public function view(){
 
		$data = array();
 
		$this->APP->model->select('blog');
		$data['entries'] = $this->APP->model->results();
 
		$this->APP->template->addView($this->APP->template->getTemplateDir().DS . 'header.tpl.php');
		$this->APP->template->addView($this->APP->template->getModuleTemplateDir().DS . 'index.tpl.php');
		$this->APP->template->addView($this->APP->template->getTemplateDir().DS . 'footer.tpl.php');
		$this->APP->template->display($data);
 
	}

The first step is to setup an array called $data. This array will hold everything that needs to be passed to the template. Place the array variable name as the only argument to the display function. For more information on this process, see the Template documentation.

The next line of code asks the model class to do a basic select of all records in the "blog" table.

		$this->APP->model->select('blog');

The model class has many features which allow you to build and perform actions on queries, but for now we'll keep it simple. For more information see the Model documentation.

The function above is essentially the same as running the following query:

SELECT * FROM blog

The next line then runs the query and parses the results. We assign the array that gets returned to $data['entries']. Since the results have now been included in the $data array, they will be passed to our template.

		$data['entries'] = $this->APP->model->results();

Display Entries Template

Now that your blog entries are being queried and sent to the index.tpl.php template that's being loaded, we need to display any results.

Open for Editing: modules/Blog/templates/index.tpl.php

Replace the default template content with the following:

<h3>Blog</h3>
 
<table>
	<thead>
		<tr>
			<th>Entry Id</th>
			<th>Entry Title</th>
			<th>Entry Body</th>
			<th>Date Posted</th>
		</tr>
	</thead>
	<tbody>
 
		<?php
		if($blog['RECORDS']){
			foreach($blog['RECORDS'] as $record){
		?>
		<tr>
		<td><?php print $record['entry_id']; ?></td>
		<td><?php print $record['entry_title']; ?></td>
		<td><?php print $record['entry_body']; ?></td>
		<td><?php print $record['date_posted']; ?></td>
		</tr>
 
		<?php
			}
		}
		?>
 
	</tbody>
</table>

Whenever you return database results from the model class, they're returned as the RECORDS index in an array. If no results were found then it will simply be set to false.

All keys in the $data array from our class are turned into separate variables when passed to the template. So calling $entries['RECORDS'] refers to the $data['entries']['RECORDS'] we had in the class.

Add Entry Link

We need to add our first entry, so we need to create the ability to write a blog post. Somewhere in your index.tpl.php file, let's post a link to the add page:

<p><?php print $this->createLink('Add a new record', 'add'); ?></p>

The template class has a special createLink function that makes it easy to create a link with your application. The first argument in the createLink function is the link text. The second argument is the page within your module to call.

Go ahead and save all of your changes and then refresh your browser.

Next: Add Entry Page