简体   繁体   English

在PHP中包含标题时,如何添加内容?

[英]When including your header, in PHP, how do you add things?

I am building one of my first PHP websites, and just playing around with it. 我正在构建我的第一个PHP网站之一,并且只是玩它。 I am going to be using PHP includes, so I only have to change my meta stuff once. 我将使用PHP包含,所以我只需要改变我的元素一次。 Currently, I have everything from the doctype to the end of the </head> . 目前,我拥有从doctype到</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. 一位朋友说我不应该在header.php中包含</head> ,而是将其包含在每个页面上,这样我就可以添加特定于页面的内容。 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? 如果这是我正在这样做的方式,我将如何添加页面特定的Javascript文件?

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 coming up as your pages get more and more dynamic and complex (dynamic navigation, page-specific layouts, etc.). 这个问题不仅出现在您的<title>标签上,而且会随着您的网页变得越来越动态和复杂(动态导航,特定于页面的布局等)而不断出现。

An MVC approach solves this problem perfectly. MVC方法完美地解决了这个问题。 MVC consists of models, which talk to your data sources (MySQL, Redis, whatever) and handle your logic; MVC由模型组成,它们与您的数据源(MySQL,Redis,无论如何)交谈并处理您的逻辑; views, which render HTML; 视图,呈现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). 用户做出的每个请求最终都会路由到控制器,然后是控制器中的操作方法(例如: /users/login可能会映射到User控制器和login操作)。

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: 您可以在控制器的action方法中设置页面标题(或任何其他元信息),它知道请求是什么,但在呈现视图之前调用:

$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. 如果您刚刚开始使用PHP,那么这是学习MVC的好时机,因为它会让您养成一些不仅影响您的PHP开发而且影响任何其他开发的良好习惯。

I recommend you check out CodeIgniter for its simplicity and excellent documentation. 我建议您查看CodeIgniter的简单性和出色的文档。 I used CodeIgniter for a while, but ended up writing my own framework that fitted my needs better. 我使用CodeIgniter一段时间了,但最终编写了我自己的框架 ,更符合我的需求。

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/com.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 recommend this for a site with just a bunch of pages. 但我只推荐这个只有一堆页面的网站。

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>

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 start.php连接到我的数据库,包括我的类,将所有默认元值设置为$page对象并启动$_SESSION
  • header.php Spits out all my html top matter (doctype, <head> etc.) based on the values in $page header.php根据$page的值吐出所有html顶级内容(doctype, <head>等)
  • top.php Includes start.php and header.php top.php包含start.phpheader.php

For most files, the default values are fine. 对于大多数文件,默认值都可以。 I just include top.php . 我只包括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. 这样做的另一个好处是页面可以访问$ _SESSION和其他自定义类,并使用该信息修改标题而不会收到“已发送标头”错误。 For example, you can validate some user input and redirect them using location('header: foo.php'); 例如,您可以验证一些用户输入并使用location('header: foo.php');重定向它们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.com/js/myScript.js"; 对于特定于页面的javascript包含,我会创建一个包含$page->javascript的url数组,然后输入类似: $page->javascript[] = "http://example.com/js/myScript.js"; between start.php and header.php start.phpheader.php之间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM