Download the PHP package serhii/goodbye-html without Composer
On this page you can find all versions of the php package serhii/goodbye-html. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download serhii/goodbye-html
More information about serhii/goodbye-html
Files in serhii/goodbye-html
Package goodbye-html
Short Description Simple html parser for parsing html files into a string
License MIT
Homepage https://github.com/goodbye-html/goodbye-html
Informations about the package goodbye-html
Goodbye HTML
A very simple package for separating PHP logic from HTML or any other text. It allows you to insert variables, if/elseif/else statements, loops and ternary operators into any text file and dynamically get parsed content of this file.
Supported PHP versions
- ✅ 8.2
- ✅ 8.3
What is it for?
This package is useful when you need to separate PHP logic from HTML or any other text. For example if you need to send an email with some dynamic content, you can create a template file with HTML and insert variables, if/elseif/else statements, loops and ternary operators into it. Then you can pass this file to the parser and get parsed content of this file as a string. Then you can use this string as a content of your email.
What is it not for?
This package is not for creating a full-featured template engine. It's just a simple parser that allows you to insert some PHP logic into any text file. It's not for creating a full-featured template engine like Twig, Blade or Latte. If you need a full-featured template engine, you should use one of the mentioned above.
What Goodbye HTML has?
- [x] Variables
- [x] Assigning variables
- [x] Using variables
- [x] Printing variables
- [x] Comparison operators
- [x] Equal (==)
- [x] Not equal (!=)
- [x] Strong equal (===)
- [x] Strong not equal (!==)
- [x] Greater than (>)
- [x] Less than (<)
- [x] Greater than or equal (>=)
- [x] Less than or equal (<=)
- [x] If/Else-If/Else statements
- [x] Expressions
- Ternary Expressions (true ? 'yes' : 'no')
- Grouped Expressions ((3 + 4) * 5)
- [x] Loops
- [x] Prefix operators
- Negation operator (!)
- Minus operator (-)
- [x] String concatenation
- [x] Math operations
- Addition
- Subtraction
- Multiplication
- Division
- Modulus
Usage
HTML file content with 2 php variables before parsing it
Parsed HTML to a PHP string
Same example but for WordPress shortcode
Options
The instance of Parser
class takes the third argument as a ParserOption
enum. You can pass it to the constructor of the Parser
class as a third argument. For now, it has only a single options:
ParserOption::PARSE_TEXT
If you pass this option, the parser, instead of getting the content of the provided file path, will parse the provided string. This option is useful when you want to parse a string instead of a file.
Supported types
Types that you can pass to the parser to include them in the html/text
file. Note that not all PHP types are supported for know. More types will be added in next releases.
PHP Type | Value example |
---|---|
bool | true |
string | 'Is title' |
int | 24 |
float | 3.1415 |
null | null |
Supported prefix operators
Prefix operators are used to change the value of the variable. For example if you have a variable $is_smoking
and you want to check if it's false, you can use !
prefix operator to change the value of the variable to false. Or if you have a variable $age
and you want to make it negative, you can use -
prefix operator to change the value of the variable to negative.
Prefix name | Prefix value | Example | Supported types for prefix |
---|---|---|---|
Not | ! | !true | all the types |
Minus | - | -24 | int, float |
Supported infix operators
Infix operators are used to perform math operations or string concatenation. For example if you have a variable $age
and you want to add 1 to it, you can use +
infix operator to add 1 to the variable. Or if you have a variable $first_name
and you want to concatenate it with $last_name
, you can use .
infix operator to concatenate these 2 variables.
Operator name | Operator literal | Example | Supported types for prefix |
---|---|---|---|
Plus | + | 3 + 4 | int, float |
Minus | - | 5 - 4 | int, float |
Multiply | * | 3 * 4 | int, float |
Divide | / | 6 / 3 | int, float |
Modulo | % | 5 % 2 | int, float |
Concatenate | . | 'Hello' . ' world' | string |
Assigning | = | {{ $a = 5 }} | all the types |
Equal | == | 5 == 5 | all the types |
Not equal | != | 5 != 4 | all the types |
Strong equal | === | 5 === 5 | all the types |
Strong not equal | !== | 5 !== 4 | all the types |
Greater than | > | 5 > 4 | int, float |
Less than | < | 5 < 4 | int, float |
Greater or equal | >= | 5 >= 4 | int, float |
Less or equal | <= | 5 <= 4 | int, float |
All the available syntax in html/text file
Variable
If statements
If/Else statements
If/Else-If/Else statements
Similar to PHP, you can write
elseif
orelse if
in the same way.
Ternary operator
Ternary operator is commonly referred to as the conditional operator, inline if/else. An expression a ? b : c
evaluates to b
if the value of a
is true, and otherwise to c
. One can read it aloud as "if a then b otherwise c".
Loops
Loop takes 2 integer arguments. The first argument is from what number start looping, and the second argument is where to stop. For example if you start from 1 to 4, it's going to result 4 repeated blocks. Inside each loop you can use $index variable that is going to have a value of current iteration number.
Assigning statements
You can assign values to variables inside your text files using curly braces. For example if you want to assign value 5 to variable $a
, you can do it like this {{ $a = 5 }}
. You can also use prefix operators to change the value of the variable. For example if you want to assign value false to variable $is_smoking
, you can do it like this {{ $is_smoking = !true }}
.