Download the PHP package rossato/declarative-php without Composer
On this page you can find all versions of the php package rossato/declarative-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rossato/declarative-php
More information about rossato/declarative-php
Files in rossato/declarative-php
Package declarative-php
Short Description A micro-framework for building declarative and static web applications in PHP
License MIT
Informations about the package declarative-php
:hammer: Simple micro-framework for building declarative web applications in PHP :newspaper:
Minimal experimental PHP framework inspired by ReactDOM to create web pages in a declarative way.
Create dynamic elements through php classes that render into HTML.
Getting started
After installing this framework with composer require rossato/declarative-php
you can build elements with the Element constructor: The class constructor receives 3 arguments: the element tag name, the properties array and the content of the element (its children):
This outputs an html div
element with the custom
class and text
as content, like so: <div class="custom">text</div>
Nested examples
Elements can be nested, the third, fourth, and other parameters can be other elements
You can also supply an array of elements as children:
Generates the following:
Functional usage
You are supposed to create components like this:
Page example
The Page element is useful if you're doing top-level stuff, it is supposedly the page's HTML tag element:
Inspiration
React (the javascript library / framework) has an interesting way of defining components and their hierarchical relations. This experiment is supposed to explore that on PHP.
PHP already has a DOM library internally, but it's full of features such as querying and parsing, this library is only focused on building html elements that are sanitized so that you can just do stuff. This library is 2 source files big and handles attribute and content sanitization.
And this library wasn't made to be complicated, here's an oversimplification of how the element's content is made on the Element
class:
You can probably read and understand this library in 15 minutes.
Problems you can solve using this
Transforming data in PHP to HTML is difficult because html is not strictly validated in general:
You think your browser will let you know that you mispelled 'span' on the second closing tag? Nope, the browser just silently goes on.
You can take advantage of PHP interpreter to tell you if something is incorrect with syntax errors that fail fast.
The idea is to declare in composition-based components all the visual logic, with the data that it needs to render fully.
React developers have been doing this in javascript, its tremendously useful and the same coding rules apply. Sadly implementing JSX is a huge chore and I don't think PHPX would catch on.
Managing data
Data has to be passed around in a top-down fashion:
Obviously, you're left alone regarding how and where you get data. But be advised: All security rules still apply and you must strip your tags from your data!.
Instalation
But you should use composer
to install the project from the command line, at the folder of your project:
Just don't forget to require composer's autoload on your entry point:
And at the top of every file you need to use the Element class, you write this:
Now everytime you write Element
you'll be referring to this project's Element.
Alternatively, you can download this repository and require the files inside /src/
manually, no credits required.
Automatic Compressing of Styles in Elements
Alright, there's just ONE piece of magic in this library: they style
property of html elements are heavily featured in two ways:
-
If you put in an object on the property, it becomes the string representation of that object:
- But if you put a raw string, then we minimize it with a simplistic function to save a few bytes every now and then:
html
$script = new Element("script", [], "console.log('hi
');");echo $script; // '"
$script->isRawHTML = true;
echo $script; // '" javascript async function requestData() { const element = document.querySelector(".content"); document.querySelector(".content").innerHTML = "loading..."; const response = await fetch("/api/endpoint/"); const html = await response.text(); document.querySelector(".content").innerHTML = content; }
requestData();
And at the endpoint you could just handle that
## Testing and Balancing
The idea about the declarative programming style is that you can render different things based on arbitrary conditions:
Or, you could setup caching and serve the cache to end users and bypass it for developers (but keeping the cache files intact).
## Performance
Since we're developing the HTML structure in the backend, the server CPU usage will be increased, I think most servers have low CPU usage and high memory latency problems, like database calls and file readings.
Every request re-renders the entire structure, so caching the result (memoing) is advised. Guess how the you do that:
php
# Licence
You are free to use this and do what you want with it, like changing, redistributing, even claiming as your own, just don't annoy me as I provide no warranty or anything.