• Stars
    star
    132
  • Rank 274,205 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A set of classes to create and manipulate HTML objects abstractions

HTMLObject

Build Status Latest Stable Version Total Downloads Scrutinizer Quality Score Code Coverage Support via Gittip

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 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}';

More Repositories

1

underscore-php

A redacted PHP port of Underscore.js with additional functions and goodies – Available for Composer and Laravel
PHP
1,121
star
2

flatten

A package to flatten any website to plain HTML
PHP
336
star
3

polyglot

Laravel localization and translation helper
PHP
131
star
4

flickering

A modern PHP interface for the Flickr API
PHP
50
star
5

cookie-monster

A rehosted, cleaned up and updated version of the CookieMonster plugin.
JavaScript
31
star
6

fakable

Allows the creation and seeding of fake Eloquent models
PHP
24
star
7

arrounded

A set of self-aware Laravel abstracts that know their way around
PHP
23
star
8

janitor

Dust off your Laravel applications
PHP
21
star
9

illuminage

Wrapper for the Imagine library that manages caching of images
PHP
18
star
10

registry

The Laravel Packages Registry
PHP
15
star
11

dotfiles

Personnal dotfiles
Ruby
9
star
12

seonnet

SEO handler for Laravel 4, with customization of slugs, meta data and more
PHP
8
star
13

phpstorm-twig-live-templates

Twig Live Templates for PHPStorm
7
star
14

versioner

Small CLI utility to easily create new versions of Composer packages
PHP
4
star
15

isaac-mod-manager

IMM is a cross-plateform CLI-tool to manage your Workshop mods for Binding of Isaac.
PHP
2
star
16

psr-container-helpers

PHP
2
star
17

bs-moment-duration-format

This package allows you to use moment-duration-format with bs-moment
C++
2
star
18

autopergamene

Personnal portfolio
PHP
2
star
19

sadness-deployer

PHP
1
star
20

php-configuration

PHP configuration reference dumper/loader for symfony/config
PHP
1
star
21

slides-agnostic

JavaScript
1
star
22

furnace

An app to rate and curate Rocksmith CDLCs
PHP
1
star