Class Parser
A template engine. A Parser instance can set template variable values, then parse a formatted string containing template variable placeholders to replace their values.
Syntax
Examples
Parser p;
p.setVariable("firstName", "Jack");
p.setVariable("lastName", "Black");
String output = p.parseString("Hello [[firstName]] [[lastName]]!");
printLine(output);
Constructors
Parser()
Initializes a new instance of the Parser class.
Declaration
Parser
Examples
Parser p;
p.setVariable("firstName", "Jack");
p.setVariable("lastName", "Black");
String output = p.parseString("Hello [[firstName]] [[lastName]]!");
printLine(output);
Methods
addVariable(String,String)
Adds a variable and its value to the parser.
Declaration
Void addVariable(String field, String value)
Parameters
Type | Name | Description |
---|---|---|
String | field | The field to add |
String | value | The value of the field. |
Returns
Type | Description |
---|---|
Void |
Examples
Parser p;
p.setVariable("firstName", "Jack");
p.setVariable("lastName", "Black");
String output = p.parseString("Hello [[firstName]] [[lastName]]!");
printLine(output);
evaluateString(String)
Parses a text and return the evaluated result from a RETURN statement inside the text.
Declaration
String evaluateString(String stringToParse)
Parameters
Type | Name | Description |
---|---|---|
String | stringToParse | The string to parse. |
Returns
Type | Description |
---|---|
String | Any string set by a RETURN statement inside stringToParse. |
Examples
Parser p;
p.setVariable("firstName", "Jack");
p.setVariable("lastName", "Black");
String output = p.parseString("Hello [[firstName]] [[lastName]]!");
printLine(output);
getVariable(String,Integer)
Returns the value at the given index with the given name from the Parser instance.
Declaration
String getVariable(String name, Integer row)
Parameters
Type | Name | Description |
---|---|---|
String | name | The name of the variable to return. |
Integer | row | The zero-based index of the value to return. |
Returns
Type | Description |
---|---|
String | The value at the given index with the given name. |
Examples
Parser p;
p.setVariable("firstName", "Jack");
p.setVariable("lastName", "Black");
String output = p.parseString("Hello [[firstName]] [[lastName]]!");
printLine(output);
getVariableCount(String)
Returns the number of values for the given variable in the Parser instance.
Declaration
Integer getVariableCount(String name)
Parameters
Type | Name | Description |
---|---|---|
String | name | The name of the variable. |
Returns
Type | Description |
---|---|
Integer | The number of values of the given variable. |
Examples
Parser p;
p.setVariable("firstName", "Jack");
p.setVariable("lastName", "Black");
String output = p.parseString("Hello [[firstName]] [[lastName]]!");
printLine(output);
parseString(String)
Parses a text and return the result.
Declaration
String parseString(String stringToParse)
Parameters
Type | Name | Description |
---|---|---|
String | stringToParse | The string containing the text to parse. |
Returns
Type | Description |
---|---|
String | The parsed result. |
Examples
Parser p;
p.setVariable("firstName", "Jack");
p.setVariable("lastName", "Black");
String output = p.parseString("Hello [[firstName]] [[lastName]]!");
printLine(output);
setVariable(String,String)
Sets the variable with the given name to the given value in the Parser instance.
Declaration
Void setVariable(String name, String value)
Parameters
Type | Name | Description |
---|---|---|
String | name | The name of the variable to set. |
String | value | The value to set. |
Returns
Type | Description |
---|---|
Void |
Remarks
Existing values will be overwritten with this new value.
Examples
Parser p;
p.setVariable("firstName", "Jack");
p.setVariable("lastName", "Black");
String output = p.parseString("Hello [[firstName]] [[lastName]]!");
printLine(output);
toParser(Parser)
Copies all values in the input parser to the original parser.
Declaration
toParser(Parser parser)
Parameters
Type | Name | Description |
---|---|---|
Parser | parser | The parser to copy the values from. |
Examples
Parser p;
p.setVariable("firstName", "Jack");
p.setVariable("lastName", "Black");
String output = p.parseString("Hello [[firstName]] [[lastName]]!");
printLine(output);