SxCore\Html\HtmlElement

Wesley Overdijk edited this page Aug 30, 2013 · 2 revisions
Clone this wiki locally

I'm not fully documenting the component, as there aren't that many methods. But here is an example showing off some of the stuff it can do:

<?php

namespace Example;

use SxCore\Html\HtmlElement;

class Example
{

    /**
     * Creating an element is extremely simple.
     *
     * @return HtmlElement
     */
    public function simpleElement()
    {
        $element = new HtmlElement('p');

        $element->setContent('This is the paragraph content.');

        return $element;
    }

    /**
     * HtmlElement even detects void elements and detects whether you're using xhtml or not.
     *
     * When echoing the output of this method, you'll get:
     *  <br> OR <br /> depending on your doctype settings
     *
     * @return HtmlElement
     */
    public function simpleVoidElement()
    {
        $element = new HtmlElement('br');

        return $element;
    }

    /**
     * Spawning child-elements is extremely simple!
     *
     * When echoing the output of this method, you'll get:
     *  Check out my list!<ul><li><a href="some-page">Click here!</a></li><li>I'm special!</li></ul>
     *
     * @return HtmlElement
     */
    public function spanChildExample()
    {
        $element         = new HtmlElement('ul');
        $specialListItem = new HtmlElement('li');
        $childElement    = $element->spawnChild('li');

        $childElement
            ->spanChild('a')
            ->setAttribute('href', 'some-page')
            ->setContent('Click here!');

        $element->setContent('Check out my list!');
        $element->setPrependContent(); // Render the content in front of the child elements.

        /*
         * The coolest part, is that the order of method calls doesn't matter.
         * It'll work it's magic on render ( or __toString)
         */
        $specialListItem->setContent("I'm special!");

        // and you can also add children yourself.
        $element->addChild($specialListItem);

        return $element;
    }

    /**
     * A small attributes demo
     *
     * @return HtmlElement
     */
    public function smallDemo()
    {
        $element = new HtmlElement('div'); // div is the default

        // You can easily add classes...
        $element->addClass('some-class');

        // an attribute....
        $element->addAttribute('id', 'some-id');

        // attributes...
        $element->addAttribute(array(
            'id'         => 'some-id',
            'data-stuff' => 'stuff data.',
        ));

        return $element;
    }
}