Creating a New Page

Nova 2 Tutorial Developer Resource

Oftentimes, the built-in pages in Nova just won't be enough and you need to create a new page for you sim to hold new information.

The Problem

After having a killer month, your simming organization bestows on you several awards and you want a place to display those awards for the world to see.

The Solution

Like a lot of things in Nova, there are two ways to accomplish this. We'll start with the easy way and then move up to the more advanced way.

Using Thresher

The simplest solution is to use Thresher, Nova's built-in mini-wiki. Using Thresher, you can quickly and easily create a new page, add links to images and whatever else you want. The only downside to this solution is that it's stored in the wiki. If that isn't an issue or you're having trouble following the more advanced option below, Thresher is your best bet.

Creating a New Page

That's all well and good, but what if you don't want the page stored in the wiki? Instead, you want it as part of your sim section. Let's step through how you would go about adding a new page to the sim section.

1. Set Up the Controller

This is, most likely, one of your first forays into controllers. In the simplest terms, a controller is like a traffic cop. The web browser navigates to a URL at which point the traffic cop (the controller), tells you where to go from there. A controller is simply a PHP class that directs a request to the appropriate place. In order to build our page, we need some simple code in our controller.

To start, we're going to open app/controllers/sim.php. This is the controller that handles all of the pages in the sim section. When you open the file, you'll notice that it's almost empty. The core sim pages are stored in the base controller in the Nova module. Since we're not interested in modifying those, we're going to just add a new controller action to this file after the comment about adding your own methods after it.

public function sim_awards()
{
	$this->_regions['content'] = Location::view('sim_allawards', $this->skin, 'main', false);
	$this->_regions['title'].= "Awards We've Won";
	
	Template::assign($this->_regions);
	
	Template::render();
}

Let's step through this piece-by-piece to see what's going on.

public function sim_awards()

A controller action is nothing more than a class method. A class method is a function inside a class. Pretty simple. Nova will use the name of the controller action as part of the URL. (This actually is what tells the controller where it needs to go.) In this case, our URL would be index.php/sim/sim_awards. You can name your controller action whatever you want provided it doesn't conflict with another method in that particular controller or that it isn't a reserved PHP word.

$this->_regions['content'] = Location::view('sim_allawards', $this->skin, 'main', false);

Nova templates are broken up into regions. The guts of a page are part of the content region. What we're doing here is assigning a view file to the content region to be rendered by the browser. Sounds complicated, but it really isn't. All you need to know is the second part: the location class.

In order for seamless substitution to work, we created a location class (it used to be a helper in Nova 1) that does all the heavy lifting and figures out where to pull files from. In this case, we're looking for a view that's named sim_allawards.php (the .php part is assumed so you don't have to include it). After that, the class is simplying being told where to look and what section it's part of.

$this->_regions['title'].= "Awards We've Won";

Now, we're simply setting the title of the page (what we see in the browser's title bar). You can see that to whatever you want provided it's a string.

Template::assign($this->_regions);

This is required as it takes all the regions and assigns them to the template.

Template::render();

Pretty self-explanatory here, but we're telling the Template class to render the template to the browser window.

2. Set Up the View

If you tried to go to your page above, you'd be greeted by a nasty error telling you something is missing. What we're missing is the guts of the page, or the view file. The second part of this is to create your view file. To do that, we're going to create a file called sim_allawards.php in app/views/_base_override/main/pages.

So what are we going to put in our view file?

<h1 class="page-head">Awards We've Won</h1>

<p>Below is a list of all the awards we've won!</p>

<p><img src="<?php echo base_url();?>app/assets/images/award_image.jpg" /></p>

As you can see, it's really straightforward. The only thing we're doing is using CodeIgniter's built-in base_url function to get the base URL of our site. (If we don't use the base URL, CI will try to append index.php to our image path and it won't be able to find the image.)

From here, you can make whatever changes you want to the view file and continue to add awards. Because we've done all this work in the application directory, we don't have to worry about losing our changes when we make an update either!

Log in with your AnodyneID to rate this article or submit the article for review.