-
Notifications
You must be signed in to change notification settings - Fork 20
Creator
(FluentDOM >= 5.1)
The FluentDOM\Nodes\Creator
is a functor, a class usable like a function. In PHP you implement the magic method __invoke to support this. It allows you to have a function with a state/configuration.
You can use the FluentDOM::create() function to get a new instance and assign it to a variable.
The first argument of the function is the node name. The returned object can be cast to string.
$_ = FluentDOM::create();
echo $_('ul');
<?xml version="1.0" encoding="UTF-8"?>
<ul/>
Other arguments can set attributes (arrays or attribute nodes) and create text nodes (string) or child elements.
$_ = FluentDOM::create();
echo $_(
'ul',
['class' => 'navigation'],
$_('li', 'FluentDOM')
);
<?xml version="1.0" encoding="UTF-8"?>
<ul class="navigation"><li>FluentDOM</li></ul>
XML supports several types of nodes. Use methods to creating specific nodes:
- Creator::cdata() - CData Section
- Creator::comment() - Comment
- Creator::pi() - Processing Instruction
$_ = FluentDOM::create();
echo $_(
'ul',
['class' => 'navigation'],
$_(
'li',
$_->cdata('FluentDOM')
)
);
<?xml version="1.0" encoding="UTF-8"?>
<ul class="navigation"><li><![CDATA[FluentDOM]]></li></ul>
If you case the result into a string it will save it as XML. Reading the document property return the created nodes in a DOM document. One possible use case would be generating HTML.
$_ = FluentDOM::create();
echo $_(
'ul',
['class' => 'navigation'],
$_(
'li',
$_('a', ['href' => 'http://fluentdom.org'], 'FluentDOM')
)
)->document->saveHtml();
<ul class="navigation"><li><a href="http://fluentdom.org">FluentDOM</a></li></ul>
Creator::each()
provides a way to iterate a data source. The first argument is the data source. This could be a list of nodes from another document or an array. The second argument is a mapping function. It will be called for
each item from the data source. If you allow the creator into the mapping function you can use it to create nodes from items.
$_ = FluentDOM::create();
$_->formatOutput = TRUE;
$links = [
'http://www.fluentdom.org' => 'FluentDOM',
'http://www.php.net' => 'PHP'
];
echo $_(
'ul',
['class' => 'navigation'],
$_->each(
$links,
function($text, $href) use($_) {
return $_('li', $_('a', ['href' => $href], $text));
}
)
)->document->saveHtml();
<ul class="navigation">
<li><a href="http://www.fluentdom.org">FluentDOM</a></li>
<li><a href="http://www.php.net">PHP</a></li>
</ul>
If you don't like each(), try generators:
$_ = FluentDOM::create();
$_->formatOutput = TRUE;
$links = [
'http://www.fluentdom.org' => 'FluentDOM',
'http://www.php.net' => 'PHP'
];
echo $_(
'ul',
['class' => 'navigation'],
function () use ($_, $links) {
foreach ($links as $href => $text) {
yield $_('li', $_('a', ['href' => $href], $text));
}
}
)->document->saveHtml();
The return value implements FluentDOM\Appendable. This allows to append created fragments to existing documents.
$document = new FluentDOM\DOM\Document();
$document->loadHtml(
'<!DOCTYPE html>
<html><body><div id="navigation"/></body></html>'
);
$_ = FluentDOM::create();
$document
->getElementById('navigation')
->append(
$_(
'ul',
['class' => 'navigation'],
$_(
'li',
$_('a', ['href' => 'http://fluentdom.org'], 'FluentDOM')
)
)
);
echo $document->saveHtml();
<!DOCTYPE html>
<html><body><div id="navigation">
<ul class="navigation"><li><a href="http://fluentdom.org">FluentDOM</a></li></ul>
</div></body></html>
You can register namespaces on the Creator. (They are registered on the internal document, actually).
$_ = FluentDOM::create();
$_->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
$_->formatOutput = TRUE;
echo $_(
'atom:feed',
$_('atom:title', 'Example Feed')
);
<?xml version="1.0" encoding="UTF-8"?>
<atom:feed xmlns:atom="http://www.w3.org/2005/Atom">
<atom:title>Example Feed</atom:title>
</atom:feed>
- Home
- Getting Started
- Tasks
- Plugins
- Functions
- Lists
- Creator (5.1)
- CSS Selectors
- Convertors
- Loaders
- Serializers (5.1)
- Transformers (5.1)
- Extended DOM
- XMLReader (6.1)
- XMLWriter (6.1)
- Interfaces