pre, samp, code

P

Pieter Provoost

Hi,

I'm trying to display some c++ code (without using >) that contains this
line:

#include <string>

I tried <pre>, <samp>, <code> and <pre class="code">, but the only thing
that works is the deprecated <xmp>. In all other case the <string> is not
displayed. Are there any better solutions?

Thanks!
Pieter
 
K

Kim André Akerø

Pieter Provoost said:
Hi,

I'm trying to display some c++ code (without using &gt;) that contains this
line:

#include <string>

I tried <pre>, <samp>, <code> and <pre class="code">, but the only thing
that works is the deprecated <xmp>. In all other case the <string> is not
displayed. Are there any better solutions?

The < and > characters in will always (at least in IE) be used as HTML tags.
If those tags are not valid, it won't be displayed either.

Your solution to the problem is to convert it to the following:

#include &lt;string&gt;

If you're using PHP to display code samples, you can use the function
htmlentities():
http://www.php.net/htmlentities
 
M

Michael Wilcox

Pieter said:
I'm trying to display some c++ code (without using &gt;) that contains this
line:

Why can't you use &gt; and &lt;? This is the proper way.
I tried <pre>, <samp>, <code> and <pre class="code">, but the only thing
that works is the deprecated <xmp>.

Yes, xmp is quite old. <code> would be best.
 
D

David Dorward

Pieter said:
I'm trying to display some c++ code (without using &gt;) that contains
this line:

#include <string>

<pre><code>#include &lt;string&gt;</code></pre>
 
S

Spartanicus

Pieter Provoost said:
I'm trying to display some c++ code (without using &gt;) that contains this
line:

#include <string>

The only proper way to do it is to replace instances of < and >, there's
no good reason why you can't do this.

You could also save the c++ code as a text file and embed it with an
iframe or <object> construct, but this is a flawed method.
 
D

David Dorward

Pieter said:
I'm trying to display some c++ code (without using &gt;)

Why are you trying to avoid the correct way to achive this?
that contains this line:

#include <string>

<pre><code>#include &lt;string&gt;</code></pre>

You need to convert the reserved characters to entities.
 
J

Jukka K. Korpela

Spartanicus said:
The only proper way to do it is to replace instances of < and >,
there's no good reason why you can't do this.

To be exact, the ">" character can be written as such. It's just a common
habit to present it as &gt; for symmetry. There's no reason why a browser
should treat ">" as anything but a data character when it is processing
element content (as opposite to processing a tag after having encountered
a "<" character that starts a tag).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top