{"id":26964418,"url":"https://github.com/nicklayb/htmlbuilder","last_synced_at":"2025-04-03T06:32:16.810Z","repository":{"id":57026714,"uuid":"68251290","full_name":"nicklayb/htmlbuilder","owner":"nicklayb","description":"PHP Helper for building HTML tags and stuff","archived":false,"fork":false,"pushed_at":"2016-09-15T19:32:51.000Z","size":15,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-27T02:08:02.881Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nicklayb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-14T23:23:57.000Z","updated_at":"2016-09-14T23:25:08.000Z","dependencies_parsed_at":"2022-08-23T16:11:04.556Z","dependency_job_id":null,"html_url":"https://github.com/nicklayb/htmlbuilder","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicklayb%2Fhtmlbuilder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicklayb%2Fhtmlbuilder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicklayb%2Fhtmlbuilder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicklayb%2Fhtmlbuilder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nicklayb","download_url":"https://codeload.github.com/nicklayb/htmlbuilder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246948003,"owners_count":20859360,"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":[],"created_at":"2025-04-03T06:31:23.146Z","updated_at":"2025-04-03T06:32:16.791Z","avatar_url":"https://github.com/nicklayb.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# HtmlBuilder\nUseful helper for builder HTML entities with php\n\nYou should not use this to build full DOM et full pages since it mays get harder to load than pure HTML, however it ensure a correct HTML syntax and some friendly tools for creating tags.\n\nA use case example would be outputting an edit button to a datatable.\n\n## Introduction\n\nInstall it via composer\n```\ncomposer require nicklayb/htmlbuilder\n```\n\n### Creating simple DOM element\n\nFor example, if you would like to create a `input` of type `text` with name `my_input` and classes `super-input` and `holy-input`, you could do it that way.\n```php\n\u003c?php\n\n$tag = '\u003cinput type=\"text\" name=\"my_input\" class=\"super-input holy-input\"\u003e';\n```\nOr you could do it that way\n```php\n\u003c?php\n\nuse Nicklayb\\HtmlBuilder\\Tags\\Input;\n\n$tag = new Input('text');\n$tag-\u003ename('my_input')\n    -\u003eclasses([\n        'super-input',\n        'holy-input',\n    ])\n$tag-\u003emake();\n\n```\n\nThe make method will return your string dom element\n\nYou may say \"Well, how is that an improvement if it takes more lines?\". It simply because it allows you to manipulate element easily. You could swipe class in an easier way than concatenating string. And so, I found it's better to take 5 lines of ~15 characters each than one of concatenation an ternary that takes 70 characters.\n\n### Each tag, each class\nMost of the tags have their own class which let's you use them for what they need to be used. As example, you would never set a value attribute to a div. Let's see some examples.\n```php\n\u003c?php\n\nuse Nicklayb\\HtmlBuilder\\Tags\\A;\nuse Nicklayb\\HtmlBuilder\\Tags\\Input;\nuse Nicklayb\\HtmlBuilder\\Tags\\Select;\n\n$tag = new A('http://google.ca');   //\n$tag = new A;                       //  Both of these will output '\u003ca href=\"http://google.ca\"\u003e\u003c/\u003e'\n$tag-\u003ehref('http://google.ca');     //\n\n$tag = new Input;                   //  '\u003cinput type=\"text\" /\u003e'\n$tag = new Input('password');       //  '\u003cinput type=\"password\" /\u003e'\n\n$tag = new Select([         //  \u003cselect\u003e\n    '-1'=\u003e'Refused',        //      \u003coption value=\"1\"\u003eAccepted\u003c/option\u003e\n    '0'=\u003e'Pending',         //      \u003coption value=\"1\"\u003eAccepted\u003c/option\u003e\n    '1'=\u003e'Accepted'         //      \u003coption value=\"1\"\u003eAccepted\u003c/option\u003e\n]);                         //  \u003c/select\u003e\n```\n\n### Nesting is fun\nYou can easily nest tags, let's nest some stuff together\n\n```php\n\u003c?php\n\nuse Nicklayb\\HtmlBuilder\\Tags\\Div;\n\n$div = new Div([\n    'main',\n    'primary-div'\n]);\n$div-\u003echild(\n    (new Div('second'))-\u003echild(\n        (new Div('third'))-\u003econtent('Tada, stuff!')\n    )\n);\n\necho $div-\u003emake();\n\n/*\n    Will result in\n\n    \u003cdiv class=\"main primary-div\"\u003e\n        \u003cdiv class=\"second\"\u003e\n            \u003cdiv class=\"third\"\u003e\n                Tada, stuff!\n            \u003c/div\u003e\n        \u003c/div\u003e\n    \u003c/div\u003e\n*/\n```\n\n### Custom tag list\n\nLet's say for example you would use the bootstrap button sometimes but don't want to always rewrite it. You could create a custom tag, like this one\n```php\n\u003c?php\n\nuse Nicklayb\\HtmlBuilder\\Tags\\Button as BaseButton;\n\nclass BsButton extends BaseButton\n{\n    public function __construct($color = 'default', $size = '')\n    {\n        __parent::__construct();\n        $this-\u003eclasses([\n            'btn',\n            ($size != '') ? 'btn-'.$size : '',\n            'btn-'.$color\n        ]);\n    }\n}\n\n//  And you can easily use it\nuse MyNamespace\\BsButton;\n\n(new BsButton)-\u003emake()      //  \u003cbutton class=\"btn btn-default\"\u003e\u003c/button\u003e\n(new BsButton('danger'))    //  \u003cbutton class=\"btn btn-danger\"\u003e\u003c/button\u003e   \n\n```\n\n## Conclusion\n\nThank you for using, testing and improving it and feel free to contact me for any question.\n\nEnding joke :\n\u003e A QA engineer enter a bar\n\u003e He order 1 beer\n\u003e He order 0.3 beer\n\u003e He order null beer\n\u003e He order a lizard\n\u003e He order ¡ beer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicklayb%2Fhtmlbuilder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnicklayb%2Fhtmlbuilder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicklayb%2Fhtmlbuilder/lists"}