substitute.html revision 67cec5a4a455b7740bec2e6e07086d69ce28b1c1
<html>
<head>
<title>substitute</title>
</head>
<body>
<h1>substitute</h1>
<div id="out"></div>
<script type="text/javascript">
(function() {
var logger= new YAHOO.widget.LogWriter("(");
function log(msg) {
logger.log(msg);
}
Event.onDOMReady(function() {
new YAHOO.widget.LogReader(")");
////////////////////////////////////////////////////////////////////////////////
var param = {domain: 'valvion.com',
media: 'http://media.{domain}/', /* nested */
contextdomain: { context1: 'context{domain}', context2: 'yahoo.com' }, /* the value is an object, we will use a custom function to extract the correct data */
contextmedia: 'http://contextmedia.{contextdomain context1}/'};
var url;
// standard replace, nested
url = l.substitute("{media}logo.gif", param);
log(url);
// If the replacement value is an object, use the meta info as a key to extract the
// correct data. Otherwise just return the value.
function multipleChoice(key, val, meta) {
return (l.isObject(val)) ? val[meta] : val;
}
// "random data" is not used since the value for the contextmedia key is a string.
// contextdomain uses "context1" as a key to expand the string correctly
url = l.substitute("{contextmedia random data}logo.gif", param, multipleChoice);
log(url);
////////////////////////////////////////////////////////////////////////////////
var obj = {
level1_1: 1.1,
level1_2: 1.2,
level1_3: {
level2_1: 2.1,
level2_2: 2.2,
level2_3: {
level3_1: 3.1,
level3_2: 3.2,
level3_3: 3.3,
level3_4: 3.4
},
level2_4: 2.4
},
level1_4: 1.4
};
var tostr = {
custom1_1: 1.1,
custom1_2: 1.2,
custom1_3: {
custom2_1: 2.1,
custom2_2: 2.2,
custom2_3: {
custom3_1: 3.1,
custom3_2: 3.2,
custom3_3: 3.3,
custom3_4: 3.4
},
custom2_4: 2.4
},
custom1_4: 1.4,
toString: function() {
return "custom toString executed";
}
};
var arr = [
1.1,
1.2,
[
2.1,
2.2,
[
3.1,
3.2,
3.3,
3.4
],
2.4
],
1.4
];
var mix = [
1.1,
new Date(),
{
level2_1: 2.1,
level2_2: 2.2,
level2_3: [
3.1,
3.2,
3.3,
3.4
],
level2_4: 2.4
},
1.4,
function(){}
];
log(l.substitute("the first level of my object looks like: {testobj 0}", { testobj: obj }));
log(l.substitute("the first two levels of my object looks like: {testobj 1}", { testobj: obj }));
log(l.substitute("my entire object looks like: {testobj 10}", { testobj: obj }));
log(l.substitute("the first two levels of my array looks like: {testarr 1}", { testarr: arr }));
log(l.substitute("the first two levels of my mixed obj looks like: {testmix 1}", { testmix: mix }));
var el = document.getElementById("out");
log(l.dump(el, 0));
////////////////////////////////////////////////////////////////////////////////
log(l.substitute("If the object has a toString, that is used rather than dump. " +
"This does not apply to objects encountered during a dump: {customtostr 1}",
{ customtostr: tostr }));
log(l.substitute("Supplying 'dump' meta info, we can force the dump rather than using the " +
"custom toString: {customtostr dump 1}",
{ customtostr: tostr }));
////////////////////////////////////////////////////////////////////////////////
});
})();
</script>
</body>
</html>