1
\$\begingroup\$

I tried making a class that handles my header and footer of my site. It looks like a mess, because I like to preserve the indentations as is.

But I'm wondering if there maybe is an even cleaner or better way of writing this class?

<?php
class HeadFoot {
    protected $title,
              $scripts;

    public function __construct($title, $scripts) {
        $this->title = $title;
        $this->scripts = $scripts;
    }

    public function getHeader() {
        if($this->title) {
$header = <<< EOD
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>$this->title - Domain</title>
        <link rel="shortcut icon" href="/favicon.ico">
        <link rel="stylesheet" href="/stylesheets/init.css">
        <link rel="stylesheet" href="/stylesheets/layout.css">

EOD;

if($this->scripts) {
    foreach($this->scripts as $script) {
        $header .= str_repeat(' ', 8) . $script . "\n";
    }
}

$header .= <<< EOD
    </head>
    <body>
        <div id="topbar"></div>

        <div id="container">
            <div id="header">
                <div class="wrapper">
                    <h1 id="site-name"><a href="/">Domain</a></h1>
                </div>
            </div>


EOD;

$header .= Navigation::get();
$header .= str_repeat(' ', 12) . "\n";

            return $header;
        }

        return false;
    }

    public function getFooter($ads = null) {
        if($ads) {
            return
'            <div id="footer">
                <p>Copyright © 2014 - Domain.net</p>
                <p><a href="#" title="Terms of Service">Terms of Service</a> / <a href="#" title="Privacy Policy">Privacy Policy</a></p>
            </div>
        </div>
    </body>
</html>';
        }

        return
'            <div id="advertisement">
                <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                <ins class="adsbygoogle"
                     style="display: inline-block; width: 468px; height: 60px; vertical-align: top;"
                     data-ad-client="ca-pub-8849502800889803"
                     data-ad-slot="2598909127"></ins>
                <script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
            </div>

            <div id="footer">
                <p>Copyright © 2014 - Domain.net</p>
                <p><a href="#" title="Terms of Service">Terms of Service</a> / <a href="#" title="Privacy Policy">Privacy Policy</a></p>
            </div>
        </div>
    </body>
</html>';
    }
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Separation of Concerns

You should implement Separation of Concerns. In your case, don't mix logic with your view. Follow basic MVC, where M (model) is the data source, V (view) is where your HTML is, and C (controller), where all your logic is. In this case, put your HTML in one file, the logic in the other.

Templating

One thing that messes up development is when logic is mixed to the template. It makes the template messy and hard to maintain and visualize. I suggest you take a look at the PHP implementation of Mustache templates. In your controller, simply make a function that loads the template to a variable, feeds it to Mustache, put some data in and done.

Inheritance

One use of inheritance is to hide utility functions. For instance, you can create a View class, where your future views inherit from.

End Result

You can simply do something as simple as this in the controller!

public function index(){
    $myView = new View('path/to/template');     // Create a view
    $myDataSource = new Model('MODEL_NAME');    // Create a model
    $theData = myDataSource->getData();         // Grab data
    $theResultHTML = myView->render($theData);  // Render data to template and print
}
\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.