Lee Posted December 24, 2008 Share Posted December 24, 2008 Hi, I am building my first site using php templates. I'm just a beginner so Its just a chopped up html page where I include a header & footer, then only edit the page body & meta for each page. My index page looks like this <?php include("templates/head-tag1-tpl.php"); ?> <?php include("templates/index-meta-tpl.php"); ?> <?php include("templates/head-tag2-tpl.php"); ?> <?php include("templates/header-tpl.php"); ?> <?php include("templates/index-body-tpl.php"); ?> <?php include("templates/footer-tpl.php"); ?> I wonder is it possible to create a variable at the top of this code to alter the name of the template to be called for. For example the above cose is for the index page so it calls for index-meta and index-body. Can I do a variable at the top of the page, then call that variable in the page includes. I tried the following. It doesn't work, but hopefully explains what I'm trying to achieve. <?php $page = "index"; ?> <?php include("templates/head-tag1-tpl.php"); ?> <?php include("templates/$page-meta-tpl.php"); ?> <?php include("templates/head-tag2-tpl.php"); ?> <?php include("templates/header-tpl.php"); ?> <?php include("templates/$page-body-tpl.php"); ?> <?php include("templates/footer-tpl.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/138304-included-page-name-variable/ Share on other sites More sharing options...
wildteen88 Posted December 24, 2008 Share Posted December 24, 2008 I guess your variable is called $page? If so then you'll do: <?php include("templates/head-tag1-tpl.php"); ?> <?php include("templates/{$page}-meta-tpl.php"); ?> <?php include("templates/head-tag2-tpl.php"); ?> <?php include("templates/header-tpl.php"); ?> <?php include("templates/{$page}body-tpl.php"); ?> <?php include("templates/footer-tpl.php"); ?> Or <?php include("templates/head-tag1-tpl.php"); ?> <?php include('templates/'.$page.'-meta-tpl.php'); ?> <?php include("templates/head-tag2-tpl.php"); ?> <?php include("templates/header-tpl.php"); ?> <?php include('templates/'.$page.'body-tpl.php'); ?> <?php include("templates/footer-tpl.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/138304-included-page-name-variable/#findComment-723130 Share on other sites More sharing options...
Lee Posted December 24, 2008 Author Share Posted December 24, 2008 WooHoo! Yes, that works thanks wildteen. Now I can just paste that same tpl list on every page and edit only the variable to call for the relevant templates. Hopefully this should set my site up for a good starting point as I learn more php. Very Merry Xmas to you! Quote Link to comment https://forums.phpfreaks.com/topic/138304-included-page-name-variable/#findComment-723132 Share on other sites More sharing options...
wildteen88 Posted December 24, 2008 Share Posted December 24, 2008 I noticed I had a slight typo, the line: <?php include("templates/{$page}body-tpl.php"); ?> should of been <?php include("templates/{$page}-tpl.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/138304-included-page-name-variable/#findComment-723133 Share on other sites More sharing options...
Lee Posted December 24, 2008 Author Share Posted December 24, 2008 Lol, I just spotted that too thanks! I went a little further with the organising and now have this. Should make life a lot easier for pages in sub-directories e.t.c. <?php // Page name $page = "index"; ?> <?php // Path to the templates folder $path = "templates"; ?> <?php include("{$path}/head-tag1-tpl.php"); ?> <?php include("{$path}/{$page}-meta-tpl.php"); ?> <?php include("{$path}/head-tag2-tpl.php"); ?> <?php include("{$path}/header-tpl.php"); ?> <?php include("{$path}/{$page}-body-tpl.php"); ?> <?php include("{$path}/footer-tpl.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/138304-included-page-name-variable/#findComment-723135 Share on other sites More sharing options...
flyhoney Posted December 24, 2008 Share Posted December 24, 2008 Just so you know, you can format it like this: <?php // Page name $page = "index"; // Path to the templates folder $path = "templates"; include("{$path}/head-tag1-tpl.php"); include("{$path}/{$page}-meta-tpl.php"); include("{$path}/head-tag2-tpl.php"); include("{$path}/header-tpl.php"); include("{$path}/{$page}-body-tpl.php"); include("{$path}/footer-tpl.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/138304-included-page-name-variable/#findComment-723137 Share on other sites More sharing options...
Lee Posted December 24, 2008 Author Share Posted December 24, 2008 It took me a while to spot the difference there, just one opening & closing php tag. That's cool thanks flyhoney! Quote Link to comment https://forums.phpfreaks.com/topic/138304-included-page-name-variable/#findComment-723139 Share on other sites More sharing options...
Lee Posted December 24, 2008 Author Share Posted December 24, 2008 I just hit a problem. Although I now have a variable to adjust the path to the template files, the image paths also change now. I'm a bit confused how to get around this. I thought perhaps to make a variable such as $imgpath = "images". Then change it to imgpathe = "../images" for sub-directories e.t.c. But how to I code that into the image html? I tried this. <img src="<?php $imgpath ?>/bg_head.jpg" alt="Header Pic" width="836" height="307" /> Quote Link to comment https://forums.phpfreaks.com/topic/138304-included-page-name-variable/#findComment-723177 Share on other sites More sharing options...
flyhoney Posted December 24, 2008 Share Posted December 24, 2008 echo <img src="<?php echo $imgpath ?>/bg_head.jpg" alt="Header Pic" width="836" height="307" /> Quote Link to comment https://forums.phpfreaks.com/topic/138304-included-page-name-variable/#findComment-723179 Share on other sites More sharing options...
Lee Posted December 25, 2008 Author Share Posted December 25, 2008 Thanks again flyhoney. I think I need to work out a bit better how to setup these templates. I want to have pages in sub-directories, mainly so that I can direct people easier, for example go to mysite.com/examples or mysite.com/news. However, doing this means that the templates will not work because they are one level above so the images & css will not be called for. I could put copies of them in the same directory, but that will just defeat the object of having an overall header, footer & page body. I guess I could just create these sub-directories with an index page, then just reirect after 0 secs to the relevant page in the root folder. I wonder if there could be a better way? Quote Link to comment https://forums.phpfreaks.com/topic/138304-included-page-name-variable/#findComment-723733 Share on other sites More sharing options...
Lee Posted January 5, 2009 Author Share Posted January 5, 2009 Can anyone suggest a way I might be able to continue with my templates without having to make a 2nd set for pages one level down or having to do the re-direct thing? I'm a bit stumped. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/138304-included-page-name-variable/#findComment-729815 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.