-
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() method 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 add 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. Here a several methods for 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::any() 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'],
$_->any(
$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>The return value of the Creator functor implements FluentDOM\Appendable. This allows to appedn created fragments to existing documents.
$dom = new FluentDOM\Document();
$dom->loadHtml(
'<!DOCTYPE html>
<html><body><div id="navigation"/></body></html>'
);
$_ = FluentDOM::create();
$dom
->getElementById('navigation')
->append(
$_(
'ul',
['class' => 'navigation'],
$_(
'li',
$_('a', ['href' => 'http://fluentdom.org'], 'FluentDOM')
)
)
);
echo $dom->saveHtml();<!DOCTYPE html>
<html><body><div id="navigation">
<ul class="navigation"><li><a href="http://fluentdom.org">FluentDOM</a></li></ul>
</div></body></html>- 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