Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9300141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:43:02+00:00 2026-06-18T22:43:02+00:00

I’ve written some days ago a simple templating function, problem now is that whitespace

  • 0

I’ve written some days ago a simple templating function, problem now is that whitespace will kill the tag. So when I type something like this: <div id="str">#{my} #{name} #{is} #{#{a}}</div> it works well, however the following does not: <div id="str">#{my} #{name} #{is} #{ #{a} }</div>.

Here’s what I’ve done so far:

$.tmpl = function(str, obj) {
    do {
        var beforeReplace = str;
        for(var key in obj) {
            str = str.replace("#{" + key + "}", obj[key]);
        }
        var afterReplace = str !== beforeReplace;
    } while (afterReplace);

    return str;
};


var map = {
    my: "Am",
    name: "I",
    is: "<a href='#'>awesome</a>",
    a: "#{b}",
    b: "c",
    c: "?"
};

$("#str").html(function(index, oldhtml) {
    $(this).html( $.tmpl(oldhtml, map) );
});

How can i get it working if I use #{a} and #{ #{a} }.
I know, it is possible, and even simple, however I am not a regexp pro.

This works.

This fails.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:43:03+00:00Added an answer on June 18, 2026 at 10:43 pm

    You need to trim the whitespace from the key you’re replacing. To do this, you can use a function as the second parameter to the String.replace() function. You can also replace all the matches for each pass at once using a function like this. So, try the following:

    $.tmpl = function(str, obj) {
        do {
            var beforeReplace = str;
            str = str.replace(/#{([^}]+)}/g, function(wholeMatch, key) {
                var substitution = obj[$.trim(key)];
                return (substitution === undefined ? wholeMatch : substitution);
            });
            var afterReplace = str !== beforeReplace;
        } while (afterReplace);
    
        return str;
    };
    

    The regexp /#{([^}]+)}/g finds all occurrences (the “g” means “global”) of the any string that starts with “#{“, then has at least one character that is not a “}”, then ends with a “}”. The parentheses simply serve to group the key by itself so that the regexp engine will pull it out for us as a parameter to the function.

    The other regexp replacement (/^\s+|\s+$/g) just trims the string. It finds all leading and trailing whitespace and removes it. In ECMAScript 5, there is a String.trim() function that provides this functionality built-in, but that is not widespread enough to reliably use it. See the MDN documentation for details.

    However, I’m not sure of the wisdom of doing multiple passes like this. You introduce the possibility of infinite looping:

    var map = {
        a: "#{b}",
        b: "#{a}"
    };
    
    $.tmpl("#{a}", map);
    

    You also introduce the possibility that the values in the map might accidentally contain #{…} and be expanded without you intending to. This is particularly worrisome if those values are entered by the user. In that case, you have just given him the possibility to make your app loop infinitely or expose the values of other keys. Most template engines do not provide the ability to repeatedly substitute parameters in this fashion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I have just tried to save a simple *.rtf file with some websites and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.