Blockly.Blocks['controls_forEach'] = { /** * Block for 'for each' loop. * @this Blockly.Block */ init: function() { this.jsonInit({ "message0": "for each item %1 in list %2 : ", //Blockly.Msg.CONTROLS_FOREACH_TITLE, "args0": [ { "type": "input_value", "name": "VAR", "check": "Tuple" }, { "type": "input_value", "name": "LIST", "check": "Array" } ], "inputsInline": true, "previousStatement": null, "nextStatement": null, "colour": Blockly.Blocks.loops.HUE, "helpUrl": Blockly.Msg.CONTROLS_FOREACH_HELPURL }); this.appendStatementInput('DO') .appendField(Blockly.Msg.CONTROLS_FOREACH_INPUT_DO); this.setInputsInline(true); // Assign 'this' to a variable for use in the tooltip closure below. var thisBlock = this; this.setTooltip(function() { return Blockly.Msg.CONTROLS_FOREACH_TOOLTIP.replace('%1', Blockly.Python.valueToCode(thisBlock, 'VAR', Blockly.Python.ORDER_RELATIONAL) || '___'); }); }, customContextMenu: Blockly.Blocks['controls_for'].customContextMenu }; Blockly.Python['controls_forEach'] = function(block) { // For each loop. var variable0 = Blockly.Python.valueToCode(block, 'VAR', Blockly.Python.ORDER_RELATIONAL) || '___'; var argument0 = Blockly.Python.valueToCode(block, 'LIST', Blockly.Python.ORDER_RELATIONAL) || '___'; var branch = Blockly.Python.statementToCode(block, 'DO'); branch = Blockly.Python.addLoopTrap(branch, block.id) || Blockly.Python.PASS; var code = 'for ' + variable0 + ' in ' + argument0 + ':\n' + branch; return code; };