Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We'll use Twig's extends tag to create a second page for our site. We will also set up some simple routing to control which page should be shown.
Simple routing:
$path = $_SERVER['REQUEST_URI'];
if ($path == '/contact') {
$nav['contact']['status'] = 'active';
echo $twig->render('contact.twig', array('navigation'=>$nav, 'a_variable' => 'Hello World!', 'page' => $path, 'form' => $_POST));
} else {
$nav['contact']['status'] = 'active';
echo $twig->render('home.twig', array('navigation'=>$nav, 'a_variable' => 'Hello World!', 'page' => $path));
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
I don't want to make any other
changes to my header, so
0:00
I'm just going to ignore the headblock.
0:03
Now I'm ready to add the main content.
0:05
For now, we're just going to
add an h1 header to the page.
0:08
block content.
0:13
<h1> Contact Us.
0:19
And we'll end the block.
0:26
We need to tell our index.html file when
to call the home versus the contact page.
0:30
But first we need to make sure
that our server will know to
0:37
use index.php as our controller.
0:41
Check the notes for
the htaccess file that you'll need.
0:44
It was included with the downloads.
0:48
We'll put it into our public folder.
0:52
And make sure that this
is named .htaccess.
0:57
There are some great routing
packages that you can use.
1:03
And if you're using a framework,
it will include routing.
1:06
Since that's not the focus of this course,
1:10
I'm gonna set up some simple routing
myself in the index.php file.
1:13
After I describe my navigation,
I'm gonna check the request URI.
1:19
This will tell me which page I will show.
1:24
i'm gonna use a condition, if,
and then I'll use a substring.
1:26
And I'll check my server.
1:33
REQUEST_URI.
1:38
I'm going to start with the first
character and grab the first 8 characters.
1:42
I'm going to then compare
that to /contact.
1:49
This isn't the most robust conditional,
which is why there are packages.
2:05
But it will work for our example and
allow for a query string.
2:09
I'm going to duplicate this render line,
And then I'm going to add an else.
2:13
Now I can tell my contact route
to use my contact.twig file.
2:26
Finally, I want the navigation
to have the correct status.
2:34
I'm going to change the default for
both items to false.
2:39
And then within my routing,
I'm going to say,
2:44
$nav['contact'] ['status'] = 'active'.
2:51
And then in home,
2:59
$nav['home']['status'] =
3:02
'active'.
3:10
Now we're ready to try our new page.
3:12
Now we can navigate between contact and
welcome.
3:16
We're ready to display a form.
3:21
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up