Created
April 14, 2011 15:55
-
-
Save johnrees/919780 to your computer and use it in GitHub Desktop.
This file contains 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
// Usage: | |
// Create a dynamic textfield with linkage name helloworld_txt and give it an initial value of '{{hello}} {{world}}!' | |
// add the function substitute below to a static class or put it somewhere in your project | |
// call it by passing the textfield name first and an object of key values to replace second | |
Helpers.substitute(helloworld_txt, {hello: 'It', world: 'Works' }); | |
// run the file, the textfield should now say "It Works!" |
This file contains 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
// Put this in a generic utilities class e.g. Helpers.as | |
public function substitute(t:TextField,o:Object):void | |
{ | |
// take the initial textfield that contains {{variable}} | |
var str:String = t.text; | |
var replace:RegExp = /{{(\w+)}}/ig; | |
var result:Object = replace.exec(str); | |
while (result != null) { | |
// loop through each {{variable}} and replace with object parameter | |
t.text = t.text.replace(result[0],o[result[1]]); | |
result = replace.exec(str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment