Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Simple PHP Template Engine

#3
I was working on a project recently, and I realized this could be shrunk down quite a bit while keeping the exact same functionality. Reduced it by over 80% without even employing typical minification (I need to start coding a little more efficiently!) Finna

Lines: 17 (from 79)
Bytes: 408 (from 2.65KB)

Code:
<?php
//Template Engine. License: GPL v3

class template_engine {
    public $tags = array();

    public function set ($k,$v) {
        $this->tags["[@$k]"]=$v;
    }
    public function parse($tmpl) {
        return $this->parse_raw(file_get_contents("templates/$tmpl.html"));
    }
    public function parse_raw($htm) {
        return strtr($htm, $this->tags);
    }
    public function load($name) {
        $this->set($name, $this->parse($name));
    }
}

And there you have it. The world's most lightweight template engine. Big Grin

(I say "engine," but it's essentially a wrapper for template bindings that stores variables, loads templates, and does string replacements without having to keep track of functions, directories, or variables directly. I've used it in several projects, and it's been a great time saver.)


Edit: Pushed this further... Cool [spoiler]

Came back and did some real minification on it, and got it down to about 255 bytes. I must say it definitely gives an appreciation for every single byte. After all, there was once a time when computers only had 64K of RAM. Finna

Code:
<?php class template_engine{function set($k,$v){$this->b["[@$k]"]=$v;}function parse($t){return$this->parse_raw(file_get_contents("templates/$t.html"));}function parse_raw($d){return strtr($d,$this->b);}function load($m){$this->set($m,$this->parse($m));}}

7-zip failed to compress this at all (the file ended up expanding). That being said, XZ got it down to 228 bytes, and Gzip got it down to about 191 (so at least it wasn't totally uncompressable) Tongue

I'm curious to see if this can be made any smaller. If anyone has any ideas, feel free to share them! We might be able to pack this even tighter. Finna

Edit 2: Just how far can this go?
-- It turns out that by renaming all functions to s, l, r, and p, along with the class to t, and removing the template/[name].html hardcoded path (must provide full path on function call), it can be brought down to 193 bytes (178 with gz). Unfortunately, in doing so, compatibility was broken because functions and paths were renamed. Wasn't quite worth the 62 bytes, but this demonstrates just how far this can go. Looks like 255 is still the winner in the meantime.)

[/spoiler]


Attached Files
.php   template_min.php (Size: 409 bytes / Downloads: 156)

Reply


Messages In This Thread
Simple PHP Template Engine - by Darth-Apple - October 15th, 2020 at 10:03 PM
RE: Simple PHP Template Engine - by tc4me - October 16th, 2020 at 6:13 AM
RE: Simple PHP Template Engine - by Darth-Apple - November 25th, 2020 at 6:55 AM
RE: Simple PHP Template Engine - by Darth-Apple - January 17th, 2021 at 7:32 PM
RE: Simple PHP Template Engine - by Darth-Apple - January 19th, 2021 at 12:37 AM
RE: Simple PHP Template Engine - by tc4me - January 19th, 2021 at 6:31 AM
RE: Simple PHP Template Engine - by Darth-Apple - January 27th, 2021 at 6:49 AM
RE: Simple PHP Template Engine - by tc4me - January 27th, 2021 at 10:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple PhpBB theme (getting my feet wet) Darth-Apple 3 2,458 October 23rd, 2020 at 7:05 PM
Last Post: Megan
  Good, simple blog software? Darth-Apple 0 3,706 June 19th, 2013 at 6:23 PM
Last Post: Darth-Apple



Users browsing this thread: 1 Guest(s)

Dark/Light Theme Selector

Contact Us | Makestation | Return to Top | Lite (Archive) Mode | RSS Syndication 
Proudly powered by MyBB 1.8, © 2002-2024
Forum design by Makestation Team © 2013-2024