Skip to content

Commit 9d4d830

Browse files
committed
finished the package
1 parent 1e84326 commit 9d4d830

File tree

7 files changed

+354
-37
lines changed

7 files changed

+354
-37
lines changed

composer.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{
22
"name": "levizwannah/php2html",
3-
"description": "Allows you to Write HTML using PHP in an elegant manner. And the result is a clean formatted html.",
3+
"description": "Allows you to Write HTML using PHP in an elegant manner. And the result is a clean formatted html markup.",
4+
"keywords": ["php to html", "php", "html", "markup generation"],
45
"type": "library",
56
"license": "MIT",
7+
"version": "1.0.0",
68
"autoload": {
79
"psr-4": {
810
"LeviZwannah\\Php2html\\": "src/"
@@ -16,5 +18,12 @@
1618
],
1719
"require": {
1820
"php": "^7.4"
19-
}
21+
},
22+
"homepage": "https://github.com/levizwannah/php2html",
23+
"support": {
24+
"issues": "https://github.com/levizwannah/php2html/issues",
25+
"source": "https://github.com/levizwannah/php2html"
26+
},
27+
"minimum-stability": "dev",
28+
"prefer-stable": true
2029
}

example.php

Lines changed: 99 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,104 @@
1-
<!DOCTYPE html>
2-
<html lang="en">
3-
<head>
4-
<meta charset="UTF-8">
5-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7-
<title>Document</title>
8-
</head>
9-
<body>
101
<?php
2+
require (__DIR__ . "/vendor/autoload.php");
113

12-
use LeviZwannah\Php2html\Support\Facades\Html;
4+
use LeviZwannah\Php2html\Facades\Html as H;
135

14-
Html
15-
::div()
16-
::p()::_id("p1")
17-
::text("Hello World")
18-
::p(1)
19-
::div(1);
6+
H::__html5() // echo <!DOCTYPE html>
207

8+
::html()::_lang("en")
9+
10+
::head()
11+
::meta()::_charset("UTF-8")::meta(1)
12+
::meta()::_http_equiv("X-UA-Compatible")::_content("IE=edge")::meta(1)
13+
::meta()
14+
::_name("viewport")::_content("width=device-width, initial-scale=1.0")
15+
::meta(1)
16+
::link()
17+
::_rel("stylesheet")
18+
::_href("https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css")
19+
::_integrity("sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T")
20+
::_crossorigin("anonymous")
21+
::link(1)
22+
::title()::__text("Document")::title(1)
23+
::head(1)
24+
25+
::body()
26+
27+
::div()::_class("container card m-3 shadow-sm p-3 w-full")
28+
::p()::_id("p1")
29+
::__text("Hello World from Paragraph1")
30+
::p(1)
31+
::div()
32+
::p()
33+
::b()::__text("This is bold")::b(1)
34+
::__text(" From another text ")
35+
::i()::__text("This is italic")::i(1)
36+
::__text(" Another Text In here")
37+
::p(1)
38+
::div(1)
39+
::div(1)
40+
41+
::div()::_class("container", "card", "m-3", "w-full")
42+
::__pause(); // pauses
43+
44+
foreach([1, 2, 3, 4, 5] as $num){
45+
H::p()::_id("random1")
46+
::__text("Random Text Here Number $num ")
47+
::p(1)
48+
::__pause(); //pauses
49+
}
50+
51+
H::div(1)
52+
53+
// handling buttons
54+
::div()::_class("container card m-3")
55+
::div()::_class("d-flex justify-content-between w-100 p-3")
56+
57+
::button()::_type("button")::_onclick("btn1()")
58+
::_class("btn btn-outline-danger")
59+
::__text("Click Me 1")
60+
::button(1)
61+
62+
::button()::_type("button")::_onclick("btn2()")
63+
::_class("btn btn-outline-success")
64+
::__text("Click Me 2")
65+
::button(1)
66+
67+
::div(1)
68+
::div(1)
69+
70+
::footer()
71+
::script()
72+
::_src("https://code.jquery.com/jquery-3.3.1.slim.min.js")
73+
::_integrity("sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo")
74+
::_crossorigin("anonymous")
75+
::script(1)
76+
77+
::__text('
78+
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.7/dist/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
79+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
80+
') // you could use this, or script tag as shown below
81+
82+
::script()
83+
// you could write this in an external script file
84+
// or even here, but close the php tags!
85+
// here it is in plain text... beware of injections if you
86+
// use the below approach
87+
::__text('
88+
function btn1(){
89+
console.log("btn 1 clicked");
90+
}
91+
92+
function btn2(){
93+
console.log("btn 2 clicked");
94+
}
95+
')
96+
::script(1)
97+
::footer(1)
98+
99+
::body(1)
100+
::html(1)
101+
102+
::__end(); // finish
21103
?>
22-
</body>
23-
</html>
104+

readme.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# PHP2HTML
2+
Php2html is a library that converts your php code to HTML. You don't need to close and open php tags anymore to write html. The library is expressive and supports every HTML component, tags, and attributes... and even your own elements as long as you follow the rules.
3+
4+
# Usage
5+
You can use this to embed html markup in a php file (among other html markups) or just write the whole markup using the library as shown in `example.php`.
6+
7+
# Syntax
8+
Use the `Html` Facade which is located in `LeviZwannah\Php2html\Facades` for elegant writing.
9+
10+
```
11+
<?php
12+
13+
use LeviZwannah\Php2html\Facades\Html as H;
14+
15+
H::div()
16+
::p()::_id("p1")
17+
::text("Hello PHP2HTML From Paragraph")
18+
::b()::text("A Bold Text")::b(1)
19+
::p(1)
20+
::div(1)
21+
22+
::__end();
23+
24+
?>
25+
```
26+
> Note: functions with double underscores `__` belong to the `php2html` library.
27+
28+
## HTML Attributes
29+
Use underscore(`_`) before the name of the function to tell the library
30+
that it is an attribute. The argument passed to the function is the attribute's value. For example, `_id("p1")` generates `id="p1"`.
31+
> Note: Everything is possible. `_data_phone("num")` will generate and attribute `data-phone="num"`. You can write anything, and it will be converted to markup. The library makes to presumptions of what you will write.
32+
33+
### HTML Attributes with dash(-)
34+
For attributes that contains dash(-) in their names, replace all dashes with underscores(_). For example, `http-equiv="X-UA-Compatible"` will be `::_http_equiv("X-UA-Compatible")`
35+
36+
## HTML Element
37+
Any name without an `_` will be treated as an HTML element. This means, you can even create your own HTML elements. There is no limit. For example
38+
`::random()::_id("random1")::text("A Text here")::random(1);` will generate an html markup: `<random id="random1">A Text here</random>`.
39+
40+
To close a previously opened tag, pass 1 as the argument to the open tag's function. For example, `::p()::text("Hello")::p(1)`. Notice the `::p(1)`.
41+
42+
## The __text() Function
43+
Use this function to put text/markup/code/string between opening and closing tags.
44+
45+
## The __html5() Function
46+
Adds the famous `<!DOCTYPE html>` to your markup.
47+
48+
## The __end() function
49+
Prints the html markup and empties the buffer. All unclosed HTML tags will be closed.
50+
> Warning: Call this at the end of the file. Otherwise, use __pause() if your want to run a php code in between markup generation.
51+
52+
## The __pause() function
53+
Prints out the current generated markup without closing opened tags. This allows you to run a php code in between markup generation. For example,
54+
```
55+
...
56+
::div()::_class("container", "card", "m-3") // opens div
57+
::__pause(); // prints the current markup
58+
59+
foreach([1, 2, 3, 4, 5] as $num){
60+
H::p()::_id("random-$num")
61+
::__text("Random Text Here Number $num ")
62+
::p(1)
63+
::__pause(); //prints the current markup
64+
}
65+
66+
H::div(1) // closes div
67+
...
68+
...
69+
70+
H::end(); // prints all the remaining markups and closes open tags.
71+
```
72+
73+
## Self-Closing tags
74+
Just follow the formats of opening and closing elements. If there is no inner text in the, the library will handle self-closing tags.
75+
76+
77+
# Installation
78+
From Composer
79+
`composer require levizwannah/php2html`
80+
81+
# Finally,
82+
You will like this! But the `Html` object is a singleton🏃‍♂️🏃‍♂️ for memory sake.
83+
84+

src/Support/Facades/AbstractFacade.php renamed to src/Facades/AbstractFacade.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
namespace LeviZwannah\Php2html\Support\Facades;
2+
namespace LeviZwannah\Php2html\Facades;
33

44

55
abstract class AbstractFacade{
@@ -26,7 +26,9 @@ public static function __callStatic($name, $arguments)
2626

2727
$instance = self::$instances[$class];
2828

29-
return $instance->$name(...$arguments);
29+
$instance->$name(...$arguments);
30+
31+
return static::class;
3032
}
3133
}
3234

src/Facades/Html.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace LeviZwannah\Php2html\Facades;
3+
4+
use LeviZwannah\Php2html\Html as Php2htmlHtml;
5+
6+
/**
7+
* The Html Facade. Mainly use this as H, to reduce the writing
8+
* @method static Html __text(string $string)
9+
* @method static Html __html5()
10+
* @method static Html __pause()
11+
* @method static Html __end()
12+
* @method static Html _<attr>(string $attributeValue)
13+
* @method static Html <elem>()
14+
* @method static Html <elem>(int $close = 1)
15+
*/
16+
class Html extends AbstractFacade{
17+
18+
19+
public static function accessor()
20+
{
21+
return Php2htmlHtml::class;
22+
}
23+
}
24+
?>

0 commit comments

Comments
 (0)