Yaw Ansong Snr
Yaw Ansong Snr

Reputation: 517

dynamically inserting meta description into php page

ok so I've created this website and want to convert it to php just for fun. The website structure looks like any 'normal' web structure. like this:-

<html xmlns="http://www.w3.org/1999/xhtml">


 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

<body class="fish">
</body>
</html>

ok so i included from the head to the beginning of the body tag in header.php file. so header.php looks like this:- `

 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

<body class="fish">`

now here is the problem. Each page should have it's own title, body class! and each page will also obviously have it's own meta description and content. How will I accomplish this guys? I was think of creating a function that base the meta description and body class on the page title. But is there a smatter way to accomplish this? Thanks

Upvotes: 2

Views: 5205

Answers (3)

Gimli
Gimli

Reputation: 488

Either use a template engine or a MVC framework (such as CakePHP or CodeIgniter) which have template engines already incorporated in them.

Upvotes: 1

ryan0319
ryan0319

Reputation: 412

You should be creating a template to do this if it will be dynamic. You have many options on how you with to pass the data, whether it be a database, an object, array, etc. It is really tough to generate the data based on the page title, unless you are using a very persistent format to title each page.

<head>
    <meta property="og:title" content="<?= $values['title'] ?>" />
    <meta property="og:type" content="website" />
    <meta property="og:url" content="<?= $values['url'] ?>" />
    <meta property="og:image" content="<?= $values['image'] ?>/>
    <meta property="og:site_name" content="<?= values['name'] ?>"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="ROBOTS" content="NOODP">
    <link rel="icon" type="image/png" href="<?= $values['image'] ?>" />
    <title><?= $values['title'] ?></title>
    <? if(isset($values['css'])) : ?>
    <? foreach($values['css'] as $css) : ?>
        <link href="/css<?= $css['data'] ?>" rel="stylesheet" type="text/css" />
    <? endforeach ?>
    <? endif ?>
    <? if(isset($values['js'])) : ?>
    <? foreach($values['js'] as $js) : ?>
        <script src="/js<?= $js['data'] ?>" type="text/javascript"></script>
    <? endforeach ?>
    <? endif ?>
</head>

Upvotes: 0

BenM
BenM

Reputation: 53198

Inside your header.php do something like this:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title><?php echo $_tpl['title'] ?></title>
    <meta name="description" content="<?php echo $_tpl['meta_desc'] ?>">
</head>

<body class="<?php echo $_tpl['body_class'] ?>">

On your page, before you use include('header.php'), define the vars as follows:

$_tpl = array();
$_tpl['title'] = 'My Title';
$_tpl['meta_desc'] = 'My meta description.';
$_tpl['body_class'] = 'fish';

As others have said though, don't re-invent the wheel. You'd be better to investigate some of the already-established templating engines for PHP:

Upvotes: 0

Related Questions