Heredocs (Here Documents)

Here documents (heredocs) were introduced in programming languages that needed a mechanism for dealing with large amounts of text without having to constantly escape or manually combine strings.

For example, if we try to work with HTML or XML in JS++, it can become a nightmare:

1
2
3
4
5
6
7
string html =   "<!DOCTYPE html>" +
                "<html>" +
                "<head></head>" +
                "<body>" +
                "<a href=\"#\" class=\"blue\" target='_blank'>Link</a>" +
                "</body>" +
                "</html>";

According to the HTML specification, attributes can be enclosed in either double or single quotes. The same is true for XML based on the specification. Manually going through each quotation mark in a large amount of HTML or XML can quickly become tedious and counterproductive. Heredocs were introduced to be able to include large amounts of texts literally without the need for escaping characters, wrapping each line with string quotation marks, and manual concatenation.

JS++ does not provide a special syntax for here documents. Instead, heredocs can be created using regular multi-line strings.

Therefore, we can rewrite the example above using multi-line strings as follows:

1
2
3
4
5
6
7
8
9
10
string html =
    """
    <!DOCTYPE html>
    <html>
    <head></head>
    <body>
    <a href="#" class="blue" target='_blank'>Link</a>
    </body>
    </html>
    """;

See Also

Share

HTML | BBCode | Direct Link