Jump to content

[SOLVED] APPLY TITLE


jaymc

Recommended Posts

My page is laid out like this

 

<html><head>
<title><? $title_should_go_here ?></title>
</head>

<body>

SOME STATIC CONTENT HERE

<div> <? include("$page.php"); ?></div>

</body>
</html>

The problem is, I can only really get the correct title from $page.php, but <title></title> is called before the include

 

Im just thinking of the best way to sort this out?

 

By the way $page.php has php code within, that must be parsed, so the likes of $content = file_get_contents("$page.php"); is no good

Link to comment
Share on other sites

do it the other way around.

Have each page include the header and footer. That way you can store the title ina variable on each page:

<? include("header.pho"); ?>
$title = 'this is a title..';
<? include("footer.pho"); ?>

 

then in header.php:

<html><head>
<title><? echo $title; ?></title>
</head>

<body>

SOME STATIC CONTENT HERE

<div>

 

and footer.php:

</div>

</body>
</html>

Link to comment
Share on other sites

Define the dynamic content within a function, or something similar.

 

An example of $page.php

 


$pageTitle = 'Photos';

# Make sure another page hasnt already be included
if ( !function_exists( 'displayPage' ) )
    function displayPage () {

        # The regular content of $page.php goes here #

    }
else
    echo 'Display function already defined!';

 

Then on your main index page, use something like this...

 

<?php
if ( file_exists( $page . '.php' ) )
    include( $page . '.php' );
?>
<html><head>
<title><?php echo ( !empty($pageTitle) ? $pageTitle : 'Default Title' ) ?></title>
</head>

<body>

SOME STATIC CONTENT HERE

<div> <?php if ( function_exists( 'displayPage' ) ) displayPage(); ?></div>

</body>
</html>

 

As it becomes more complex and there's more variables being referenced, use classes :)

Link to comment
Share on other sites

Define the dynamic content within a function, or something similar.

 

An example of $page.php

 


$pageTitle = 'Photos';

# Make sure another page hasnt already be included
if ( !function_exists( 'displayPage' ) )
    function displayPage () {

        # The regular content of $page.php goes here #

    }
else
    echo 'Display function already defined!';

 

Then on your main index page, use something like this...

 

<?php
if ( file_exists( $page . '.php' ) )
    include( $page . '.php' );
?>
<html><head>
<title><?php echo ( !empty($pageTitle) ? $pageTitle : 'Default Title' ) ?></title>
</head>

<body>

SOME STATIC CONTENT HERE

<div> <?php if ( function_exists( 'displayPage' ) ) displayPage(); ?></div>

</body>
</html>

 

As it becomes more complex and there's more variables being referenced, use classes :)

 

That did not work, because you cant use a variable outside of a function, when the variable was set within the function

 

Unless there is a way to set the variable so it can be used GLOBALLY outside of the function?

Link to comment
Share on other sites

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.