BLOG

Incoherent ramblings, devlogs, edgy philosophy and other topics of interest



The framework's dead simple template engine was reworked to expand on its modular design. Namely, it now keeps track of the final HTML string in a stack and renders everything in a single, uninterrupted call. The content stack is filled using addFile() (adds and prepares a file for writing) and addString() (adds and prepares a string for writing) which completely replaces the previous functions. Once filled, the internal content stack is finally sent to the client using render().

This helps smooth the server to client content parse since it's sent in a sequential manner without interruptions. By atomically writing the complete stack to the socket, the potential for performance hiccups and browser related issues greatly diminishes as the server load grows and the time between writes potentially increases. Previously, it was possible to break the parsing sequence by sandwiching unrelated operations between renderHeader() and renderFooter(), which start and end the writing process respectively. Though this was never the intended design, as the projects relying on pocket_php diversify and the use of its more elemental components get tested from different angles, the limitations of the overly restrictive templating interface became apparent. Leaving services like REST APIs with little choice but to modify the core class in ways that don't disrupt its intended functionality, a clear design flaw.
The new TemplateEngine class

class TemplateEngine
{
    private $contentStack;
    private $stackCounter;

    function __construct ()
    {
        $this->contentStack = array();
        $this->stackCounter = 0;
    }

    public function addFile($filename, $data = NULL)
    {
        $fileContents = file_get_contents(VIEWS_DIR . $filename);
        if (empty($fileContents))
            return;
        else
            $this->addString($fileContents, $data);
    }

    public function addString($string, $data = NULL)
    {
        if (empty($string))
            return;

        if ($data != NULL && is_array($data) || !$empty($data))
            $this->contentStack[$this->stackCounter] = replaceValues($string, '{{', '}}', $data);
        else
            $this->contentStack[$this->stackCounter] = $string;

        $this->stackCounter++;
    }

    public function render()
    {
        if (!ob_get_status()) // IF ob hasn't been manually started by the user
            ob_start();

        foreach($this->contentStack as $section)
            echo($section);

        ob_flush();
    }
}
The core/utility.php file was also overhauled. The template engine keyword replacement function replaceVariables() was substituted by replaceValues(), a more flexible version that can parse keywords between any two different string limiters to encourage its use outside TemplateEngine. Basic directory management, a very general requirement to deal with user uploads was also added in the form of newDir() & delDir(). Check out the core/utility.php commentary on how to speed up directory deletion using system calls.