To add to @Cameron's answer on escaping your html:
If you have a large amount of code that you want to highlight and don't want to resort manually escaping everything, one option is to save it as a variable using heredoc syntax and use a variant of htmlspecialchars()
(PHP) to escape it. Heredoc lets you save a multi-line string as a variable without having to use concatenation operators.
Then it's just a matter of echo
ing it within your <pre><code>...</code></pre>
block.
The benefit of using this method is the code remains readable in your document.
Example:
<?php
$code = <<< EOT
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<img src="image.png" />
</body>
</html>
EOT;
?>
<pre>
<code class="html">
<?php echo htmlspecialchars( $code ); ?>
</code>
</pre>
One thing to note about using heredoc is that your closing EOT
must be completely left-aligned.
hljs.configure({ 'languages': ['html'] });
before the initialization line. – Nowhither