I have to change product name in html help pages (Microsoft help workshop). Product name appears at many places in different html files. Is there anyway I could define constant in html files, and replace product name with constant.
3 Answers
What you want is not possible in HTML, but it is possible in XHTML.
Since XHTML is XML based, you can define new entities within the DOCTYPE declaration. So for example, to define a new entity called title
, you can do this
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
[
<!ENTITY title "Demonstration of entities in XHTML">
]
>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>&title;</title>
</head>
<body>
<h1>&title;</h1>
<p>As you may have guessed, the title of this document is <q>&title;</q>.</p>
</body>
</html>
And this passes the W3C validator too!
Sorry I can't make this into a snippet, because SO doesn't do XHTML. But you can copy this source and paste it into an .xhtml file, or, look at this file (on my own website): http://temp.strictquirks.nl/demo-entities.xhtml

- 45,515
- 15
- 108
- 150
No, HTML is a markup language, not a programming language. It does not know variables or constants when used on its own. It is a set of rules for static document. What you need is a HTML Templating Engine that replaced placeholders with variables or constants. These variables/constants need to be managed by some other programming language, though.
There are various engines that can do the job for you for pretty much every programming language. Jinja for JavaScript is a common choice. If you are looking for something related to C++ in particular this thread lists many options.

- 1,388
- 1
- 17
- 27
It's not possible in html to create variables although you use given way
Create tag with class name as
<span class="abc"></span>
At every place, where you want to have that constant value
Then, just before closing body tag, add JavaScript as
<script>
document.getElementByClassName('abc').innerHTML='CONSTANT';

- 1,832
- 2
- 16
- 18
-
Well, that doesn't make much difference, but for the sake of good practices, i edited my answer to add it before closing body tag, see https://stackoverflow.com/questions/3037725/is-it-wrong-to-place-the-script-tag-after-the-body-tag – Mesar ali Apr 29 '18 at 16:46