开发者

How to change HTML title in PHP without breaking the XHTML markup validation?

Here is the structure of the web site:

PHP index file

//my class for analyzing the PHP query
$parameter = new LoadParameters();

//what this does is it accesses the database
//and according to the query, figures out what should be
//loaded on the page
//some of the things it sets are:
//    $parameter->design -  PHP file which contains the design
//                          HTML code of the page
//    $parameter->content - Different PHP file which should be loaded
//                          inside the design file
$parameter->mysqlGetInfo($_SERVER['QUERY_STRING']);

//load the design file
include($parameter->design);

PHP design file

Just the generic structure. Obviously it has a lot more design elements.

<html>
  <head>
     ...
  </head>
  <body>
    <?php
       //this loads the content into the design page
       include($parameter->content);
    ?>
  </body>
</html>

Question

So here is the problem I experience. The $parameter->content file is a dynam开发者_开发问答ic PHP file, meaning the content also changes according to the query.

For instance if I have a image pages with queries like ?img=1 and ?img=2, my LoadParameter class will only look at the img part of the query and will know that the content of the page should be image.php. image.php however will look at the query again and figure out exactly what image to load.

This causes issues for me because I want to have a different <title></title> for different images. So my solution was just to set the <title></title> element in the content page. This works but it breaks the XHTML markup validation at W3C because it makes the structure of the site to be the following:

<html>
  <head>
     ...
  </head>
  <body>
    ...
    <title>sometitle</title>
    ...
  </body>
</html>

And having <title></title> within <body></body> is not allowed.

So how can I change the title without breaking the XHTML markup validation?

Note: I can't use javascript because then Search engines would not be able to see the title of the page. I need to do it directly in PHP.

Thanx in advance.


why not do a second include to perform the title in the proper place?

<html>
    <head>
     <?php
       inlcude($parameter->title);
     ?>
      ...
    </head>
    <body>
    <?php
    //this loads the content into the design page
       include($parameter->content);
    ?>
    </body>
</html>


Can't you just change the PHP code so that you can do something like:

<html>
  <head>
     <title><? print($parameter->title); ?></title>
  </head>
  <body>
    <?php
       //this loads the content into the design page
       include($parameter->content);
    ?>
  </body>
</html>


I'd move all of the <head> code into a 'common function' called something like html_head($title) and then have it put the title where it belongs.

Then simply call that function from within the pages and it's fixed.

Don't forget to include the <body> tag in that function, otherwise it won't work!

Elaborating ;)

function html_head($title) {?>
    <html>
        <head>
            <title><?=$title?></title>
            <!-- Put whatever you want... here! -->
        </head>
        <body>
<?}

Then in $parameter->content, call html_head("Title")


It would be easier if $parameter->content could be included without displaying its HTML code, but instead have a $parameter->display (or similar) function that displays the HTML code. That way, you can include the PHP code at the beginning of the file and not worry about being unable to access the title.

<?php
  require_once($parameter->content);
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="UTF-8" /> 
    <title><?php echo $parameter->title; ?></title>
  </head>
  <body>
    <?php
       echo $parameter->display;
    ?>
  </body>
</html>


This is how I solved the issue.

I changed the PHP design to something like:

//get the content PHP file
//inside the file I set the following variables
//which are used below:
//$parameter->title - the string which contains the title
//$parameter->html - the string which contains the HTML content
include($parameter->content);

//string which will contain the html code of the whole page
$html = <<<EndHere
<html>
  <head>
      <title>
EndHere;
//add title
$html .= $parameter->title;
$html .= <<<EndHere
      </title>
  </head>
  <body>
EndHere;
//add the content of the page
$html .= $parameter->html;
$html .= <<<EndHere
  </body>
</html>
EndHere;

//output the html
echo $html;

And here is the basic structure of the Content PHP file. Since the only page which can possibly include the file is the my design page, I can reference $parameter in it.

//set the title
$parameter->title = "sometitle";

//set the content HTML
$parameter->html = "some HTML here";

It's not a very clean solution but it works fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜