HTML attribute management

An HTML attribute is a modifier of an HTML element. Particular attributes are only supported by particular element types.

Some attributes are required attributes, needed by particular element types for them to function correctly; while in other cases they are optional attributes. Standard attributes are supported by a large number of element types.

HTML attribute objects implementing Attribute

Framework defines an abstract AbstractAttribute that implements the Attribute. All build in attribute classes are derived from AbstractAttribute.

Any attribute class support at least these four value manipulation methods:

  1. Setting an attribute values: Attribute::setValue()
  2. Requiring (atleast attribute name is always existent in the output): Attribute::forceVisibility()
  3. Value locking: Attribute::protectValue()
    • Notes: attribute is always existent and contains atleast the locked value(s) and cannnot be removed
  4. Clearing non locked attribute values: Attribute::clear()

IdAttribute object for unique HTML element ids

The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document). This attribute is used when identifying IdentifiableContent components.

BooleanAttribute object for boolean attributes

The MultiValueAttribute class

The MultiValueAttribute implements an attribute that can contain multiple separate atomic values.

ClassAttribute :
The most common example of such an attribute is the class attribute which specifies one or more classnames for an HTML component. This attribute is mostly used to point to a class in a style sheet. However, it can also be used by a JavaScript statement to make changes to HTML elements with a specified class.
  • Usage example of class attribute
    1. <?php
    3. namespace Sphp\Html\Attributes;
    5. $classes = (new ClassAttribute())
    6.         ->setValue("button tiny")
    7.         ->add(["alert", "disabled"]);
    8. echo "<button $classes>button</button>\n";
    10. $classes->remove("disabled", "alert");
    11. echo "<button $classes>button</button>\n";
    13. $classes->protectValue('button', "success")->clear();
    14. echo "<button $classes>button</button>\n";
    Highlighted with GeSHi
  • Execution result as highlighted code
    1. <button class="button tiny alert disabled">button</button>
    2. <button class="button tiny">button</button>
    3. <button class="button success">button</button>
    Highlighted with GeSHi
  • Execution result as HTML5 flow

The PropertyCollectionAttribute class

The PropertyCollectionAttribute implements an attribute that contains multiple name value pairs as a set of properties. Property names are always unique and a property name acts as a key to the corresponding value.

The most common example of a property attribute is the style attribute. The style attribute specifies an inline style for an HTML element. The style attribute will override any style set globally, e.g. styles specified in the style tag or in an external style sheet.

  • PHP code
    1. <?php
    3. namespace Sphp\Html\Attributes;
    5. $styles = (new PropertyCollectionAttribute("style"))
    6.         ->setValue("color: #c00; padding: .5em;")
    7.         ->setProperty("font-size", "1.6em")
    8.         ->setProperties([
    9.             "font-weight" => "bold",
    10.             "border" => "dotted 2px #ccc"
    11.         ]);
    12. echo "<p $styles>Styled paragraph</p>\n";
    13. $styles["text-align"] = "right";
    14. echo "<p $styles>Styled paragraph</p>\n";
    Highlighted with GeSHi
  • Execution result as highlighted code
    1. <p style="color:#c00;padding:.5em;font-size:1.6em;font-weight:bold;border:dotted 2px #ccc;">Styled paragraph</p>
    2. <p style="color:#c00;padding:.5em;font-size:1.6em;font-weight:bold;border:dotted 2px #ccc;text-align:right;">Styled paragraph</p>
    Highlighted with GeSHi
  • Execution result as HTML5 flow

    Styled paragraph

    Styled paragraph

  • Examples of scalar attributes
    1. <?php
    3. namespace Sphp\Html\Attributes;
    5. $placeholder = new GeneralAttribute("placeholder", "Give your name");
    6. $type = (new PatternAttribute("type", "/^text|email|number*$/"))->setValue("text");
    7. $name = new GeneralAttribute("name", "foo");
    8. $required = new BooleanAttribute("required", true);
    9. echo "<input $placeholder $type $name $required>\n";
    11. $id = new IdAttribute();
    12. $id->identify();
    13. $value = new GeneralAttribute("value", "Sami Holck");
    14. echo "<input $placeholder $type $name $id $value $required>\n";
    Highlighted with GeSHi
  • Execution result as highlighted code
    1. <input placeholder="Give&#x20;your&#x20;name" type="text" name="foo" required>
    2. <input placeholder="Give&#x20;your&#x20;name" type="text" name="foo" id="PSTHtEVCfcyyXPq0" value="Sami&#x20;Holck" required>
    Highlighted with GeSHi
  • Execution result as HTML5 flow


  • Message: Method set does not exist
  • File: /home/int48291/public_html/playground/sphp/php/Sphp/Reflection/ClassMethodReflector.php
  • Line: #30

Trace information:

  1. File: '/home/int48291/public_html/playground/sphp/php/Sphp/Reflection/ClassMethodReflector.php'
    Line: #30
    Call: ReflectionClass->getMethod('set')
  2. File: '/home/int48291/public_html/playground/sphp/php/Sphp/Apps/Linking/PHP/BasicClassLinker.php'
    Line: #173
    Call: Sphp\Reflection\ClassMethodReflector->__construct('Sphp\Html\Attributes\AttributeManager', 'set')
  3. File: '/home/int48291/public_html/playground/manual/pages/Sphp.Html.Attributes.AttributeManager.php'
    Line: #10
    Call: Sphp\Apps\Linking\PHP\BasicClassLinker->methodLink('set', false)
  4. File: '/home/int48291/public_html/playground/manual/common/manual_helper_functions.php'
    Line: #47
    Call: include('/home/int48291/public_html/playground/manual/pages/Sphp.Html.Attributes.AttributeManager.php')
  5. File: '/home/int48291/public_html/playground/manual/common/manual_helper_functions.php'
    Line: #66
    Call: Sphp\Manual\createPage('Sphp.Html.Attributes.AttributeManager.php')
  6. File: '/home/int48291/public_html/playground/manual/pages/Sphp.Html.Attributes.php'
    Line: #24
    Call: Sphp\Manual\printPage('Sphp.Html.Attributes.AttributeManager')
  7. File: '/home/int48291/public_html/playground/sphp/php/Sphp/Stdlib/Filesystem.php'
    Line: #106
    Call: include('/home/int48291/public_html/playground/manual/pages/Sphp.Html.Attributes.php')
  8. File: '/home/int48291/public_html/playground/sphp/php/Sphp/Html/ContentParserTrait.php'
    Line: #62
    Call: Sphp\Stdlib\Filesystem::executePhpToString('manual/pages/Sphp.Html.Attributes.php')
  9. File: '/home/int48291/public_html/playground/manual/classes/MVC/PageLoader.php'
    Line: #57
    Call: Sphp\Html\AbstractContainerTag->appendPhpFile('manual/pages/Sphp.Html.Attributes.php')
  10. File: '/home/int48291/public_html/playground/manual/classes/MVC/PageLoader.php'
    Line: #93
    Call: Sphp\Manual\MVC\PageLoader->load('manual/pages/Sphp.Html.Attributes.php')
  11. [internal]:
    Call: Sphp\Manual\MVC\PageLoader->loadPage('/Sphp.Html.Attributes/', 'Sphp.Html.Attributes')
  12. File: '/home/int48291/public_html/playground/sphp/php/Sphp/MVC/Router.php'
    Line: #138
    Call: call_user_func_array(array(object(Sphp\Manual\MVC\PageLoader), 'loadPage'), array('/Sphp.Html.Attributes/', 'Sphp.Html.Attributes'))
  13. File: '/home/int48291/public_html/playground/index.php'
    Line: #22
    Call: Sphp\MVC\Router->execute('')