Home >Backend Development >PHP Tutorial >How to Integrate HTML Templates into PHP DOMNodes While Preserving Raw HTML Content?
Integrating HTML Templates into PHP DOMNodes
Integrating HTML templates into existing PHP DOMNodes can be challenging, especially if you desire to preserve the raw HTML content.
Native PHP Solutions
PHP provides a few native methods to achieve this:
Example:
$dom = new DOMDocument; $dom->loadXml('<html><body/></html>'); $body = $dom->documentElement->firstChild; $template = $dom->createDocumentFragment(); $template->appendXML('<h1>
Output:
<html><body><h1>
Example for Importing from Another DOMDocument:
$tpl = new DOMDocument; $tpl->loadXml('<h1>
Example:
libxml_use_internal_errors(true); $tpl = new DOMDocument; $tpl->loadHTML('<h1>
Note:
In all these methods, using TRUE as the second argument to importNode imports the node and its entire subtree.
The above is the detailed content of How to Integrate HTML Templates into PHP DOMNodes While Preserving Raw HTML Content?. For more information, please follow other related articles on the PHP Chinese website!