Use variable inside CDATA block in Javascript?

CDATA-blocks work great for encoding large blocks of HTML or CSS into strings. But I can't figure out how to use a value variable within one.

For example, consider this JavaScript code:

var FullName    = "Friedrich Hayek";
var ProfileCode = (<><![CDATA[
                    <div id="BigHonkingChunkO_HTML">
                        ...Lot o' code...

                        Name: $FullName$
                        Birth: $Birthdate$

                        ...Lot o' code...

                        ... $FullName$ ...

                        ...Lot o' code...
                    </div>
                ]]></>).toString ();


How do I get $FullName$to render as "Friedrich Hayek" instead of "$ FullName $"?

Note that there are several variables, and each variable can be used several times in a CDATA block.


Alternative code example:

var UserColorPref   = "red";
var UI_CSS          = (<><![CDATA[
                        body {
                            color:              $UserColorPref$;
                        }
                    ]]></>).toString ();

Look for the color attribute red.

+3
source share
3 answers

Is he elegant enough?

function replaceVars(content, vars)
{
    return content.replace(/\$(\w+)\$/g, function($0, $1)
    {
        return ($1 in vars ? vars[$1] : $0);
    });
}

ProfileCode = replaceVars(ProfileCode, {"FullName" : "Friedrich Hayek"});

, :
sprintf vsprintf

?

function replaceVars(content, scope) {
    if (!scope || typeof scope != "object") {
        scope = this;
    }

    return content.replace(/\$(\w+)\$/g, function($0, $1) {
        return ($1 in scope ? scope[$1] : $0);
    });
}

// example1
var FullName = "Friedrich Hayek";
ProfileCode = replaceVars(ProfileCode);

// example2
var vars = {"FullName" : "Friedrich Hayek"};
ProfileCode = replaceVars(ProfileCode, vars);
+5
ProfileCode=ProfileCode.replace('$FullName$',FullName);
+3

CDATA "CDATA Confusion" , , CDATA , (]]>). ,

var x = $('<!DOCTYPE X[<!ENTITY foo "BAR">]><z> cc &#65; &foo;</z>');
console.log ($(x, 'z').text() );

: ]> cc A &foo;

, CDATA. , , , :

var FullName    = "Friedrich Hayek";
var ProfileCode = (<><![CDATA[
                    <div id="BigHonkingChunkO_HTML">
                        ...Lot o' code...

                        Name: ]]></>).toString () + FullName+ (<><![CDATA[

                        ...Lot o' code...
                    </div>
                ]]></>).toString ();
console.log (ProfileCode);

- , , .


:

, CDATA ( , ). CDATA , , Ratna Dinakar.

:

function sSetVarValues (sSrcStr, sReplaceList /* , Variable */)
/*--- function sSetVarValues takes a string and substitutes marked
    locations with the values of the variables represented.
    Conceptually, sSetVarValues() operates a little like sprintf().

    Parameters:
        sSrcStr         --  The source string to be replaced.   
        sReplaceList    --  A string containing a comma-separated list of variable
                            names expected in the raw string.  For example, if 
                            sReplaceList was "Var_A", we would expect (but not require)
                            that sSrcStr contained one or more "$Var_A$" substrings.
        *Variable*      --  A variable-length set of parameters, containing the values
                            of the variables specified in sReplaceList.  For example,
                            if sReplaceList was "Var_A, Var_B, Var_C", then there better 
                            be 3 parameters after sReplaceList in the function call. 
    Returns:                The replaced string.
*/
{
    if (!sSrcStr)       return null;
    if (!sReplaceList)  return null;

    var aReplaceList    = sReplaceList.split (/,\s?/);

    for (var J = aReplaceList.length-1;  J >= 0;  --J)
    {
        var zRepVar     = new RegExp ('\\$' + aReplaceList[J] + '\\$', "g");
        sSrcStr         = sSrcStr.replace (zRepVar, arguments[J+2]);
    }
    return sSrcStr;
}


Using an example:

var AAA     = 'first';
var BBB     = 'second'; 
var CCC     = 'third';
var Before  = "1 is $AAA$, 2 is $BBB$, 3 is $CCC$";

var After   = sSetVarValues (Before, "AAA, BBB, CCC", AAA, BBB, CCC);

console.log (Before);
console.log (After);

Productivity:

    1 is $ AAA $, 2 is $ BBB $, 3 is $ CCC $
    1 is first, 2 is second, 3 is third
+1
source

Source: https://habr.com/ru/post/1784323/


All Articles