29

I'm attempting to set up a script to concatenate some variables inside a string if they exist, in order to place the appropriate metadata tags into a rendered HTML document.

My concatenation code is:

data = "<html>\n<head>\n" + "</head>\n<body>\n\n" + paras.join("\n\n") + "\n\n</body>\n</html>";

I'm trying to add if statements like the following into it (between the first and second items):

if (typeof metadata_title !== "undefined") {
    "<title>" + metadata_title + "</title>\n"
}
if (typeof metadata_author !== "undefined") {
    "<meta name=\"author\" content=\"" + metadata_author + "\"></meta>\n"
}
if (typeof metadata_date !== "undefined") {
    "<meta name=\"date\" content=\"" + metadata_date + "\"></meta>\n"
}

But I can't add any of these statements directly into the concatenation code (it throws an error: Unexpected token ().

How best would I go about adding statements such as these into my concatenation string?

astuetz
  • 2,693
  • 2
  • 23
  • 22
木川 炎星
  • 3,893
  • 13
  • 42
  • 51

5 Answers5

62

I'd use a ternary operator:

data = "<html>\n"
     + "<head>\n" 
     + ( typeof metadata_title  !== "undefined" ?  "<title>" + metadata_title + "</title>\n"                             : "" )
     + ( typeof metadata_author !== "undefined" ?  "<meta name=\"author\" content=\"" + metadata_author + "\"></meta>\n" : "" )
     + ( typeof metadata_date   !== "undefined" ?  "<meta name=\"date\" content=\"" + metadata_date + "\"></meta>\n"     : "" )
     + "</head>\n"
     + "<body>\n"
     + "\n"
     + paras.join("\n\n")
     + "\n"
     + "\n"
     + "</body>\n"
     + "</html>"
;
bluish
  • 26,356
  • 27
  • 122
  • 180
  • 1
    < is confused about the usage of `typeof` in this case. Why not just check to see whether or not the `var` *is* `undefined`?.. – Demian Brecht Sep 08 '11 at 07:08
  • 3
    In exceptional cases (`var undefined = 'test';`) the result is wrong. Using `typeof` is saver. – scessor Sep 08 '11 at 07:17
  • If one of the variables is not defined at all you will get a `ReferenceError`. I think this is what the OP had in mind. Yours is perfectly fine if all three metadatas are defined, of course. – Ray Toal Sep 08 '11 at 07:23
7
data = "<html>\n<head>\n" 
    + (
        typeof metadata_title !== "undefined" ?
        "<title>" + metadata_title + "</title>\n" :
        ""
    )
    + (
        typeof metadata_author !== "undefined" ?
        "<meta name=\"author\" content=\"" + metadata_author + "\"></meta>\n" :
        ""
    )
    + (
        typeof metadata_date !== "undefined" ?
         "<meta name=\"date\" content=\"" + metadata_date + "\"></meta>\n" :
        ""
    )
    + "</head>\n<body>\n\n" 
    + paras.join("\n\n") 
    + "\n\n</body>\n</html>";
scessor
  • 15,995
  • 4
  • 43
  • 54
5

I might do something a little different (a little more akin to templating), mainly because I hate concatenated HTML done with Javascript:

var metadata_title = "Hello";
var metadata_author = "Me";
var metadata_date = "2011-09-07";

var template = "<html>\
            <head>\
                <title>#title#</title>\
                <meta name=\"author\" content=\"#author#\"></meta>\
                <meta name=\"date\" content=\"#date#\"></meta>\
            </head>\
            <body>\
            </body>\
            </html>";

var data = template.replace("#title#", metadata_title != undefined ? metadata_title : "")
                   .replace("#author#", metadata_author != undefined ? metadata_author : "")
                   .replace("#date#", metadata_date != undefined ? metadata_date : "");

Sure, there's a very small amount of additional overhead, but to me, it's way more readable.

Demian Brecht
  • 21,135
  • 5
  • 42
  • 46
  • How would you get the "paras" in here? (Not that it is hard; I'd just like to see what you'd come up with in the absence of a real tempating engine. Cheers.) – Ray Toal Sep 08 '11 at 07:32
  • Unless there's something I'm missing (it's late 'n I'm in need of sleep), I'd most likely simply add `#paras#` in between the `body` tags and replace it the same way other elements are replaced.. `join` just returns a string, so I'm assuming that it would work. – Demian Brecht Sep 08 '11 at 07:37
1

I liked the readability of Demian Brecht answer, but I would only change the string for a regex instead, because the replace() function only replaces the first match (see more here: JavaScript .replace only replaces first Match)

var metadata_title = "Hello";
var metadata_author = "Me";
var metadata_date = "2011-09-07";

var template = "<html>\
            <head>\
                <title>#title#</title>\
                <meta name=\"author\" content=\"#author#\"></meta>\
                <meta name=\"date\" content=\"#date#\"></meta>\
            </head>\
            <body>\
            </body>\
            </html>";

var data = template.replace(/#title#/g, metadata_title != undefined ? metadata_title : "")
                   .replace(/#author#/g, metadata_author != undefined ? metadata_author : "")
                   .replace(/#date#/g, metadata_date != undefined ? metadata_date : "");
Community
  • 1
  • 1
Francisco Costa
  • 6,713
  • 5
  • 34
  • 43
1

Build up the entire document into an array, then join with a "\n" at the end. (The rationale for this is of course to not have lots of new lines scattered all about! And if you are on IE7 or less, Array#join is considerably faster than repeated string concatenation.)

Code here: http://jsfiddle.net/ZCbCZ/

UPDATE I forgot to include the "paras" in the first fiddle. The code with the paras is here: http://jsfiddle.net/U8325/1/

For those not wishing to click through and try it out, here is the script:

// Not going to define metadata_author just to be saved by typeof :-)
var metadata_title = "Hello";
var metadata_date = "2011-09-07";

// Okay 3 paras for fun
var paras = ["<p>paragraph1</p>", "<p>paragraph2</p>", "<p>paragraph3</p>"];

data = ["<html>", "<head>"]

if (typeof metadata_title !== "undefined") {
    data.push("<title>" + metadata_title + "</title>");
}
if (typeof metadata_author !== "undefined") {
    data.push("<meta name=\"author\" content=\"" + metadata_author + "\"></meta>");
}
if (typeof metadata_date !== "undefined") {
    data.push("<meta name=\"date\" content=\"" + metadata_date + "\"></meta>");
}

data.push("</head>");
data.push("<body>");
paras.forEach(function (p) {data.push(p);});   // Requires ES5; use a for-loop if you don't have it
data.push("</body>");
data.push("<html>");
data = data.join("\n");
alert(data);
Ray Toal
  • 86,166
  • 18
  • 182
  • 232