Download the PHP package sebastopolys/ginsenghtml without Composer
On this page you can find all versions of the php package sebastopolys/ginsenghtml. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sebastopolys/ginsenghtml
More information about sebastopolys/ginsenghtml
Files in sebastopolys/ginsenghtml
Package ginsenghtml
Short Description Html builder engine for PHP
License GPL-2.0-only
Informations about the package ginsenghtml
Ginseng Html is a html engine for PHP
Provides PHP functions you can use on your code, in order to build large & complex html structures in a more simple, effective and organized way.
Installation
- Install with composer using the following command on a terminal.
composer require sebastopolys/ginsenghtml
- Manual installation
Save the ginsenghtml.php
file somewere in your PHP application.
Usage:
After installing, instantiate ginsenghtml
class in the constructor of a class:
[!NOTE]
ginsenghtml()
is a static class so it needs to be instantiated only once, on the first template file that is loaded.
Because functions are used to build html elements, it is necessary to call the html functions also.
Add the necessary html functions on each template file.
If you change namespace of ginsenghtml class to match your project namespace, it is not neccesary to add use function
declaration.
After this, html functions will become available in further methods of your class:
Html functions:
Function names make reference to the Html tag to be used on the Html the function outputs. I.e:
Will output the following html:
Using the html functions
In the above example, a paragraph is built by passing a string to the function
It is also possible to pass an array to the function and add attributes to the html element
This will print out:
Additional functions: close()
Note that on the above example, there is no content inside the html, neither is a closing html tag (</span>
)
The additional close()
function is provided in order to have more control on html elements closing and opening
Use this function to close any opened html tag:
This will output a self enclosed paragraph element:
Result:
Additional function: content()
The content()
function takes a content value referenced on the given array under the 'content'
index. And it uses this value to insert it within the html element
This function takes a non associative array, composed with one or more associative arrays
When the content function is called, it will include the content and close the html element with its corresponding closing tag
Usage of the content function;
Will output:
[!CAUTION]
'content'
and'tag'
indexes are required when using thecontent()
function on template files
Opened and closed html elements
There are several situations were html functions can return a self enclosed html tag or not
Without closing tag
-
When using any html function with array NOT containing the 'content' index
- If a boolean value is passed to the function
With closing tag
-
When passing a string to any function, instead of an array:
-
If the argument array passed to the function contains the 'content' index
- If a boolean value is passed
Note that if any html element is printed without its closing tag, browsers will add it at the end of the <body>
automatically
Print multiple elements with a single html function
It is possible to output multiple html tags using a single html function.
In order to achieve this the array passed to the function must be indexed without associative keys:
So you can build the array like this:
Giving this array to a function will print the amount of elements represented by the count of the first array on the array.
In the above sample array, it would be the count of "id" values, which is 4
Result:
Supported Html tags (functions)
head | link | footer | header | script |
nav | section | div | span | h1 |
h2 | h3 | p | a | img |
table | form | input | label | ul |
li | hr | th | tr | td |
Feel free to edit the ginsenghtml.php
file and add any missing html tag if you need
Keys & concepts
Functions and arrays
Key of the parser is to understand that you can build the html structure either on the PHP temlate file by using the functions, or in the data arrays passed to the functions that comes from the controller.
I.e.
and:
Will both output the same html:
Nesting functions:
It is possible to nest functions inside functions:
Will output:
Nested argument arrays:
Both 'tag' and 'content' indexes are required so the content()
additional function is executed recursively
Use both 'tag' and 'content' index on your arrays to create & nest html elements.
Argument arrays passed to functions
You can build the arrays passed to functions dinamically, but be aware that this can harm the loading speed of page load. Try to keep the arguments array the smaller as possible.
Changelog
- 1.0.0
- 1.1.0
Initial release
- 1.0.0
Latest Version
- 1.1.0
Author
sebastopolys