Template class, include header and footer

0

Hello, I had posted here for some time about my template class, but I decided to modify it, because every time I want to edit a menu, for example, I have to edit 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();

Notice that I'm 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}.");
        }
    }
}

But it always adds the last one that is my footer, is there any way to create a function or add it without having to use require ? When I try to use require it does not change my strings str_replace() ;

Well, I've tried to explain, I'm waiting for answers.

    
asked by anonymous 10.03.2016 / 20:13

1 answer

2

This excerpt:

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 whether or not to include 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 moment that instantiates the class Template

$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);
    
12.03.2016 / 23:13