Template class, include header and footer

Asked

Viewed 609 times

0

Hello, I had already posted here some time ago about my templates class, but I decided to a modified one, because every time I want to edit a menu for example, I have to edit in all my files.

This is my class

class Template {

    private $_template;

    private $_assign = array();

    public function set($file) {
        $path = './templates/' . DEFAULT_THEME . '/header.tpl.php';
        $path = './templates/' . DEFAULT_THEME . '/' . $file . '.tpl.php';
        $path = './templates/' . DEFAULT_THEME . '/footer.tpl.php';

        if (!empty($path)) {
            if (file_exists($path)) {
                $this->_template = file_get_contents($header);
                $this->_template = file_get_contents($path);
                $this->_template = file_get_contents($footer);
            } else {
                die("Template error: file not found in: {$path}.");
            }
        }
    }

    public function assign($string_search, $string_replace) {
        if (!empty($string_search)) {
            $this->_assign[strtoupper($string_search)] = $string_replace;
        }
    }

    public function display() {
        if (count($this->_assign) > 0) {
            foreach ($this->_assign as $key => $value) {
                $this->_template = str_replace('{' . $key . '}', $value, $this->_template);
            }
        }

        return $this->_template;
    }
}

And I use it like this:

<?php

require './includes/configs/Configs.php';
require './includes/autoload/autoload.php';

$template = new Template();

$template->set('home');

$template->assign('home', 'this is home');

echo $template->display();

Note that I am trying to include my header and footer in the template class:

public function set($file) {
    $path = './templates/' . DEFAULT_THEME . '/header.tpl.php';
    $path = './templates/' . DEFAULT_THEME . '/' . $file . '.tpl.php';
    $path = './templates/' . DEFAULT_THEME . '/footer.tpl.php';

    if (!empty($path)) {
        if (file_exists($path)) {
            $this->_template = file_get_contents($header);
            $this->_template = file_get_contents($path);
            $this->_template = file_get_contents($footer);
        } else {
            die("Template error: file not found in: {$path}.");
        }
    }
}

However it always adds the last one that is my footer, there is some way to create a function or add without having to use the require? 'Cause when I try to use the require he doesn’t change my strings str_replace();

Well I tried to explain, I await answers.

1 answer

2


That stretch:

public function set($file) {
    $path = './templates/' . DEFAULT_THEME . '/header.tpl.php';
    $path = './templates/' . DEFAULT_THEME . '/' . $file . '.tpl.php';
    $path = './templates/' . DEFAULT_THEME . '/footer.tpl.php';

    if (!empty($path)) {
        if (file_exists($path)) {
            $this->_template = file_get_contents($header);
            $this->_template = file_get_contents($path);
            $this->_template = file_get_contents($footer);
        } else {
            die("Template error: file not found in: {$path}.");
        }
    }
}

do so

public function set($file) {

    $this->_template = '';

    $path = './templates/' . DEFAULT_THEME . '/header.tpl.php';

    if (file_exists($path)) {
        $this->_template .= file_get_contents($path);
    } else {
        die("Template error: file not found in: {$path}.");
    }

    $path = './templates/' . DEFAULT_THEME . '/' . $file . '.tpl.php';

    if (file_exists($path)) {
        $this->_template .= file_get_contents($path);
    } else {
        die("Template error: file not found in: {$path}.");
    }

    $path = './templates/' . DEFAULT_THEME . '/footer.tpl.php';

    if (file_exists($path)) {
        $this->_template .= file_get_contents($path);
    } else {
        die("Template error: file not found in: {$path}.");
    }

}

If you want to control options to include or not the header and footer, you can do something like this

public function set($file, $header = true, $footer = true) {

    $this->_template = '';

    if (!empty($header)) {
        $path = './templates/' . DEFAULT_THEME . '/header.tpl.php';
        if (file_exists($path)) {
            $this->_template .= file_get_contents($path);
        } else {
            die("Template error: file not found in: {$path}.");
        }
    }

    $path = './templates/' . DEFAULT_THEME . '/' . $file . '.tpl.php';
    if (file_exists($path)) {
        $this->_template .= file_get_contents($path);
    } else {
        die("Template error: file not found in: {$path}.");
    }

    if (!empty($footer)) {
        $path = './templates/' . DEFAULT_THEME . '/footer.tpl.php';
        if (file_exists($path)) {
            $this->_template .= file_get_contents($path);
        } else {
            die("Template error: file not found in: {$path}.");
        }
    }

}

To use, just assign the parameters at the time you instantiate the Template class

$template = new Template();

// Exemplos
// NÃO inclui o header e  INCLUI o footer:
$template->set('home', false, true);
// também pode chamar assim:
$template->set('home', false);

// NÃO inclui o header e o footer:
$template->set('home', false, false);

// INCLUI o header e NÃO inclui o footer:
$template->set('home', true, false);

// Inclui ambos:
$template->set('home');
// também pode chamar assim:
$template->set('home', true, true);
  • Thanks for your comment, it worked the same way I had done... is always catching the last one that is my footer.

  • 1

    Ah, sorry, I forgot to concatenate. I corrected the answer.

  • Opa functioned, now all my templates are required to contain a header and a footer. I’ll mark it as settled, thank you, but just so I don’t want to slack off on you, would you have a logic of how to create a function to do that? for example if I want to separate my Sidebar from the layout and need to include only include using the function. Note: The reward can only be given in 22 hours :/

  • 1

    I thought you were going to ask this about the header and footer. There are several ways to solve. I will show a way to implement without modifying much that already has.

  • Opa, perfect! I will save in an array, so I will be able to include other things besides header and footer, let’s see if my logic will work... For example, divide the layout into parts, they being the files header.tpl.php, menu.tpl.php, footer.tpl.php and etc every time you have additional parts just activate in the array...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.