1

Hello everyone.

I've got a small problem. Instead of making a $content variable and putting in the whole content there, I would like to set the page up like this (below), though the problem is that the php variables that is placed at the end (title, keywords, description, page), aren't working even though its in the index.php.

Question: How can I make the variables work without making a few head_template.php with the variables filled out already?

index.php:

<!DOCTYPE html>
<html class="html" lang="en">


<?php require_once "head_template.php"; ?>

<body class="<?php echo $page; ?>">

    <?php require_once "navigation_menu_template.php"; ?>

    <?php require_once "load_template.php"; ?>

    <h1>Content</h1>

    <?php require_once "cookies_script_template.php"; ?>

    <?php require_once "footer_template.php"; ?>

</body>

</html>


<?php
    $title = "QCG | Homepage";
    $keywords = "QCG, Homepage";
    $description = "QCG | Homepage - Description";
    $page = "homepage";
?>

head_template.php:

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="UTF-8">
    <meta name="description" content="<?php echo $description; ?>">
    <meta name="keywords" content="<?php echo $keywords; ?>">
    <title><?php echo $title; ?></title>
    <link rel="stylesheet" type="text/css" href="css/css.css">
</head>

1 Answer 1

2

You are setting them after they are used. If you put that block at the top of your page it will work.

<!DOCTYPE html>
<html class="html" lang="en">

<?php
    $title = "QCG | Homepage";
    $keywords = "QCG, Homepage";
    $description = "QCG | Homepage - Description";
    $page = "homepage";
?>

<?php require_once "head_template.php"; ?>

<body class="<?php echo $page; ?>">

    <?php require_once "navigation_menu_template.php"; ?>

    <?php require_once "load_template.php"; ?>

    <h1>Content</h1>

    <?php require_once "cookies_script_template.php"; ?>

    <?php require_once "footer_template.php"; ?>

</body>

</html>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.