PHP template engine that uses data-attributes and keeps HTML templates valid and clean

Overview

Dataplater

PHP template engine that uses data-attributes and keeps HTML templates valid and clean. Scroll down to see a usage example.

Install

composer require leongrdic/dataplater

Requirements

  • PHP 8.0 +
  • XML extension
  • DOM extension

Example

template.html:

table has rows
Your current balance is dollars
">
<table>
    <template data-var-foreach="users; row">
    <tr>
        <template data-var-foreach="row; cell">
        <td data-var-text="cell">td>
        template>
    tr>
    template>
table>
<span>table has <var data-var-text="f/count, users">var> rowsspan> <div> <template data-var-foreach="links; index; link"> <a data-var-href="link.url" data-var-text='f/text.concat, index, v/" ", link.name'>a><br> template> div> <div data-var-if-text='f/compare.equal, balance, v/0 ; lang.balance_empty'>Your current balance is <var data-var-text="balance">var> dollarsdiv> <ul> <template data-var-foreach='f/text.explode, v/" ", sentence, v/4 ; word'> <li data-var-text="word">li> template> ul>

PHP:


require_once 'vendor/autoload.php';

$dp = new \Le\Dataplater('template.html');
echo $dp->render([
    'users' => [
        ['Demo', 'User', '01.01.2020.'],
        ['John', 'Doe', '31.12.2021.'],
    ],
    'links' => [
        ['name' => 'Google', 'url' => 'https://google.com/'],
        ['name' => 'YouTube', 'url' => 'https://youtube.com/'],
        ['name' => 'Facebook', 'url' => 'https://facebook.com/']
    ],
    'items' => [
        'first',
        'second',
        'third',
        'fourth'
    ],
    'balance' => 10,
    'sentence' => 'split this sentence by spaces',
    'lang' => function(){
        return [
            'balance_empty' => 'Your balance is empty.',
            'some_text' => 'Translation'
        ];
    }
]);

Output:

Your current balance is 10 dollars
  • split
  • this
  • sentence
  • by spaces
">
<table>
    <tr>
        <td>Demotd>
        <td>Usertd>
        <td>01.01.2020.td>
    tr>
    <tr>
        <td>Johntd>
        <td>Doetd> <td>31.12.2021.td> tr> table> <span>table has <var>2var> rowsspan> <div> <a href="https://google.com/">0 Googlea><br> <a href="https://youtube.com/">1 YouTubea><br> <a href="https://facebook.com/">2 Facebooka><br> div> <div>Your current balance is <var>10var> dollarsdiv> <ul> <li>splitli> <li>thisli> <li>sentenceli> <li>by spacesli> ul>

(not as nicely formatted as here)

Attributes

attribute name attribute value result
data-var-[text / html] value expression inserts or replaces (html) content with supplied value
data-var-[id / class / title / alt / value / href / src / style] value expression inserts or replaces attribute value with supplied value
data-var-attr attribute name ; value expression adds or replaces attribute value for a specified attribute
data-var-foreach expression to iterate through ; variable name for value
or
expression to iterate through ; variable name for key ; variable name for value
iterates through var and writes value and/or key variables
data-var-if condition expression removes block if condition not true
data-var-if-[text/html] condition expression ; value expression if true
or
condition expression ; value expression if true ; value expression if false
inserts or replaces (html) content with value expression depending on the condition expressions result
data-var-if-[id / class / title / alt / value / href / src / style] condition expression ; value expression if true
or
condition expression ; value expression if true ; value expression if false
adds or replaces attribute value for the attribute depending on the condition expressions result
data-var-if-attr attribute name ; condition expression ; value expression if true
or
attribute name ; condition expression ; value expression if true ; value expression if false
adds or replaces attribute value for a specified attribute name depending on the condition expression result

Expression syntax:

variableName

returns content of the variable

if variable is a closure, executes it and returns the result

variable names shouldn't contain characters like dots or spaces, but ideally they should only contain letters, numbers, _ and -

variableName.arrayElement.objectProperty

access element of array or object property when array/object is in variable

also supports closures at any depth

v/json

decodes json after /, useful for literal values like: v/true, v/false, v/0 or even strings, e.g. v/"string"

f/variableName, param, ...

calls the callable in variableName and passed provided params (if any)

params can be encapsulated into | characters if your expression contains ,

Built-in functions

function php equivalent notes
logic.and, a, b $a && $b
logic.or, a, b $a || $b
logic.not, a !$a
compare.equal, a, b $a == $b
compare.different, a, b $a != $b
compare.exact, a, b $a === $b
compare.notExact, a, b $a !== $b
compare.larger, a, b $a > $b
compare.largerEqual, a, b $a >= $b
compare.smaller, a, b $a < $b
compare.smallerEqual, a, b $a <= $b
array.count, var count($var) var can be a countable array or object
array.reverse, var array_reverse($var)
text.length, string strlen($string)
text.concat, string1, string2, ... implode('', $strings)
text.implode, separator, array implode($separator, $array)
text.explode, separator, string, limit explode($separator, $string, $limit) limit is optional
json.decode, string json_decode($string, true)
json.encode, var json_encode($var)

Class

new Le\Dataplater\Dataplater(string $template, array $vars)

when creating an object, pass your template filename as the first constructor parameter. the second parameter is optional and can hold global variables that will be made available to each render of the template.

$dataplater->render(array $vars);

this method renders the template using global vars together with vars optionally passed as parameter and returns the rendered HTML string. you can call it multiple times on the same dataplater object to render multiple different pages.

Disclaimer

Use on your own responsibility, this may not be suitable for production - I honestly haven't benchmarked it.

You might also like...
This template repository is a boilerplate setup for a PHP application

PHP_Docker Repository Template What is this ? This template repository is a boilerplate setup for a PHP application. Its current version ships with PH

Symfony React Blank is a blank symfony and react project, use this template to start your app using Symfony as an backend api and React as a frontend library.

Symfony React Blank Symfony React Blank is a blank symfony and react project, use this template to start your app using Symfony as an backend api and

A Laravel 5 package that switchs default Laravel scaffolding/boilerplate to AdminLTE template and Pratt Landing Page with Bootstrap 3.0

AdminLTE template Laravel package A Laravel package that switch default Laravel scaffolding / boilerplate to AdminLTE template with Bootstrap 3.0 and

Initial template to start your awesome Laravel, Tailwind and Vue projects
Initial template to start your awesome Laravel, Tailwind and Vue projects

Features Laravel 8.* Tailwind 2.1 Ready and Loaded @tailwindcss/typography @tailwindcss/forms Dark mode ready All variants enabled by default Vue 2, V

Template for repository helper, library - Basic, Simple and Lightweight

Template start helper, library Template for repository helper, library - Basic, Simple and Lightweight Use this Template First, you can Use this templ

Laravel backend Inertia and Vue starter template

Inertia.js - Vue.js ve Laravel Starter Template Yunus Emre Altanay If you want to make a single page application using laravel infrastructure. This re

A template for web development using Laravel, Inertia and Svelte

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Pantheon platform's standard Drupal upstream, and recommended starter template for custom upstreams.

Composer-enabled Drupal template This is Pantheon's recommended starting point for forking new Drupal upstreams that work with the Platform's Integrat

It's a template for using Temporal with Laravel

Temporal Integration with Laravel Make sure you have PHP 7.4 and laravel 8.* , or higher, installed. Temploral PHP SDK is available as composer packag

Releases(3.1.2)
  • 3.1.2(Jul 11, 2022)

  • 3.1.1(Jun 29, 2022)

  • 3.1.0(Jun 23, 2022)

  • 3.0.0(Jun 13, 2022)

    major breaking update!

    • biggest change is now using SMPLang for expression evaluation
    • now using data-dp instead of data-var
    • including html files now possible
    • utf8 support
    • not adding doctype and html tags anymore
    • added tests
    • more thorough documentation with every feature covered in readme
    Source code(tar.gz)
    Source code(zip)
  • 2.1.0(May 4, 2022)

  • 2.0.1(Mar 30, 2022)

  • 2.0.0(Mar 13, 2022)

  • 1.1.0(Feb 28, 2022)

    • the render method now has a $vars parameter so you can add global vars in the constructor and render multiple different pages using the same instance
    • removed undocumented methods
    • updated and rearranged readme
    Source code(tar.gz)
    Source code(zip)
  • 1.0.0(Feb 25, 2022)

Owner
Leon
freelancer
Leon
Textpattern-plugin-template - A template for building plugins for Textpattern CMS.

Plugin template for Textpattern CMS Developer documentation Refer to the Textpattern plugin development documentation, and notably the Plugin template

Textpattern CMS 17 Apr 17, 2022
Until 2018, Backpack v3 used this Base package to offer admin authentication and a blank admin panel using AdminLTE. Backpack v4 no longer uses this package, they're now built-in - use Backpack/CRUD instead.

Note: This package is only used by Backpack v3. Starting with Backpack v4, everything this package does is included in Backpack/CRUD - one package to

Backpack for Laravel 845 Nov 29, 2022
A simple and clean boilerplate to start a new SPA project with authentication and more features from fortify

A simple and clean boilerplate to start a new SPA project with authentication and more features from fortify. Its like the little sister of Jetstream, but as SPA.

Tobias Schulz 11 Dec 30, 2022
A clean integration between Laravel and Expose. ⚡️

A clean integration between Laravel and Expose. This package provides an artisan expose command to quickly share you site via Expose. It will also rep

Ryan Chandler 21 May 23, 2022
This repository is pre-configured, clean and empty skeleton for creating a new projects using Kraken Framework.

Kraken Application Skeleton Note: This repository contains pre-configured application skeleton for fast creation of new projects with Kraken Framework

Kraken 79 Aug 6, 2022
Laravel wrapper of LightnCandy for using Handlebars (and Mustache) templates

Laravel Handlebars This package allows you to use Handlebars (and Mustache) templates with Laravel. You can integrate Handlebars templates into Blade

null 37 Jul 26, 2022
a laravel package to create dynamically dashboard views in several templates ( in development)

Laravel Dashboarder A laravel package for generate admin dashboard dynamically based on Tabler template use livewire - alpinejs Installation Run the c

Laravel Iran Community 7 Dec 12, 2022
PHP Laravel, MySQL and AIML chatbot engine and admin portal

Lemur Engine The Lemur Engine is a PHP/MySQL/AIML Chatbot. Written using the Laravel Framework. Demo You can demo the bot at the website: https://lemu

The Ramen Robot Disco Code 18 Nov 8, 2022
Lumen PHP Framework (Starter Template)

This repository contains JWT Auth, Form Request, Route List, Redis, RabbitMQ and Mail packages. A ready auth system comes with the project

Yasin Köse 1 Feb 11, 2022