Home  >  Article  >  Backend Development  >  Class for generating HTML files using php

Class for generating HTML files using php

藏色散人
藏色散人forward
2019-10-11 13:24:493086browse

Purpose

Use PHP to generate HTML documents, support label nesting and indentation, and support label custom attributes

Cause

This thing was indeed written on a whim. It was originally intended to output HTML fragments, but later it was simply written as a function that can output complete HTML;

I am very satisfied with the indentation mechanism inside. , if you find it useful, you can take a look at

p.s. Now the front-end and back-end are truly separated (vue, nodejs, webpack...), just use this thing if you like it, forget it if you don’t like it~

Supported tags

html, head, meta, title, body, input, select/option, form, table, div, comments, and custom tags (appearing in pairs tag), and a lot of attributes (introduced below)

Implementation

Overall

1. Each Tags correspond to a class, such as the tag , which corresponds to a class called html

2. I have put all the useful attributes in tags into a class, and provided methods to obtain them. Attributes assigned a value

Nested indent

I designed a class for each tag, such as the table tag, there is a class called table with it Corresponding to

, each class generally has the following three important members:

1. The indentation value of this tag

2. The method of outputting the final string of this tag , out()

3. To add a subtag to this tag, append()

where append() receives the object of the subtag, and finally calls $html-> ;When out(), the out() method in each sub-label object will be called in sequence, and the indent value will also be passed from the upper class to the lower class and accumulated;

In addition, each class Contains attribute classes (calss attribute). The indentation value of each class is inherited from the attribute class, so if the indentation in the attribute class is modified, the indentation of all classes will change accordingly

You can also add your own indentation value to the label class to override the unified indentation value

Attribute

My solution here is to define a Class (calss attribute), which contains

1. Attributes that tags may have

2. Corresponding setXxx method

3. Assemble non-empty attributes into a string Method

4. In order not to affect the next display, there will be a special clearing method to clear all the previously assigned attributes

In this case, when defining the label attribute, what is needed Assign a value. If you do not assign a value if it is not needed, it will not be displayed in the final HTML code.

Source code

Address: code云Look at the test.php file , is to introduce its usage.

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of Class for generating HTML files using php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete