avplab / php-html-builder
PHP Html builder simplifies creation of an html code in php scripts. Allows to build (or generate) the html in simple natural way, similarly as create a html page.
Installs: 8 250
Dependents: 1
Suggesters: 0
Security: 0
Stars: 17
Watchers: 1
Forks: 2
Open Issues: 0
Type:package
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: ~4.2
This package is auto-updated.
Last update: 2024-10-27 03:57:33 UTC
README
Sometimes, we strongly need something simple for creating html code in php runtime. For example, we want to build a simple html report, or build a simple html for highlighting some profiling data, etc. Usually for such cases we don't want to use templates engines, or create separated html files. As result we have mess of html and php code, or tons of concatenated strings with html code. PhpHtmlBuilder was created to solve these issues quickly and without clogging the php code. You simply use it same as you write a html.
Installation
Install the component by using Composer. Update your project's composer.json
file to include dependency.
"require": {
"avplab/php-html-builder": "~2.0"
}
Usage
To start building an html code, create an instance of AvpLab\PhpHtmlBuilder
and use it similary as writing html.
$builder = new \AvpLab\PhpHtmlBuilder(); $builder ->tag('!DOCTYPE')->setHtml()->endOpened() ->html()->setLang('en') ->head() ->meta()->setHttpEquiv('X-UA-Compatible')->setContent('IE=edge,chrome=1')->endOpened() ->title('PhpHtmlBuilder: Example')->end() ->end() ->body() ->div()->setClass('container') ->div()->setClass('row') ->div()->setClass('col-md-12') ->h1('PhpHtmlBuilder Demo')->end() ->p('Designed to make the code easier')->end() ->end() ->end() ->end() ->end() ->end(); echo $builder;
The example above will build the following html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>PhpHtmlBuilder: Example</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>PhpHtmlBuilder Demo</h1>
<p>Designed to make the code easier</p>
</div>
</div>
</div>
</body>
</html>
Comments
To add comment block use method addComment()
.
$builder = new \AvpLab\PhpHtmlBuilder();
echo $builder->addComment('foo');
//Result
<!--foo-->
Tags
There are two ways to create HTML tags:
The first one(this is also the most common way) is to call the method with the same name as HTML tag in CamelCase
format.
Tags are always be converted into lowercase with dashes.
$builder = new \AvpLab\PhpHtmlBuilder(); echo $builder->html()->customTag()->end()->end(); //Result <html><custom-tag></custom-tag></html>
The second way is to call the method tag()
with the name of the HTML tag. In this case no any conversion is applied.
This is useful when you need to create very specific tags like <!DOCTYPE>
.
$builder = new \AvpLab\PhpHtmlBuilder(); echo $builder->tag('!DOCTYPE')->endOpened(); //Result <!DOCTYPE>
To complete the tag need to call one of the following methods: end()
, endShorted()
and endOpened()
.
- Method
end()
will create tag<tag></tag>
(i.e.div
,p
,span
, etc.). - Method
endShorted()
will create short tag<tag />
. In this case, tag can have only attributes (i.e.script
,link
,img
orinput
and similar). - Method
endOpened()
will create opened tag<tag>
. In this case, tag can have only attributes (i.e.meta
or similar).
There is also a possibility to add html and attributes during tag creation, using arguments of methods. Arguments will be recognized in the following way:
- If only one argument is provided, and this is an array, it will be recognized as tag attributes, otherwise as tag html(see
addHtml()
below). - If two arguments are provided, the first is a tag html, and second is array of attributes. NOTE: in this case don't need to use camelCase for attributes names. It will keep the name as is. Also if attribute doesn't have a key, it will be recognized as attribute without value.
$builder = new \AvpLab\PhpHtmlBuilder(); echo $builder ->div('<h1>title</h1>', ['class' => 'container', 'Foo' => 'Bar', 'baz']) ->p(['class' => 'article'])->end() ->end(); //Result <div class="container" Foo="Bar" baz><h1>title</h1><p class="article"></p></div>
Attributes
Creating attributes is very similar as creating tags. You need to call the method with an appropriate name in CamelCase
format and beginning with set
.
If method called without arguments, only attribute name will be applied.
$builder = new \AvpLab\PhpHtmlBuilder(); $builder ->tag('!DOCTYPE')->setHtml()->endOpened() ->html()->setLang('en')->end(); //Result <!DOCTYPE html><html lang="en"></html>
Content
To add a plain text(escaped) into the tag, call the method addText()
. For adding "raw" html string, use another method addHtml()
.
$builder = new \AvpLab\PhpHtmlBuilder(); $builder ->div() ->addText('foo') ->addHtml('<b>bar</b>') ->end(); //Result <div>foo<b>bar</b></div>
Render
To get an HTML string, you need to call the build()
method or simply recognize it as string.
$builder = new \AvpLab\PhpHtmlBuilder(); // Do some html $htmlString = $builder->build(); echo $htmlString; // Supports string recognision echo $builder;
License
PhpHtmlBuilder is licensed under the MIT License - see the LICENSE
file for details