PHP Classes

PHP HTML Generator Library: Compose and generate HTML programatically

Recommend this page to a friend!
  Info   View files Example   View files View files (10)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 136 This week: 1All time: 9,258 This week: 560Up
Version License PHP version Categories
gen-html 1.0.0Custom (specified...5HTML, PHP 5
Description 

Author

This package can compose and generate HTML programatically.

It provides a document class that can compose HTML documents by adding contained element objects. Then it returns a string with the generated HTML.

The package also provides a HTML element class that be used to define many types of HTML page elements like paragraphs, headings, text formatting styles, links, forms, inputs, images, tables, etc..

Picture of Temuri Takalandze
  Performance   Level  
Name: Temuri Takalandze <contact>
Classes: 6 packages by
Country: Georgia Georgia
Age: 24
All time rank: 35775 in Georgia Georgia
Week rank: 411 Up2 in Georgia Georgia Up
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

Example

<?php

// Include Composer Autoloader.
require_once __DIR__ . '/../vendor/autoload.php';

use
ABGEO\HTMLGenerator\Document;
use
ABGEO\HTMLGenerator\Element;
use
ABGEO\HTMLGenerator\Exception\InvalidDocumentException;

$element = new Element();
$document = new Document();

try {
   
$element
       
->add2Content(
           
Element::createHeader(
               
Element::createParagraph('I\'m Paragraph in Header')
            )
        )
        ->
add2Content(
           
Element::createLink(
               
'Informatics.ge', 'https://informatics.ge',
               
Element::TARGET_BLANK, ['class1', 'class1', 'class2'], 'id'
           
)
        )
        ->
add2Content(Element::createBreak())
        ->
add2Content(Element::createBreak())
        ->
add2Content(Element::createLine())
        ->
add2Content(
           
Element::createArticle(
               
Element::createHeading('I\'m Article'), ['class1'], 'article'
           
)
        )
        ->
add2Content(Element::createBold('I\'m Bold text', [], 'bold'))
        ->
add2Content(
           
Element::createBlockquote(
               
'I\'m Quote',
               
'http://www.worldwildlife.org/who/index.html', [], 'quote'
           
)
        )
        ->
add2Content(
           
Element::createDiv(
               
Element::createParagraph('I\'m Paragraph in Div'), [], 'div'
           
)
        )
        ->
add2Content(Element::createHeading('H1'))
        ->
add2Content(Element::createHeading('H3', 3))
        ->
add2Content(Element::createHeading('H6', 6))
        ->
add2Content(Element::createEm('I\'m Emphasized text'))
        ->
add2Content(Element::createBreak())
        ->
add2Content(Element::createBreak())
        ->
add2Content(Element::createCode('print ("Hello. World")'))
        ->
add2Content(Element::createI('I\'m alternate voice'))
        ->
add2Content(Element::createImg('../images/code.jpg', 'Alternative text'))
        ->
add2Content(
           
Element::createList(
                [
'item1', 'item2', 'item3'], Element::LIST_UNORDERED
           
)
        )
        ->
add2Content(
           
Element::createNav(
                [
                   
Element::createLink('link1', '#link1'),
                   
Element::createLink('link2', '#link2'),
                   
Element::createLink('link3', '#link3'),
                ],
               
'|'
           
)
        )
        ->
add2Content(Element::createParagraph('I\'m Paragraph'))
        ->
add2Content(Element::createPre('I\'m preformatted text'))
        ->
add2Content(Element::createProgress(10))
        ->
add2Content(
           
Element::createSection(
               
Element::concatenateElements(
                   
Element::createHeading('I\'m Section heading'),
                   
Element::createParagraph('I\'m Section paragraph')
                )
            )
        )
        ->
add2Content(Element::createSpan('I\'m Span'))
        ->
add2Content(Element::createBreak())
        ->
add2Content(Element::createStrong('I\'m important text'))
        ->
add2Content(
           
Element::createParagraph(
               
Element::concatenateElements(
                   
'We are ',
                   
Element::createSub('subscripted'), ' and ',
                   
Element::createSup('superscripted'), ' texts'
               
)
            )
        )
        ->
add2Content(
           
Element::createTable(
                [
                    [
'h1', 'h2', 'h3'],
                    [
'd1.1', 'd1.2', 'd1.3'],
                    [
'd2.1', 'd2.2', 'd2.3'],
                    [
'd3.1', 'd3.2', 'd3.3']
                ], [
'table'], 'table1'
           
)
        )
        ->
add2Content(
           
Element::createForm(
               
Element::concatenateElements(
                   
Element::createLabel('Username', 'username'),
                   
Element::createInput(
                       
Element::INPUT_TEXT, 'username', null,
                       
'Enter your username', [], 'username'
                   
),
                   
Element::createBreak(),
                   
Element::createLabel('Password', 'pass'),
                   
Element::createInput(
                       
Element::INPUT_PASSWORD, 'password', null,
                       
'Enter your password', [], 'pass'
                   
),
                   
Element::createBreak(),
                   
Element::createLabel('remember me', 'remember'),
                   
Element::createInput(
                       
Element::INPUT_CHECKBOX, 'remember', null,
                       
null, [], 'remember'
                   
),
                   
Element::createBreak(),
                   
Element::createBreak(),
                   
Element::createInput(Element::INPUT_RESET, null, 'Reset'),
                   
Element::createInput(Element::INPUT_SUBMIT, null, 'Submit'),
                ),
               
'/bootstrap.php'
           
)
        )
        ->
add2Content(
           
Element::createSelect(
                [
                   
'car1' => 'Mercedes',
                   
'car2' => 'Audi',
                   
'car3' => 'BMW',
                   
'car4' => 'Tesla',
                ],
'car'
           
)
        )
        ->
add2Content(Element::createBreak())
        ->
add2Content(
           
Element::createTextarea(
               
'textarea', 'Text about me', 'About me'
           
)
        )
        ->
add2Content(
           
Element::createFooter(
               
Element::createParagraph('I\'m Paragraph in Footer')
            )
        );

   
$document
       
->setLanguage(Document::LANG_GEORGIAN)
        ->
setTitle('Title')
        ->
setCharset(Document::CHARSET_UTF_8)
        ->
setDescription('description')
        ->
setKeywords(['k1', 'k2', 'k3', 'k2'])
        ->
addStyle('../css/theme.css')
        ->
addStyle('../css/custom.css')
        ->
setBody($element->getHtml())
        ->
addScript('../js/scripts.js')
        ->
addScript('../js/messages.js')
        ->
addScript('../js/auth.js');

    echo
$document->getDocument();
} catch (
InvalidDocumentException $e) {
    die(
$e->getMessage());
} catch (
ReflectionException $e) {
    die(
$e->getMessage());
}


Details

gen-html

PHP Library for generating HTML document

GitHub license

GitHub release

Packagist Version

Installation

You can install this library with Composer:

  • `composer require abgeo/gen-html`

Usage

Include composer autoloader in your main file (Ex.: index.php)

  • `require_once __DIR__ . '/../vendor/autoload.php';`

Classes

The library has two classes:

  • `\ABGEO\HTMLGenerator\Document` - For generating Full HTML5 Document;
  • `\ABGEO\HTMLGenerator\Element` - For generating HTML element;

Class Document

Import ABGEO\HTMLGenerator\Document class.

Public Methods

  • `setLanguage()` - Set document content language (Document::LANG_* constants);
  • `setCharset()` - Set charset for document (Document::CHARSET_* constants);
  • `setTitle()` - Set Document title;
  • `setDescription()` - Set Document description;
  • `setKeywords()` - Set Document keywords;
  • `addStyle()` - Add CSS file path;
  • `setBody()` - Set Document body content;
  • `addScript()` - Set JS file path;
  • `getDocument()` - Get generated HTML code;

Note: See usage in example.php

Class Element

Import ABGEO\HTMLGenerator\Element class.

Public Methods

  • `add2Content()` - Add given string to HTML content;
  • `getHtml()` - Get HTML Content;
  • `concatenateElements()` - Concatenate given elements;
  • `createLink()` - Generate a tag;
  • `createArticle()` - Generate article tag;
  • `createBlockquote()` - Generate blockquote tag;
  • `createBreak()` - Generate br tag;
  • `createCode()` - Generate code tag;
  • `createDiv()` - Generate div tag;
  • `createEm()` - Generate em tag;
  • `createForm()` - Generate form tag;
  • `createFooter()` - Generate footer tag;
  • `createHeading()` - Generate h1-h6 tags;
  • `createHeader()` - Generate header tag;
  • `createLine()` - Generate hr tag;
  • `createI()` - Generate i tag;
  • `createImg()` - Generate img tag;
  • `createInput()` - Generate input tag;
  • `createLabel()` - Generate label tag;
  • `createList()` - Generate ol or ul tags;
  • `createNav()` - Generate nav tag;
  • `createParagraph()` - Generate p tag;
  • `createPre()` - Generate pre tag;
  • `createProgress()` - Generate progress tag;
  • `createSection()` - Generate section tag;
  • `createSelect()` - Generate select tag;
  • `createSpan()` - Generate span tag;
  • `createStrong()` - Generate strong tag;
  • `createSub()` - Generate sub tag;
  • `createSup()` - Generate sup tag;
  • `createTable()` - Generate table tag;
  • `createTextarea()` - Generate textarea tag;
  • `clear()` - Clear HTML content;

Note: See usage in example.php

Examples

See full example in example.php and sample Bootstrap 4 page in bootstrap.php.

Authors

  • Temuri Takalandze - Initial work - ABGEO

License

This project is licensed under the MIT License - see the LICENSE file for details


  Files folder image Files  
File Role Description
Files folder image.github (1 file)
Files folder imageexamples (2 files)
Files folder imagesrc (1 directory)
Files folder imagetemplates (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  .github  
File Role Description
  Accessible without login Plain text file FUNDING.yml Data Auxiliary data

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file bootstrap.php Example Example script
  Accessible without login Plain text file example.php Example Example script

  Files folder image Files  /  src  
File Role Description
Files folder imageHTMLGenerator (2 files, 1 directory)

  Files folder image Files  /  src  /  HTMLGenerator  
File Role Description
Files folder imageException (1 file)
  Plain text file Document.php Class Class source
  Plain text file Element.php Class Class source

  Files folder image Files  /  src  /  HTMLGenerator  /  Exception  
File Role Description
  Plain text file InvalidDocumentException.php Class Class source

  Files folder image Files  /  templates  
File Role Description
  Accessible without login HTML file html5.html Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:136
This week:1
All time:9,258
This week:560Up