Packages:Tutorials/Manuals:
Package-level:
Interfaces:
Classes:
Files: |
User Guide for FluentDOMby
by
Table of Contents
Basic Usage FluentDOM Function Function Sample Object Sample Loading Data Prepare A DOMDocument Use The "document" Attribute Custom Loader Find Elements Tutorial: Create A HTML Menu Step 1: The Root Element Step 2: Add Menu Items Step 3: Improve Menu Items Step 4: Mark Menu Items Step 5: Add A Submenu XPath 101 Syntax elements Simple XPath Selector Samples Functions FluentDOMFluentDOM ist a jQuery like fluent XML interface for the DOMDocument in PHP. The idea was born in a workshop of Tobias Schlitt, about the PHP XML extensions at the IPC Spring, in Berlin. He used this idea to show XPath samples in the session. Many thanks to the jQuery people for their work, who did an exceptional job describing their interfaces and providing examples. This saved us a lot of work. We implemented most of the jQuery methods into FluentDOM, but there are differences. Most important: we use XPath for expressions, not CSS selectors. Since XPath is supported by the ext/xml extension, no extra parsing need to be done. This should be faster processing the selectors and btw. it was easier for us to implement. And as a nice topping it supports namespaces, too. We implemented several php interfaces: Countable, IteratorAggregate, SeekableIterator and RecursiveItrerator. Even ArrayAccess is supported. Since FluentDOM works on XML documents, there is no method 'html()', but 'xml()'. We support the string conversion using the magic __toString() method. It will output the xml or html of the associated DOMDocument. FluentDOM needs a document. If you do not provide a valid source, an empty one will be created. XPath do not only match element nodes (nodes with a tag name and maybe children), but text nodes, too. Which implicitly enhances FluentDOM to support them. To be able to write phpUnit Tests and develop FluentDOM a lot of examples where written. Most of them are copied and adapted from or are deeply inspired by the jQuery documentation. They are located in the 'examples' folder and linked in the documentation. Once again many thanks to the jQuery team. Basic UsageThis section shows some of the basic operation. FluentDOM FunctionThe FluentDOM function is a little wrapper. It creates a new FluentDOM object and loads the source if it was provided. Possible sources are local XML or HTML files, XML or HTML strings, DOMDocument DOMNode objects or a FluentDOM object. The following two samples do the same. Function Sample
Object Sample
Loading DataYou can use different ways to load a source into a FluentDOM object. The simplest way is the FluentDOM function. But sometimes you need more. The default loaders do not support URLs for example. But here are other ways: Prepare A DOMDocumentThe first way is to create a DOMDocument with custom logic and load this into the FluentDOM object.
Use The "document" AttributeThe FluentDOM has an read only attribute
Custom LoaderFluentDOM allows to use custom loaders. This are simple classes implementing the
FluentDOMLoader interface. The interface has a single function
Find ElementsThe selector language used in FluentDOM is XPath, not CSS. The method
If the FluentDOM object already contains a selection, the selector will be used with each selected element. If you this is the first action on the object, the document context is used. You can force the document context with the second parameter. Tutorial: Create A HTML MenuStep 1: The Root ElementAfter the FluentDOM class is included an instance of it is created. The default content type of a FluentDOM object is XML. For this tutorial it is changed to HTML.
The method append() appends elements to the current selection and returns a FluentDOM with the appended elements. For later use this is assigned to a variable. The example uses a string parameter to define the tag. This has to be some text or a valid xml tree with a single root node. The parameter can be an DOMNode, too. It is up to you which way you prefer.
Step 2: Add Menu ItemsA menu without items is useless. Using the chaining a listitem is appended and a hyperlink
into it. The link target is set using
Step 3: Improve Menu ItemsOf course menu items can get classes. The method Javascript links do not need to have a href attribute,
Step 4: Mark Menu ItemsOften the first and/or the last element in a list need special threatment. To add classes
to them FluentDOM uses XPath for selectors. In XPath "//" selects elements at an level (from root) and "[1]" means that it has to be the first element in it's parent node.
Step 5: Add A SubmenuIn the last step a submenu is added. The submenu is appended to the <li> tag.
The method
XPath 101This is a little introduction into XPath. For further information see the links at the end of this section. Syntax elements
Simple XPath Selector Samples-match the document element with the name "element-name". /element-name
Match all direct childs of the document element with the name "child". /*/child
Match all elements with name "a" and attribute "href" anywhere in the document. //a[@href]
Match all elements with name "strong" that are child (direct or not) of the current element. .//strong
Match the first direct child of the current element. *[1]
Match all elements with the name "div" in the current element with the attribute "id" set to "sampleOne". div[@id = "sampleOne"] Functions
This is only a selection of functions uses in the following samples. XPath knows some more. |