最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - When including your header, in PHP, how do you add things? - Stack Overflow

programmeradmin3浏览0评论

I am building one of my first PHP websites, and just playing around with it. I am going to be using PHP includes, so I only have to change my meta stuff once. Currently, I have everything from the doctype to the end of the </head>.

A friend is saying I should not include the </head> on the header.php, but instead, include it on each page, so I can add page specific stuff. Is that a good way of doing it?

Currently, for stuff like title, I was doing

<title><?php echo $page_title; ?>

and then on the top of each page, I was doing

<?php $page_title = 'Blah'; ?> 

How would I add a page specific Javascript file if this is the way I'm doing this?

I am building one of my first PHP websites, and just playing around with it. I am going to be using PHP includes, so I only have to change my meta stuff once. Currently, I have everything from the doctype to the end of the </head>.

A friend is saying I should not include the </head> on the header.php, but instead, include it on each page, so I can add page specific stuff. Is that a good way of doing it?

Currently, for stuff like title, I was doing

<title><?php echo $page_title; ?>

and then on the top of each page, I was doing

<?php $page_title = 'Blah'; ?> 

How would I add a page specific Javascript file if this is the way I'm doing this?

Share Improve this question edited Jul 4, 2012 at 20:07 Rob W 349k87 gold badges807 silver badges682 bronze badges asked Jul 4, 2012 at 19:43 DavidDavid 7341 gold badge6 silver badges13 bronze badges 3
  • Yeah I think that's the way to go for best SEO you want to have different titles for different pages – elclanrs Commented Jul 4, 2012 at 19:49
  • Alright - quick other question to this, what if I want to include multiple pages on 1 index.php file. Is there a good way of doing it then? – David Commented Jul 4, 2012 at 20:43
  • See my updated answer. With this you could include different pages regarding the requested route ing from your .htaccess. – insertusernamehere Commented Jul 4, 2012 at 21:18
Add a ment  | 

4 Answers 4

Reset to default 6

What's going on here is you're trying to inject logic into your templates, which isn't necessarily wrong, but it produces more confusing and harder-to-maintain code. This issue isn't only with your <title> tags, but will keep ing up as your pages get more and more dynamic and plex (dynamic navigation, page-specific layouts, etc.).

An MVC approach solves this problem perfectly. MVC consists of models, which talk to your data sources (MySQL, Redis, whatever) and handle your logic; views, which render HTML; and controllers, which are sort of the glue between models and views. Each request a user makes is eventually routed to a controller and then an action method in your controller (for example: /users/login might map to the User controller and the login action).

You would set your page title (or any other meta information) in the action method in your controller, which knows what the request is but is invoked before the view is rendered:

$request->setTitle('Home page');

And then in your view, simply render it:

<title><?php echo $request->getTitle(); ?></title>

If you're just starting PHP, it's a great time to learn MVC because it'll get you into some good habits that affect not only your PHP development but any other development as well.

I remend you check out CodeIgniter for its simplicity and excellent documentation. I used CodeIgniter for a while, but ended up writing my own framework that fitted my needs better.

I think you should do some logic first, then render it. Something like this:

<?php

$page = array(
    'title' => 'Title',
    'js' => array(
        'jquery.min.js',
    ),
    'copyright' => 'Copyright 2012',
);

if ( $needed ) {
    $page[ 'js' ][] = 'some-other.js';
}

include( 'header.php' );
include( 'content.php' );
include( 'footer.php' );


// header.php
<html>
    <head>
        <title><?= $page[ 'title' ]; ?></title>

        <? foreach ( $page[ 'js' ] as $js ) { ?>
            <script src="<?= $js"></script>
        <? } ?>

    </head>

I don't know, how you build up your pages. But on a simple site, you could use an associative array like this:

$pages = array(
    'home' => array(
        'title' => 'Home',
        'metaDescription' => 'This is the home of my website',
        'metaKeywords' => 'A,B,C',
        'scripts' => '<script src="src/js/.jquery/1.7/jquery-1.7.min.js"></script>',
        'content' => 'home.phtml'
    ),
    'page_1' => …
);

And than you can recall it like this:

<head>
    […]
    <title><?php print $pages['home']['title']; ?></title>
    […]
    <?php print $pages['home']['scripts']; ?>
    […]
</head>
<body>
    <?php require '/pathToIncludes/' . $pages['home']['content]; ?> 
</body>

But I would only remend this for a site with just a bunch of pages.

Here is how I solve this problem:

  • start.php connects to my database, includes my classes, sets all my default meta values into a $page object and starts $_SESSION
  • header.php Spits out all my html top matter (doctype, <head> etc.) based on the values in $page
  • top.php Includes start.php and header.php

For most files, the default values are fine. I just include top.php. For special cases where I need special values, my page looks like this:

<?php 
    include("start.php");
    $page->title = "My special title";
    include("header.php");
    // The rest of my content
?>

This has the added advantage that pages can have access to $_SESSION and other custom classes, and use that information to modify the header without getting the "header already sent" error. For example, you can validate some user input and redirect them using location('header: foo.php'); or change the title depending on their login status.

For page specific javascript includes, I would create an array of urls to include $page->javascript and then put something like: $page->javascript[] = "http://example./js/myScript.js"; between start.php and header.php

发布评论

评论列表(0)

  1. 暂无评论