Create loop in templates with php

1

I'm having a hard time creating an algorithm to implement using loops in a simple template class.

I have my class below, it gets an HTML file, looks for specific strings that are in this file from values of an array and replaces it with > string from a variable, finally returns the changed HTML code as print on the screen. It works fine, except when I need loops .

Note: I want to separate all my PHP code from PHP

Template Class

<?php

class Template {

private $file;
private $vars;

public function __construct($file = "") {

    $this->vars = array();
    $this->load($file);
}

public function load($file){

    if(file_exists($file)){

        if(file_get_contents($file)){

            $this->file = file_get_contents($file);

            return true;
        }
        else{

            return die("Error: Could not load file \"{$file}.\"");
        }
    }
    else{

        return die("Error: file \"{$file}\" not found.");
    }
}

public function show(){

    if($this->file){

        $output = $this->file;

        foreach ($this->vars as $varName => $varValue) {

            $tagToReplace = "{{$varName}}";

            $output = str_replace($tagToReplace, $varValue, $output);
        }

        printf($output);
    }
    else {
        return die("Error: template file is empty.");
    }
}

public function setVar($varName, $varValue){

    if(!empty($varName) && !empty($varValue)){

        $this->vars[$varName] = $varValue;
    }
}
}
?>

Example use index.php

<?php

$template   = new Template("index.template.html");

$template->setVar("page_title", "Página Inicial");
$template->setVar("msg", "Bem vindo!");

$template->show();
?>

File index.template.html

<!DOCTYPE html>
<html>
    <head>
        <title>{page_title}</title>
    </head>
    <body>
        <h1>{msg}</h1>
    </body>
</html>
    
asked by anonymous 16.05.2017 / 13:22

2 answers

0

I do not quite know how you want it to be printed at the end. But for you to use all the variables in the array you should treat them separately from the code that makes the HTML change. An example:

    // ... código anterior

    $tagToReplace = "{{$varName}}";
    $newValue = ""; // criei essa variável para ser usada depois

    foreach ($this->vars as $varName => $varValue) {

        $newValue .= $varValue." ";

    }
    // aqui ele altera com a variável $newValue
    $output = str_replace($tagToReplace, $newValue, $output); 

    // restante do código ...

I believe it will work. Any errors comment here below. Hugs!

    
17.05.2017 / 00:26
-1

What you have done so far is very simple, but obviously if you want to get something more generic or complete you will have to go much further. This way, you only bumped into the first problem (loops), but you will run into conditionals, and several other problems. So there are projects where other people have thought about, they've chewed the community into very complete, robust solutions that do exactly what they're trying to do. An example is Smarty: link

The documentation is great because it is a very complete solution. My suggestion is to research on PHP Template Engines on google and choose the one that best suits you.

    
16.05.2017 / 13:54