2009-07-14

FluentDOM Loaders

We are still improving and experimenting with FluentDOM. We removed the constructor and added a load() method. The reason was to allow the creation of new documents with FluentDOM.


Now this is possible:

$fd = new FluentDOM();
$fd->append($fd->document->createElement('html'))
   ->append($fd->document->createElement('body'))
   ->append('<h1>Hell World</h1>');
echo $fd;

FluentDOM uses loader objects (Thanks for the idea Toby) and supports different types of sources. You can load HTML or XML , files or strings or define your own custom loaders. To load a HTML file you can just use the FluentDOM function or the load() method:

$fd = FluentDOM($fileName, 'text/html');

$fd = new FluentDOM();
$fd->load($fileName, 'text/html');

Or you define your own loader object:

$fd = new FluentDOM();
$fd->setLoaders(array(new MyFluentDOMLoader()));
$fd->load($source, $contentType);

You can find an example for inifiles in ~/examples/iniloader/.

6 comments:

  1. FluentDOM === Great stuff.

    I struggle with injecting an atom:link after the last link. I tried

    $fd->find("//_:link[position() = last()]")->parent()->append('< link />');

    But the link ends up at the end. Any easy solution?

    ReplyDelete
  2. You're going one up using parent() - so it appends to the content nodes of the parent node. Try using the after() method on the selected element.

    Regards
    Tom

    ReplyDelete
  3. Fantastic, thanks! What if there is no link from before, any clever ideas of how to inject after last link (if it exists) or after last atom:* otherwise?

    ReplyDelete
  4. You could select all parent nodes that could have links but have not and append to them.

    Untested:

    $fd->find('//_:item[count(_:link) = 0]')->append('< link />')

    ReplyDelete
  5. FluentDOM is an amazing idea, but the documentation is very difficult to digest and there aren't any real helpful examples. i'm trying to learn it so i can help remedy that!

    that said, i'm trying to get setLoaders to work... here's the problem: i can't seem to get an external RSS feed to load using:

    $xml = new FluentDOM($url,$type);

    instead i have to do this:
    $xml = new DOMDocument();
    $xml->load($url);
    $xml = $xml->saveXML($xml);
    $xml = new FluentDOM($xml);

    i'm trying to create a loader to avoid this extra DOMDocument crap. any ideas? ideally it would be as simple as including FluentDOM.php and using it normally.

    please keep up the good work. hopefully more people will start using this and there will be better support! :)

    ReplyDelete
  6. The XML loader uses file_exists(), that why it won't work with urls. Maybe we should add some check for the scheme.

    But here are shorter ways:

    FluentDOM is not only a class but a function, too.

    $dom = new DOMDocument();
    $dom->load($url);
    $fd = FluentDOM($dom);

    or using the class

    $dom = new DOMDocument();
    $dom->load($url);
    $fd = new FluentDOM();
    $fd->load($dom);

    or using the internal DOMDocument

    $fd = new FluentDOM();
    $fd->document->load($url);

    You will find a little newsreader in ~/examples/atom/. It shows namespace handling, too.

    *btw* All this is for the current svn/nightly version.

    ReplyDelete

x