Noskiw Posted August 2, 2014 Share Posted August 2, 2014 (edited) So I have a simple template that I want to be the index page of the website that I'm working on, however, I don't want to have to create several almost identical files for each of the directories. Is there perhaps anyway that I can change the content of the main div yet the address be a different directory. So for example, this is the main index page: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <head> <title>Matthew Noskiw's website - Home</title> <link rel='stylesheet' type='text/css' href='./inc/CSS/styles/default.css' /> <link rel='stylesheet' type='text/css' href='./inc/CSS/static/navigation.css' /> </head> <body> <div id='Container'> <div id='Header'> <div class='right'> <ul id='Navigation'> <li id='NavButton' class='active'><a href='./'>Home</a></li> <li id='NavButton'><a href='./about'>About</a></li> <li id='NavButton'><a href='./blog'>Blog</a></li> <li id='NavButton'><a href='./contact'>Contact</a></li> </ul> </div> <span>noskiw.co.uk</span> </div> <hr /> <div id='MainContent'> <h2>Home</h2> <br /> <span> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </span> <br /><br /> <span> Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? </span> </div> <hr /> <div id='Footer'> <span>Matthew Noskiw</span> </div> </div> </body> </html> But if you look in the <h2> tag in the 'MainContent' div, then you'll see it reads "Home". What I want to happen is that when I change the page, for example, go to /about I want it to read <h2>About</h2>. I have 4 different pages for this so far, is there any way to get it down to just one yet they be in different directories? P.S The reason this is in the PHP Help section is because I assumed that it would more than likely be something to do with PHP. Edited August 2, 2014 by ghostrider1337 Quote Link to comment https://forums.phpfreaks.com/topic/290239-using-a-template-in-different-directories/ Share on other sites More sharing options...
Solution Ch0cu3r Posted August 2, 2014 Solution Share Posted August 2, 2014 yes you can setup a very basic template file. If put the above code in file called template.php. Now replace <h2>Home</h2> with <?php echo $page_title; ?> When you want to use the template file you'd do something like this // define the page title $page_title = 'About'; // now include the template include $_SERVER['DOCUMENT_ROOT'] . '/template.php'; Quote Link to comment https://forums.phpfreaks.com/topic/290239-using-a-template-in-different-directories/#findComment-1486691 Share on other sites More sharing options...
Noskiw Posted August 2, 2014 Author Share Posted August 2, 2014 So because I also want to change which of the list items in the navigation <ul> is active, would I also use $_SERVER to find the directory. Obviously I'd have to use an index to identify the directory. But then something like: <li id='NavButton' <?php if(CheckDir($dir) == "home") { echo "class='active'; } ?>><a href='#'>Home</a></li> Quote Link to comment https://forums.phpfreaks.com/topic/290239-using-a-template-in-different-directories/#findComment-1486692 Share on other sites More sharing options...
Noskiw Posted August 2, 2014 Author Share Posted August 2, 2014 I think I've figured it out. What I've done is this: <?php $FullDir = $_SERVER['PHP_SELF']; $dir = explode("/", $FullDir); echo $dir[2]; ?> This, on the index page returns "index.php", on the about page it returns "about", on the "contact" page it returns contact, and on the blog page it returns "blog". Thank you for your help though, Ch0cu3r. It was you mentioning $_SERVER which greatly helped me, as I went and researched it a little bit and that's what led me to this. Quote Link to comment https://forums.phpfreaks.com/topic/290239-using-a-template-in-different-directories/#findComment-1486693 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.