This function replaces all $ names in a string with the value found in a table, or a function. string.gsub simply requires a suitable pattern for matching these names; note that $ must be escaped.
Author: steved
License: MIT/X11 Tags: string utility template
Snippet
function string_interpolate(tmpl,t)
return (tmpl:gsub('%$([%a_][%w_]*)',t))
end
Tests/Usage
local function test(tmpl,t)
print(string_interpolate(tmpl,t))
end
test('$animal ($name)',{animal='dog',name='bonzo'})
test('what is $USER?',os.getenv)
--> dog (bonzo)
--> what is steve?
Related Snippets
- Python-like string formating (2)
- String Tokenizer (2)
- Lua Preprocessor (2)
- String's byte values to number (1)
- Extract C Comments from a String (1)
- Simple Lua Classes (1)
- Currying a Function (1)
- Write a string to a file. (1)
- string.urlescape (1)
- Hex Dump of a String (1)
- Split a string into a list (1)
- simplistic C-like printf (1)
- stack (1)
- Example of Simple Lua Classes (1)
- String Reader (1)
- String Writer (1)
- trim whitespace from string (1)
- Functor (1)
- Testing for NaN (1)
- string.explode (1)
- Table Slice (1)
- String to Hex String (1)
- Simple Table Dump (1)
- simplistic C-like fprintf (1)
- Escape 'magic' characters in a string (1)
- Filter a table in-place 2 (1)
- locale conversion with iconv (1)
- Check string ends with other string (1)
- spliterator (1)
- Filter a table in-place (1)
- memoize - simple/efficient (1)
Revision 000002 by steved; created Tue, 11 Jan 2011 15:48:01 +0000
0 comments
