About: Passionate generalist conquering the web one project at a time. Whether authoring libraries for node, JS, PHP, or Rust, I am always on the lookout for better solutions to common problems.
Location:
USA
Joined:
Oct 1, 2019
Controllable Templating in PHP
Publish Date: Aug 16 '22
0 0
It's been a while
I have been using my own templating engine for a while now as I wasn't happy with how blade & co handle certain things. Additionally, any kind of logic within the view is a violation of separation of concerns in my view.
However, after living with a few issues for a while, I finally decided to rewrite it completely:
As of version 2, we dropped PHP 7.4 support
You require at least PHP 8.0 or use v1.2.0 of this package.
Installation / Quick start
composer require neoan3-apps/template
useNeoan3\Apps\Template\Constants;
useNeoan3\Apps\Template\Template;
require_once'vendor/autoload.php';
// optional, if set, path defines the relative starting point to templatesConstants::setPath(__DIR__ . '/templates');
echoTemplate::embrace('<h1>{{test}}</h1>',['test'=>'Hello World']);
neoan3-template is not a full blown template engine, but rather what a template engine should be:
With modern JavaScript solutions creating a dynamic approach, neoan3-template focuses on the necessities of static rendering.
profile.html
<h1>{{user}}</h1><p>{{profile.name}}</p><pn-for="items as
I have had a way of adding project-specific functionality to the engine and wanted to keep it open for special needs.
By simplifying the hook-process, I noticed something interesting:
In theory, one can now use this package to create specific behavior utilizing two capabilities:
customFunctions
We are usually under full control of the data we are sending to the rendering mechanism when dealing with SSR sites, but sometimes we want to leave e.g. a model as is. So let's build a salutation template function:
I know these examples are rather strange, but due to the reusability one could easily build together a templating mechanism suited to personal needs.
As usual, leaving a star when playing around with the repo is appreciated.
Discussion
Now to the fun part: What has always bothered you when having to deal with passing data to your templating mechanism? What do you wish was less of a hassle?