Description
HTMLObject is a set of classes to create and manipulate HTML objects abstractions.
html-object alternatives and similar libraries
Based on the "Miscellaneous" category.
Alternatively, view html-object alternatives based on common mentions on social networks and blogs.
-
Country List
:globe_with_meridians: List of all countries with names and ISO 3166-1 codes in all languages and data formats. -
Essence
Extracts information about web pages, like youtube videos, twitter statuses or blog articles. -
sabre/vobject
:date: The VObject library for PHP allows you to easily parse and manipulate iCalendar and vCard objects -
Lodash-PHP
Easy to use utility functions for everyday PHP projects. This is a port of the Lodash JS library to PHP -
Embera
A Oembed consumer library, that gives you information about urls. It helps you replace urls to youtube or vimeo for example, with their html embed code. It has advanced features like offline support, responsive embeds and caching support. -
Metrics
Simple library that abstracts different metrics collectors. I find this necessary to have a consistent and simple metrics (functional) API that doesn't cause vendor lock-in. -
ClassPreloader
Optimizes class loading performance by generating a single PHP file containing all of the autoloaded files. -
Cake Utility
[READ-ONLY] CakePHP Utility classes such as Inflector, Text, Hash, Security and Xml. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp -
RedisSessionHandler
An alternative Redis session handler for PHP featuring per-session locking and session fixation protection -
Yell
PHP package to make your objects strict and throw exception when you try to access or set some undefined property in your objects.
Cloudways' Black Friday Offer - 1st Choice of Developers
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of html-object or a related project?
README
HTMLObject
HTMLObject is a set of classes to create and manipulate HTML objects abstractions.
Static calls to the classes
echo Element::p('text')->class('foobar');
// <p class="foobar">text</p>
$list = List::ul(array('foo', 'bar'));
$link = Link::create('#', 'Someone');
$list->getChild(0)->addClass('active')->setValue('by '.$link);
// <ul>
// <li class="active">foo</li>
// <li>by <a href="#">Someone</a></li>
// </ul>
echo Link::create('#foo', 'link')->class('btn btn-success')->blank();
// <a href="#foo" class="btn btn-primary" target="_blank">link</a>
Extending the core classes
The core classes are meant to be extended and used to create complex patterns. All classes implement tree-crawling properties such as the following :
$element = Element::figure();
$element->nest('content') // <figure>content</figure>
$element->nest('p', 'content') // <figure><p>content</p></figure>
$image = Image::create('img.jpg')->alt('foo'); // <img src="img.jpg" alt="foo" />
$element->setChild($image, 'thumb');
$element->getChild('thumb') // HtmlObject\Image
$element->nest(array(
'caption' => Element::figcaption()->nest(array(
'text' => Element::p('foobar'),
)),
));
$element->getChild('caption.text')->getValue() // foobar
// OR
$element->captionText->getValue() // foobar
$element->captionText->getParent(0) // figure->caption
$element->captionText->getParent(1) // figure
$element->wrap('div') // <div><figure>...</figure></div>
$element->wrapValue('div') // <figure><div>...</div></figure>
You can see examples implementations in the [examples](examples) folder.
Properties injection
If your class use properties that are at meant to be added to the final array of attributes, you can inject them using the injectProperties
method. Say you have a Link
class that has an url
property, you can overwrite the method like this, and the $this->url
will get added in the href
attribute :
protected function injectProperties()
{
return array(
'href' => $this->url,
);
}
Or if the property bears the property's name you can simply add it to the array of automatically injected properties :
protected $injectedProperties = array('href', 'title');
// Will be added as href="#foo"
protected $href = '#foo';
// Will be added as title="title"
protected $title = 'title';
Altering a precreated tree
HtmlObject allows to use the open
and close
to open tags but when your tag has children you sometimes want to open the tree at a particular point to inject data at runtime, you can do it like this :
$mediaObject = Element::div([
'title' => Element::h2('John Doe'),
'body' => Element::div(),
]);
echo $mediaObject->openOn('body').'My name is John Doe'.$mediaObject->close();
<div>
<h2>John Doe</h2>
<div>My name is John Doe</div>
</div>
Configuration
You can change whether to follow xHMTL or HTML5 specification by doing the following :
Tag::$config['doctype'] = '{xhtml|html}';