I am converting a string into a Google Blockly block using JavaScript.
The input string is something like "Hello %s World"
- where %s
defines a string input. I need to turn that into:
Blockly.Blocks['blockname'] = {
init: function() {
this.appendDummyInput()
.appendField("Hello ")
.appendField(new Blockly.FieldTextInput("input1"), "")
.appendField(" World");
}
};
But I'm not sure how to achieve this without using eval()
, and as the input string is from the user, I understand that using eval()
would not be a good idea.
My current code is:
currentLine = blockText[x].split(/(%s)/);
for( var y = 0; y < currentLine.length; y++ )
{
if( currentLine[y] == "" )
{
//do nothing
}
else if( currentLine[y] == "%s" )
{
//create a input
}
else
{
//create a label
}
}
But I'm not quite sure how to create the Blockly code that I need, without building up the JavaScript in a string and then using eval()
at the end.
Could someone please assist me with this?