Skip to content

stein197/php-xmler

Repository files navigation

PHP XML Builder

About

This package helps to create XML/HTML output directly from PHP code. The package generates very simplified node tree. Output can be generated by the tree in two ways - beautified output (all indentations and line feeds are present) and minified (without any intentations and line feeds). Also output can be generaten in two modes - HTML and XML mode (all tags are closed). The package generates valid HTML/XML markup and if you need - creates minified output on the fly.

To start using it include it via composer:

require 'vendor/autoload.php';
use STEIN197\XMLBuilder\Builder;

Examples

Simple example:

$builder = (new Builder)
->html(function($html) {
	$html
	->head()
	->body();
});
echo $builder;

outputs:

<html><head></head><body></body></html>

Any method call on Builder is proxied through __call() method, so the name of the method == tag name. To create dashed tag name, call a method in camelCase:

(new Builder)
->camelCased(/* ... */); // -> <camel-cased/>

To create namespaced element, call a method in snake_case:

(new Builder)
->snake_cased(/* ... */); // -> <snake:cased/>

If you need to place underscore, double the _ sign:

(new Builder)
->under__score(/* ... */); // -> <under_score/>

If you need tag which name contains invalid characters for PHP method, or if it points to already existing method, call tag() method. The first argument is tag name others could be callbacks, attributes etc.:

(new Builder)
->tag('custom.TagName_', ['attr' => 'value']); // -> <custom.tagname attr="value"/>

All tag names are lowercased. To create CDATA section and XML comment call these methods respectively:

(new Builder)
->cdata('cdata') // <![CDATA[cdata]]>
->comment('comment'); // <!-- comment -->

Comments are cut out in minified output.

To create <?xml?> element, pass an array of attributes to the constructor:

echo (new Builder(['version' => '1.0'])); /* <?xml version="1.0"?> */

__toString() outputs minified HTML version by default. You can change the behaviour by calling stringify() method directly:

echo $builder->stringify(Builder::OUTPUT_MINIFIED, Builder::MODE_HTML);
// or
echo $builder->stringify(Builder::OUTPUT_BEAUTIFIED, Builder::MODE_XML);

Output of the second line:

<html>
	<head/>
	<body/>
</html>

Of course the level of nesting could be infinite:

(new Builder)
->html(function($html) {
	$html
	->head(function($head) {
		$head
		->link(/* ... */);
	});
});

You can create attributes and content of an element, by passing an array and arbitrary data as arguments to tag method:

(new Builder)
->html(function($html) {
	$html
	->body('content', [
		'class' => 'index'
	]);
});

// <body class="index">content</body>

Also you can pass another builder later if you need one:

$b1 = (new Builder)
->p('Text');
echo (new Builder)
->div($b1);
// <div><p>Text</p></div>

In case if you need custom content for a tag, you should return non-null value from callback. Method calls on callback argument do not affect the tree, only returned value:

(new Builder)
->svg(function($svg) {
	$svg->path(); // Does not affect the tree
	return '<path/>';
});
// <svg><path/></svg>

The order of arguments does not matter. Arrays are always attributes, while everything else is content. Multiple content arguments are concatenated, while only last array considered as attributes.

If an output is minified and a mode is HTML, then the empty elements do not have closing slash. List of self-clising HTML tags:

  • area
  • base
  • br
  • col
  • embed
  • hr
  • img
  • input
  • link
  • meta
  • param
  • source
  • track
  • wbr
  • command
  • keygen
  • menuitem

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published