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

  • Home
  • SEARCH
  • 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 551029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:21:02+00:00 2026-05-13T11:21:02+00:00

I am writing a basic templating class for my own project. The basic usage

  • 0

I am writing a basic templating class for my own project. The basic usage is this:

$template = new Template('template_file.php');
$template->assignVariable('pageTitle', 'Home page');
$template->render();

Contents of ‘template_file.php’:

<?php print $pageTitle; ?>

This is what template class does step by step:

  1. Stores variables in a private array when assignVariable method is called
  2. When render method is called, extracts stored variables, includes template file in a ob_start() and ob_end_clean() block. Stores output in a variable with ob_get_contents() and then prints stored output.

I know this is a very simple templating class but works as expected. The question is should I delegate the including the template file to another class? I had this question when I was writing the unit tests for this class. I thought that file system interaction should be encapsulated. What do you think? If you think that it should not, how can I mock including a file in my tests?

Maybe I just pass the contents of the template file to the class like this:

$templateContent = file_get_contents('template_file.php');
$template = new Template($templateContent);
...

Edit: I decided to encapsulate the input process of template class for the sake of writing better unit tests and encapsulation. But as johannes pointed out, I needed to use eval() for that purpose which seemed not right. Johannes pointed me to the direction of stream wrappers for mocking the including in unit tests. But that inspired a new idea on me. Here is what I am going to do; I will continue to use include() in my template class but this time with stream wrappers. I will pass protocol handler to my template class while initializing it. This way I can create my own stream wrappers for fetching template data from database or using a local variable. Here are the examples:

$template = new Template('file://template_file.php');

stream_wrapper_register('database', 'My_Database_Stream');
$template = new Template('database://templates/3'); // templates table, row id 3

stream_wrapper_register('var', 'My_Var_Stream');
$myTemplate = '<?php print "Hello world!"; ?>';
$template = new Template('var://myTemplate');

I have already implement custom stream wrapper for local variables. Here it is:

class My_Var
{
    protected $position;
    protected $variable;
    function stream_open($path, $mode, $options, &$openedPath) {
        $url = parse_url($path);
        global $$url['host'];
        $this->variable = $$url['host'];
        $this->position = 0;

        return true;
    }
    public function stream_read($count) {
        $ret = substr($this->variable, $this->position, $count);
        $this->position = strlen($ret);
        return $ret;
    }
    public function stream_eof() {
        return $this->position >= strlen($this->variable);
    }
}


stream_wrapper_register('var', 'My_Var');
$myvar = '<?php print "mert"; ?>';
include 'var://myvar';
exit;
  • 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-05-13T11:21:03+00:00Added an answer on May 13, 2026 at 11:21 am

    By passing the contents using file_get_contents() and such you have to use eval() for execution which is bad in multiple ways. One of the most relevant here is that an opcode cache can’t cache the file then. Doing an include(‘template_file.php’); let’s APC or others cache the compiled script.

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

Sidebar

Ask A Question

Stats

  • Questions 310k
  • Answers 310k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You don't need to manually dispose controls that are child… May 13, 2026 at 10:14 pm
  • Editorial Team
    Editorial Team added an answer I would definitely use a relationship between a store and… May 13, 2026 at 10:14 pm
  • Editorial Team
    Editorial Team added an answer Is this what you're looking for? http://docs.python.org/library/itertools.html#itertools.product May 13, 2026 at 10:14 pm

Related Questions

I am a systems programmer, so i just know some basic css/html. I like
I am writing a basic word processing application and am trying to settle on
I am writing a basic http server in C. Handling a simple static .html
I am writing a basic toon shader in OpenGL. I am using MSVC 2008.
I am writing a program which has two panes (via CSplitter ), however I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.