- 
      
- 
        Save rwaldron/1020600 to your computer and use it in GitHub Desktop. 
    Ghetto fabulous templating system. More ghetto than fabulous.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // Ghetto fabulous template system for replacing values in strings. If {{.foo}} | |
| // or {{.bar[0].baz}} is encountered (leading dot), attempt to access properties | |
| // of data object like `data.foo` or `data.bar[0].baz`. Alternately, if {{foo}} | |
| // or {{bar("baz")}} is encountered (no leading dot), simply evaluate `foo` or | |
| // `bar("baz")`. If an error occurs, return empty string. @rworth++ | |
| function ghettoTmpl(data, str) { | |
| return (str + '').replace(/\{\{((\.)?.*?)\}\}/g, function(_, str, dot) { | |
| return eval('try{' + (dot ? 'data' : '') + str + '}catch(e){""}'); | |
| }); | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment