2017 © Pedro Peláez
 

library elements

A simple PHP-library for building HTML-Elements.

image

stefan-wiebe/elements

A simple PHP-library for building HTML-Elements.

  • Saturday, February 3, 2018
  • by Stefan Wiebe
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

Elements

This is a simple library to construct HTML elements with PHP, mainly to avoid getting into a concatenation-hell., (*1)

1 Generating an element

1.1 Constructor

The first way to generate an Element is using its constructor., (*2)

It takes the following parameters, (*3)

Parameter Type Description Example Default
$tag String The HTML-tag 'div' Required
$attributes array HTML-attributes ['id' => 'wrap-god'] array()
$content array The element's contents ['hi', $span] array()

You can put everything into an Element's contents as long as it parses to a String, including other Elements., (*4)

use Elements\Element;

$attributes = [
    'id' => 'wrap-god',
    'class' => ['fresh', 'mexican', 'tortilla'],
];

$div = new Element('div', $attributes);

Classes can be passed as Array (as shown here) or using a String, separating them with spaces ('fresh mexican tortilla'). Note however that Strings will be exploded. As in the PHP-function., (*5)

1.2 Factory Method

There's also a fairly simple factory method for generating an Element only with a tag and the provided classes. As mentioned before, you can either provide an Array of classes or seperate them by spaces., (*6)

Parameter Type Description Example Default
$tag String The HTML-tag 'span' Required
$classes Array \ String The classes 'winged-hussar' | Required
use Elements\Element;

$div = Element::withClasses('div', 'undetected unexpected');

2. Modifying Attributes

Since Elements implement ArrayAccess, its attributes can be manipulated by accessing the Element like an Array., (*7)

Let's say we wanted to give an id to the Element created in 1.2. We would do it like this:, (*8)

$div['id'] = 'night-witch';

With the exception of classes and styles, all attributes are Strings., (*9)

2.1 Classes

Classes are internally handled as Arrays, as previously mentioned. This enables you to easily add and remove classes without having to worry about having one space too many or too few., (*10)

If you wanted to add some to our pre-existing div, you could do it like so:, (*11)

$div['class'][] = 'wings-of-glory';
$div['class'][] = 'tell-the-story';

Replacing them works in the same way as declaring them in the first place:, (*12)

$div['class'] = ['deviation', 'aviation'];
// or
$div['class'] = 'deviation aviation';

2.2 Styles

Styles are also handled as Arrays, however unlike classes, they are associative Arrays, since it'd be kind of hard otherwise., (*13)

You can add classes like so:, (*14)

$div['style']['display'] = 'none'; // Stealth perfected

Note that you can also pass a String to set an Element's styles, but it's not recommended since I can't guarantee that the regex will work 100% of the time. I didn't find any glaring issues, but I also didn't test a lot., (*15)

$div['style'] = 'display: none; background: linear-gradient(to bottom, red, blue);'

3 Content

An Element's contents can be anything that parses to a String. They are kept as array and can be manipulated using the addContent($content) and setContent(array $content) methods., (*16)

Example:, (*17)

$div = Element::withClasses('div', 'form-group');

$input = new Element('input');
$input['type'] = 'text';

$div->addContent($input);

4 Outputting Elements

In order to output an Element, simply echo it or cast it to a string if you need to., (*18)

echo $div;

$someString .= (string) $div;

The Versions

03/02 2018

dev-develop

dev-develop

A simple PHP-library for building HTML-Elements.

  Sources   Download

MIT

by Avatar Stefan Wiebe

html dom html elements

03/02 2018

dev-master

9999999-dev

A simple PHP-library for building HTML-Elements.

  Sources   Download

MIT

by Avatar Stefan Wiebe

html dom html elements

03/02 2018

1.0.2.1

1.0.2.1

A simple PHP-library for building HTML-Elements.

  Sources   Download

MIT

by Avatar Stefan Wiebe

html dom html elements

03/02 2018

1.0.2

1.0.2.0

  Sources   Download

by Avatar Stefan Wiebe

03/02 2018

1.0.1

1.0.1.0

  Sources   Download

by Avatar Stefan Wiebe

03/02 2018

1.0.0

1.0.0.0

  Sources   Download

by Avatar Stefan Wiebe