https://github.com/jogemu/ob_html
Seamlessly generate HTML with PHP functions. Use named arguments to set attributes, neatly nest tags and allow HTML forms to update PHP variables.
https://github.com/jogemu/ob_html
html-form html-metadata keyword-arguments named-arguments nested-tags output-buffering passing-by-reference php
Last synced: 3 months ago
JSON representation
Seamlessly generate HTML with PHP functions. Use named arguments to set attributes, neatly nest tags and allow HTML forms to update PHP variables.
- Host: GitHub
- URL: https://github.com/jogemu/ob_html
- Owner: jogemu
- License: unlicense
- Created: 2024-02-29T02:06:29.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-22T14:45:25.000Z (4 months ago)
- Last Synced: 2025-03-22T15:32:18.031Z (4 months ago)
- Topics: html-form, html-metadata, keyword-arguments, named-arguments, nested-tags, output-buffering, passing-by-reference, php
- Language: PHP
- Homepage:
- Size: 35.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ob_html.php
Seamlessly generate HTML with PHP functions. Use named arguments to set attributes, neatly nest tags and allow HTML forms to update PHP variables. Designed to be compact and easy to understand, even in deeply nested scopes. The native control structures of PHP allow maximum flexibility while keeping the barrier to entry low.## Quick start
Echo shows the expected output of the previous call.```php
Lorem ipsum';
```Positional arguments are [content][anatomy] and [named arguments] are [attributes][anatomy]. Some elements allow arrays, where each entry is placed in its own child element like a list item. A more detailed description and further exceptions can be found [here](#special-array-interpretations).
[named arguments]: https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments
[anatomy]: https://developer.mozilla.org/en-US/docs/Learn_web_development/Getting_started/Your_first_website/Creating_the_content#anatomy_of_an_html_element```php
dl(['Lorem'=>'ipsum', 'dolor'=>'sit']);
echo '
- Lorem
- ipsum
- dolor
- sit
```
Nest tags by using a tag as an argument or provide a function to do loops or exit and re-enter PHP interpretation as needed. Variables from the parent scope can be made accessible with `function() use (...) {}` or [arrow functions](https://www.php.net/manual/en/functions.arrow.php).
```php
Lorem ipsum
`. Inputs that are `readonly` or `disabled` do not overwrite their variables. The optional parameter `ob_action` may provide a function that is called on form submission.```php
$n = $v[0] : $this->$n; }
public function __get($n) { return fn($v) => $this->$n($v); }
public function save() { echo 'Validate and save to database/session'; }
}
$user = new User('John Doe', 0);
form(
textarea('Name', value: $user->name, name: 'name'),
// echo 'John DoeName';
input('Age', value: $user->age, type: 'number', name: 'age'),
// echo 'Age';
button('Submit', type:'submit', name: 'submit', ob_action: $user->save),
// echo 'Submit';
method: 'post'
);
```
Ultimately, the essential HTML elements are still missing. Call the return value of `require 'ob_html.php'` to add a ``, stylesheets and metadata.
```php
'de.php'],
// other links
links: [
['rel'=>'icon', 'href'=>'favicon.svg']
]
);
p('I am a child of tag body.');
```
## Functions
A list of supported functions should not be necessary by design. Assuming familiarity with [HTML elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Element), simply use the functions of the respective name.
### Intentionally excluded
* Essentials and metadata (generated via return or `html()`)
* ``, ``, ``
* ``, ``, ``, ``
* Items and captions (generated via permitted parent)
* `
* ``, ``
* ``, ``, ``, ``
* ``, ``, ``, ``, ``, `` ``, ``
* ``, ``, ``
* Conflicts with native PHP functions or reserved keywords
* `` => `intro()`
* `` => `variable()`
* `