November 25th, 2020 at 6:55 AM
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!)
Lines: 17 (from 79)
Bytes: 408 (from 2.65KB)
And there you have it. The world's most lightweight template engine.
(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... [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.
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)
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.
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]
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.
(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... [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.
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)
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.
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]