{"id":16347743,"url":"https://github.com/oscarotero/html","last_synced_at":"2025-08-09T15:21:12.489Z","repository":{"id":57033371,"uuid":"183763407","full_name":"oscarotero/html","owner":"oscarotero","description":"PHP library to generate HTML code","archived":false,"fork":false,"pushed_at":"2019-12-05T18:06:24.000Z","size":33,"stargazers_count":11,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-18T16:04:04.492Z","etag":null,"topics":["html","html-templating"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oscarotero.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-27T11:18:40.000Z","updated_at":"2025-01-05T00:42:35.000Z","dependencies_parsed_at":"2022-08-23T20:50:31.775Z","dependency_job_id":null,"html_url":"https://github.com/oscarotero/html","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oscarotero%2Fhtml","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oscarotero%2Fhtml/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oscarotero%2Fhtml/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oscarotero%2Fhtml/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oscarotero","download_url":"https://codeload.github.com/oscarotero/html/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245040235,"owners_count":20551297,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["html","html-templating"],"created_at":"2024-10-11T00:45:35.561Z","updated_at":"2025-03-23T00:32:56.878Z","avatar_url":"https://github.com/oscarotero.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# html\nPHP library to generate HTML code\n\n## Install\n\nNote, PHP ~7.2 is required\n\n```sh\ncomposer require oscarotero/html\n```\n\n## Usage\n\n```php\nnamespace Html;\n\n//Create a div\n$div = div();\n\n//Render the div\necho $div;\n//\u003cdiv\u003e\u003c/div\u003e\n\n//Create a div with text content\necho div('Hello world');\n//\u003cdiv\u003eHello world\u003c/div\u003e\n\n//Create a div with more text content\necho div('Hello', ' world');\n//\u003cdiv\u003eHello world\u003c/div\u003e\n\n//HTML entities are escaped\necho div('Hello', ' \u003cworld\u003e');\n//\u003cdiv\u003eHello \u0026lt;world\u0026gt;\u003c/div\u003e\n\n//Use the function raw() to do not escape html entities\necho div('Hello', raw(' \u003cworld\u003e'));\n//\u003cdiv\u003eHello \u003cworld\u003e\u003c/div\u003e\n\n//Create a div with html content\necho div('Hello ', strong('world'));\n//\u003cdiv\u003eHello \u003cstrong\u003eworld\u003c/strong\u003e\u003c/div\u003e\n\n//A div with many content\necho div(\n    h1('Hello world'),\n    p('This is a paragraph'),\n    ul(\n        li('And this is a list item'),\n        li('Other list item')\n    )\n);\n```\n\n## Attributes\n\nThere are two ways to add attributes to the html tags: using an array as the first argument or using magic methods.\n\n```php\n//Add attributes using an array\necho div(['id' =\u003e 'foo'], 'Hello world');\n\n//Or add attributes with magic methods\necho div('Hello world')-\u003eid('foo');\n\n//Both examples outputs: \u003cdiv id=\"foo\"\u003eHello world\u003c/div\u003e\n```\n\nAll attributes with `null` as value are omitted:\n\n```php\n//Add attributes using an array\necho div(['id' =\u003e null], 'Hello world');\n\n//Or add attributes with magic methods\necho div('Hello world')-\u003eid(null);\n\n//Both examples outputs: \u003cdiv\u003eHello world\u003c/div\u003e\n```\n\n### Flags\n\nIn HTML, flags (or boolean attributes) are attributes that does not need a value. Use `boolean` values to add flags.\n\nExample using array syntax:\n\n```php\n//Positive flag \necho div(['hidden' =\u003e true]);\n//\u003cdiv hidden\u003e\u003c/div\u003e\n\n//Negative flag \necho div(['hidden' =\u003e false]);\n//\u003cdiv\u003e\u003c/div\u003e\n\n//A short method to add positive flags:\necho div(['hidden']);\n//\u003cdiv hidden\u003e\u003c/div\u003e\n\n//Mixing attributes and flags\necho div(['hidden', 'class' =\u003e 'foo']);\n//\u003cdiv hidden class=\"foo\"\u003e\u003c/div\u003e\n```\n\nExample using magic methods:\n```php\n//Positive flag \necho div()-\u003ehidden(true);\n//\u003cdiv hidden\u003e\u003c/div\u003e\n\n//Negative flag \necho div()-\u003ehidden(false);\n//\u003cdiv\u003e\u003c/div\u003e\n\n//A short method to add positive flags (true is not needed):\necho div()-\u003ehidden();\n//\u003cdiv hidden\u003e\u003c/div\u003e\n```\n\n### Classes\n\nSome attributes can contain several space-separated values, for example `class`. You can use an array to add classes:\n\n```php\n//Using an array\necho div(['class' =\u003e ['foo', 'bar']]);\n\n//Using the magic method:\necho div()-\u003eclass(['foo', 'bar']);\n\n//Both examples return: \u003cdiv class=\"foo bar\"\u003e\u003c/div\u003e\n```\n\nUse the `key =\u003e value` syntax to add classes under some conditions. Only if the value is evaluated as true, the class will be added:\n\n```php\n//Using an array\necho div([\n    'class' =\u003e [\n        'foo',\n        'bar',\n        'theme-blue' =\u003e true,\n        'error' =\u003e !empty($error)\n    ]\n]);\n\n//Using the magic method:\necho div()-\u003eclass([\n    'foo',\n    'bar',\n    'theme-blue' =\u003e true,\n    'error' =\u003e !empty($error)\n]);\n\n//Both examples output: \u003cdiv class=\"foo bar theme-blue\"\u003e\u003c/div\u003e\n```\n\n### data-* attributes\n\nAny `data-*` attribute containing a non-scalable value, will be converted to json. Unlike flags, boolean values are included too:\n\n```php\n//Using an array\necho div([\n    'data-enabled' =\u003e true,\n    'data-other' =\u003e false,\n    'data-omitted' =\u003e null, //Null values are ommitted\n    'data-options' =\u003e ['foo', 'bar']\n]);\n\n//Using the special method `data()`\necho div()\n    -\u003edata('enabled', true),\n    -\u003edata('other', false),\n    -\u003edata('omitted', null),\n    -\u003edata('options', ['foo', 'bar']);\n\n//Both examples output: \u003cdiv data-enabled=\"true\" data-other=\"false\" data-options=\"[\u0026quot;foo\u0026quot;,\u0026quot;bar\u0026quot;]\"\u003e\u003c/div\u003e\n```\n\n## Import/Export\n\nElements implements `JsonSerializable` and `Serializable` interfaces, so you can export them:\n\n```php\n$article = article(\n    header(\n        h1('Hello world')\n    ),\n    div(['class' =\u003e 'content'],\n        p('This is an article')\n    )\n);\n\n//Export to JSON\n$json = json_encode($article);\n\n//Use the function array2Html to recreate the html from json:\n$article = array2Html(json_decode($json, true));\n\n//Serialize and unserialize\n$ser = serialize($article);\n$article = unserialize($ser);\n```\n\n## Other interfaces implemented\n\nAll elements that can contain children (not self-closing elements like `\u003cbr\u003e` or `\u003cimg\u003e`) implement the following standard PHP interfaces:\n\n### ArrayAccess\n\nTo access to the children elements using the array API.\n\n```php\n$div = div('First child', strong('Second child'), 'Third child');\n\necho $div[2]; //Third child\n```\n\n### IteratorAggregate\n\nTo iterate with the element:\n\n```php\n$div = div('First child', strong('Second child'), 'Third child');\n\nforeach ($div as $child) {\n    echo $child;\n}\n```\n\n### Countable\n\nTo use `count()`:\n\n```php\n$div = div('First child', strong('Second child'), 'Third child');\n\necho count($div); //3\n```\n\nNote that `NULL` children are discarded\n\n```php\n$div = div('First child', null, 'Third child');\n\necho count($div); //2\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foscarotero%2Fhtml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foscarotero%2Fhtml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foscarotero%2Fhtml/lists"}