Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8914393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:42:33+00:00 2026-06-15T04:42:33+00:00

So, I have a template engine I made and I want to replace {pageBody}

  • 0

So, I have a template engine I made and I want to replace {pageBody} with contents from a PHP file, when I do it using the template engine it does not execute the PHP, rather it displays it in view source option.

TEMPLATE.PHP

<?php
class TemplateLibrary {
    public $output;
    public $file;
    public $values = array();

    public function __construct($file) {
        $this->file = $file;
        $this->file .= '.tpl';
        $this->output = file_get_contents('templates/'.$this->file);
    }

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

    public function output() {
        foreach($this->values as $key => $value)
        {
            $ttr = "{$key}";
            $this->output = str_replace($ttr, $value, $this->output);
        }
        return $this->output;
    }
}

index.tpl
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>{page_title}</title>
{page_style}
</head>

<body>
{page_header}
{page_body}
{page_footer}
</body>
</html>

HTML replace works fine, but PHP does not. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:42:34+00:00Added an answer on June 15, 2026 at 4:42 am

    You are rather scattered with your files and the list is incomplete. I’ve listed all the files you’ve provided so far below.

    @Jonathon above is correct, that you will need to use output buffering to capture the output of the PHP file and include() the file (so it gets executed) instead of using file_get_contents() (which does not execute the file).

    [edit] I re-created all these files in my local environment and confirmed that @Jonathon’s suggestion worked perfectly. I’ve updated dev/replacements.php to include the suggested code.

    Additionally, I added two more functions to your TemplateLibrary class : replaceFile($key, $filename) that does the file_get_contents($filename) so that you don’t have to repeat it so often, and replacePhp($key, $filename) that performs an include() while capturing the output, so you can encapsulate the complexities of including a PHP file.

    Good Luck!

    main.php

    <?php
    require_once 'dev/dev.class.php';
    require_once 'dev/templatelibrary.php';
    $dev = new dev('netnoobz-billing');
    $dev->loadLib('JS', 'js', 'jquery');
    
    // template library and required files
    $template = new TemplateLibrary('index');
    require_once 'dev/replacements.php';
    
    
    echo $template->output();
    

    dev/templatelibrary.php

    <?php
    class TemplateLibrary {
        public $output;
        public $file;
        public $values = array();
    
        public function __construct($file) {
            $this->file = $file;
            $this->file .= '.tpl';
            $this->output = file_get_contents('templates/'.$this->file);
        }
    
        public function replace($key, $value)
        {
            $this->values[$key] = $value;
        }
    
        public function replaceFile($key, $filename)
        {
            $this->values[$key] = file_get_contents($filename);
        }
    
        public function replacePhp($key, $filename)
        {
    ob_start();
    include($filename);
    $data = ob_get_clean();
            $this->values[$key] = $data;
        }
    
        public function output() {
            foreach($this->values as $key => $value)
            {
                $ttr = "{$key}";
                $this->output = str_replace($ttr, $value, $this->output);
            }
            return $this->output;
        }
    }
    

    index.tpl

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>{page_title}</title>
    {page_style}
    </head>
    
    <body>
    {page_header}
    {page_body}
    {page_footer}
    </body>
    </html>
    

    replacements.php

    <?php
    $configStyleSheet = '<style type="text/css">'
                      . file_get_contents('styles/default/main.css')
                      . '</style>';
    $pageHeader = file_get_contents('templates/header.tpl');
    $pageFooter = file_get_contents('templates/footer.tpl');
    
    #$pageBody   = file_get_contents('loaders/pageBody.php');
    ob_start();
    include('loaders/pageBody.php');
    $pageBody = ob_get_clean();
    
    $template->replace('{page_style}' , $configStyleSheet);
    $template->replace('{page_title}' , 'NetBilling');
    $template->replace('{page_header}', $pageHeader);
    $template->replace('{page_footer}', $pageFooter);
    $template->replace('{page_body}'  , $pageBody);
    

    loaders/pageBody.php

    <?php echo 'test'; ?>
    

    [edit] added loaders/pageBody.php from OP’s comment.

    [edit] Updated dev/replacements.php to capture output buffer and use include on .php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following codes using smarty template engine In php file: $smarty-&gt;assign('SITE_URL', 'http://localhost/mis/'); In
I have recently found out that Smarty, differently from Django template engine, does not
I am using smarty as my template engine . I have a php result
Recently, I was working with PHP. In PHP we have a powerful template engine
I have a template in joomla without index.php I only have index.html, css file,
I'm creating a website in Flask using the Jinja template engine. I have a
i have made an application using python and google app engine, the app works
I have two methods that I'm using as custom tags in a template engine:
For some years that I've been using my own PHP template engine, which is
I'm trying to render an html table using underscore template engine. First I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.