JQuery - replace one string for another in .load-ed html?

I'm loading an html snippet using

$("#TemplateDump").load("Themes/default.template", function() { processTemplate() })

The html i am loading contains

<div>
`hello ##name##, your age is ##age##. 
your page is <a href="##website##">here</a>
</div>

I need to replace the ## placeholders with "joe","112" and "www.whatever.com". Is there a more jquery way of doing this rather than using straight javascript .replace? Are there any place holder replacement plugins around? using .replace in IE on the url placeholder just doesnt work either. Dont know why. By the way, the templates cant be changed.

Answers


Doing a simple templating system in vanilla javascript isn't too hard.

var myValues = {
    name : 'Joe',
    age : '112',
    website: 'http://www.whatever.com'
};
var myString = "hello ##name## ..."; // etc

for (var key in myValues) {
    myString.replace(new RegExp("##" + key + "##", g), myValues[key]);
}

Just make sure you run this script on the HTML before it gets inserted into the document. You might want to use a different AJAX function other than load(), perhaps get()?

$.get(
    'themes/default.template',
    {},
    function (data) {
        data = processTemplate(data);
        $('#templateDump').html(data);
    }
);

try jTemplates or a simpler plugin


Need Your Help

C++ map values not changing on run time, what am I doing wrong?

c++ debugging dictionary

I have a map in a global class for my game called realMap. It is used to store variables in the map that all objects in the game can access as long as realMap is in scope. Basically like global. in

Can I change the type of an index on the fly without re-creating the table?

sqlite indexing collation system.data.sqlite

I have always used a custom implementation of SQLite, and it had the "LATIN_NOCASE" collation.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.