How can one ensure that HTML output from a template engine is valid?
When using a template engine to generate HTML output, it is important to ensure that the generated HTML is valid to prevent rendering issues. To achieve this, one can use a library like HTML Purifier to sanitize the output and remove any potentially harmful or invalid HTML elements. This will help ensure that the generated HTML is safe, clean, and compliant with web standards.
// Example code using HTML Purifier to sanitize HTML output from a template engine
require_once 'path/to/htmlpurifier/library/HTMLPurifier.auto.php';
$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
// HTML output from template engine
$templateOutput = '<p>This is <b>bold</b> text with <script>alert("XSS attack")</script></p>';
// Sanitize the HTML output
$cleanOutput = $purifier->purify($templateOutput);
// Output the sanitized HTML
echo $cleanOutput;