Using XML code in PHP scripts with XHP
Classic
Besides the specified HTML elements, users can also define their own. It saves a huge amount of time if you just need to add a tag like <ingredients/>
, which is swapped automatically by XHP for an HTML list with ingredients. In this way, you can quickly put together a template engine. To use an individual element, you only need to create a new class; however, a couple of conditions apply to the class: It must be derived from the :x:element
parent class, which – among other things – provides the appendChild()
introduced earlier. Also, the class name must use the tag name and start with a colon. Finally, you need to implement the render()
function, which returns XHP objects.
Listing 6 gives an example. The code in Listing 6 creates a new element called ingredients
. The first step is to define a matching :ingredients
class. This teaches XHP the <ingredients />
tag. Once it shows up somewhere in the PHP code, XHP creates the object. (See line 7 of Listing 6.) Once echo
outputs the element, XHP automatically calls the render()
method, which returns a small list. At this point, Listing 6 uses a little trick: The <ul> ... </ul>
list returned by render()
is automatically transformed by XHP into an XHP object. In other words, render()
returns an XHP object, as requested. Returning the string An article
, however, would result in an error.
Listing 6
Class Definition Example
Listing 6 declares the :ingredients
class (Figure 6). Apparently with a view to XML namespaces, XHP classes use a prefix separated by a colon from the actual name. Here, you could select the admin
prefix so that the entire class goes by the name of admin:ingredients
and the tag is thus <admin:ingredients />
. However, this is just a convention; you can leave out the prefix (or namespace) in your own projects.
How Primitive
The render()
method must always return XHP objects. If you only want to return a string, you have to derive your own class from the x:primitive
parent class. However, this class is explicitly designed for "low-level elements," such as the HTML elements a
or h1
that need to output the markup or tags. In contrast to x:element
, classes derived from x:primitive
need to implement the stringify()
method, which in turn returns the string that also ends up in the output later. Listing 7 shows an example of an element named ingredient
. Its stringify()
method simply outputs the <div>flour</div>
string.
Listing 7
Derivatives of :x:primitive
Listing 6 and Listing 7 do not worry about their child elements. In the following case,
echo <admin:ingredient> <div>sugar</div> </admin:ingredient>;
the output would still be flour
, as shown in Figure 7. <div>sugar</div>
is a child element of admin:ingredient
.
A loop is needed in stringify()
to iterate through all the child elements and integrate them into the output (Figure 8). Listing 8 shows the loop: It uses the getChildren()
method to return all the child elements of the object. The code in Listing 8 is available to all classes derived from x:element
.
Listing 8
Evaluating the Child Elements
Characteristics
If an element has attributes, you can list them just as easily as in Listing 9 with the attribute
keyword. In Listing 9, the element admin:auto
receives exactly two attributes. The first is brand
; it accepts strings and defaults to a value of Ferrari
. The attribute name is followed by @require
, stating the attribute is mandatory. Its colleague color
can only assume two possible values: blue
and red
.
Listing 9
Defining Attributes
You can access the attributes using the getAttributes()
method (Figure 9), which can then be output in a loop (see Listing 9) within render()
. getAttributes()
is also available to any class derived from x:primitive
.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.