1. Go to this page and download the library: Download rossato/declarative-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rossato / declarative-php example snippets
use Rossato\Element;
$div = new Element(
"div", // tag name
["class" => "custom"], // properties (as associative array)
"text" // content
);
echo $div;
use Rossato\Element;
echo new Element(
"ul",
["style" => "margin: 10px"],
new Element("li", null, "First element"),
new Element("li", null, "Second element")
);
use Rossato\Element;
echo new Element(
"form",
["style" => "margin: 10px"],
[
new Element("input", ["type" => "text", "id" => "hello"]),
]
);
use Rossato\Element;
function Form($url) {
return new Element("form", ["action" => $url],
new Element("input", ["type" => "submit", "value" => "Submit"])
);
}
function App() {
return new Element("div", [],
Form("/send/"),
Form("/submit/")
]);
}
echo App();
use Rossato\Element;
use Rossato\Page;
$page = new Page([
new Element("title", [], "Title in the head tag")
], [
new Element("h1", [], "Title in the body tag")
]);
echo $page; // "<html><head><title>Title in the head tag</title></head><body><h1>Title in the body tag</h1></body></html>"
class Element {
function __toString() {
return "<" . $this->$tag . ">" . escapeHTML($this->$content) . "</" . $this->$tag . ">";
}
}
$price = $data["price"];
function Post($index, $title, $text) {
return new Element(
"article",
["class"="article-".$index],
new Element("h2", [], $title),
new Element("p", [], $text),
);
}
function PostList($postData) {
$postList = [];
$index = 0;
foreach ($postsData as $name => $post) {
$index++;
$postList[] = new Post($index, $name, $post);
}
return new Element("div", ["class" => "post-list"], $postList);
}
$postsData = [
"My life as a developer" => "It was fun",
"I'm declaring" => "data like normal",
"And html elements treat" => "and using it easily"
];
echo PostList($postData); // Renders pure html (but it may also throw if rendering fails)