The situation begins with your blog or website and you need to post some code on a particular webpage. In this tutorial, we will assume you want to post some basic HTML sample code (though this method will work with any programming or scripting language) that contains a hierarchy code structure.
Step 1: The Actual Code
You probably already have the first step, and that is obtaining the code you want to display on your website or blog. For this example, we will use the below:
<html>
<head>
<title>Victor's Programming Aid</title>
</head>
<body>
<p>This is a test</p>
</body>
</html>
Step 2: Code Manipulation
The next important step is to convert special characters to their associated HTML representations. Common examples of this are the greater than (>) and less than (<) symbols often used to distinguish tags. A ‘>’ symbol should be represented as ‘>’ and a ‘<’ symbol is represented as ‘<’. If you code with the actual symbol, the tags will be evaluated and therefore not display properly. The example below is what our example looks like with special symbols replaced with their associated html names.
<html>
<head>
<title>Victor's Programming Aid</title>
</head>
<body>
<p>This is a test</p>
</body>
</html>
Step 3: Code Formatting
However, if you just post the above code in your HTML while preserving the spaces and carriage returns (line breaks), you will discover the formatting is not properly displayed. In my experience, I find that using the HTML <pre></pre> tag is the best way to achieve the end goal. The text within the<pre> tag simply displays text pre-formatted. This means that formatting such as spaces and carriage returns are preserved. The example:
<pre><html>
<head>
<title>Victor's Programming Aid</title>
</head>
<body>
<p>This is a test</p>
</body>
</html></pre>
Success!
Now, your code will be properly formatted with the correct characters and symbols as well as correctly spaced. Happy coding! Let’s see some examples of this in action!
RSS Feed
Posted in
Tags: 
