PDA

View Full Version : Starting a template.



LearningCoder
10-27-2012, 08:24 PM
Hi, I have watched a few videos on making a template and watched one from Lynda.com.

I want to produce a template designed site. I'm having trouble deciding the filenames and I am not sure if I have to create multiple files containing hardcoded html or whether I can have one file which is searched and depending on code in there will return the specific html to be outputted.

Here is what I have:

This is called index.php at the moment and contains this code:


<?php

$link = (isset($_GET['title'])) ? mysql_real_escape_string($_GET['title']) : 'home';

$link = ucfirst($link);

switch ($link) {

case 'Home':
$file = file_get_contents("homepage.php");
$file = preg_replace("/{title}/","Gardenable - {$link}",$file);
break;
default:
$file = file_get_contents("homepage.php");
$file = preg_replace("/{title}/","Gardenable - {$link}",$file);
}

echo $file;
?>



And this is my homepage.php, which contains just plain html:


<html>
<head>
<title>{title}</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>

<body>
<div id="container">

<div id="header">
<img src="images/logo1.jpg" alt="Gardenable Logo" title="Gardenable" id="logo" border="0" />

<div id="slogan_div">
<h2>Making gardening accessible to everyone!</h2>
</div>

<div id="menu_div">
<ul>
<li><a href='index.php?title=home'>Home<a/></li>|
<li><a href='index.php?title=about'>About Us</a></li>|
<li><a href='index.php?title=gallery'>Gallery</a></li>|
<li><a href='index.php?title=contact'>Contact</a></li>
</ul>
</div>
</div>

</div>
</body>
</html>


I'm having difficulty visioning how I will create the other pages and use the index.php to echo the contents. It seems if I have to create different files containing html for the different pages, then echo'ing it out depending on results, then it would not come across as a template. I wanted to create a template which just loads content to a page which already has a design layout. For instance, I have one main div which is for ALL content for that certain page, how can I dynamically load content into it depending on if the user changes the page.

Regards,

Lc.

tangoforce
10-27-2012, 08:50 PM
Easy really..



<html>
<head>
<title>{title}</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>

<body>
{content}
</body>
</html>


In your homepage php code you load this via file_get_contents() and merge it with your template like you would the title:


<div id="container">

<div id="header">
<img src="images/logo1.jpg" alt="Gardenable Logo" title="Gardenable" id="logo" border="0" />

<div id="slogan_div">
<h2>Making gardening accessible to everyone!</h2>
</div>

<div id="menu_div">
<ul>
<li><a href='index.php?title=home'>Home<a/></li>|
<li><a href='index.php?title=about'>About Us</a></li>|
<li><a href='index.php?title=gallery'>Gallery</a></li>|
<li><a href='index.php?title=contact'>Contact</a></li>
</ul>
</div>
</div>

</div>


See? You have a main template and subtemplates. You can even use templates for things like select boxes, forms, radio buttons etc. As long as its all put into the *main* template you'll only need to design one main template to hold your content no matter how many pages you have.