Jump to content

Included page name variable


Lee

Recommended Posts

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"); ?>

Link to comment
Share on other sites

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"); ?>

Link to comment
Share on other sites

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! :D

Link to comment
Share on other sites

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"); ?>

Link to comment
Share on other sites

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");

?>

Link to comment
Share on other sites

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" />

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.