1

I just go to the point.

Nvm need to add more text to much code..

Trying to load a Template with php inside it but php prints in html instead.

Init.php

class Init {

    public static $ROOT = '';
    public static $TEMPLATE = '';
    public static $SERVICE = ''; 

    public static function start() {

        // Init Paths
        Init::$ROOT     = str_replace("\\", "/", __DIR__);
        Init::$TEMPLATE = Init::$ROOT . "/Template/";
        Init::$SERVICE  = Init::$ROOT . "/Service/";

        // Init Template.php class
        require_once(Init::$SERVICE . "Template.php");

        // Load template Top.php
        $top = new Template(Init::$TEMPLATE . "Layout/Top.php");
        echo $top->load(); // Show Top.php
    }
}

Top.php

<!DOCTYPE html>
<html>
    <?
        // Load template Head.php
        $head = new Template(Init::$TEMPLATE . "Layout/Head.php");
        $head->set("TITLE", "Dashboard"); //Set [@TITLE] to Dashboard 
        $head->load(); // Show Head.php
    ?>
</html>

Head.php

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>[@TITLE] | cwEye</title> <!-- [@TITLE] will be Dashboard-->

    <?
        echo "Hello"; // ERROR ->  This will print <? echo"Hello"; ?> in my page
    ?>
</head>

Template.php

<?
class Template {

    protected $file;
    protected $values = array();

    private static $templateFile = null;

    public function __construct($file) {
        $this->file = $file;
    }

    public function set($key, $value) {
        $this->values[$key] = $value;
    }

    // This code works but it will not load php inside
    public function load() {
        if (!file_exists($this->file)) return "Error loading template file ($this->file).";

          ob_start();
        include_once($this->file);
        $data = ob_get_clean();

        foreach ($this->values as $key => $value) {
            echo str_replace("[@$key]", $value, $data);
        }
        if(count($this->values) == 0) echo $data;
    }
}
?>

Ive played with allot of functions to make it but it does not work... It just prints the php in html.

Tried with

ob_start();
include_once(FILE);
$data = ob_get_clean();
8803286
  • 81
  • 2
  • 10

2 Answers2

1

Don't use short tags like <? or <?=, use <?php instead. You probably have your short_open_tag set to false in php.ini. If you are using PHP 7 then you should know short tags were removed completely and wont work anymore.

Linek
  • 1,353
  • 10
  • 20
1

In head.php use the full tag. Change to

<?php echo "hello"; ?>