Statement blocks are blocks without output connections. These act like statements in text-based languages.
Just like all blocks in Blockly, statement blocks can be turned into code strings by defining a block-code generator.
import {javascriptGenerator} from 'blockly/javascript';
javascriptGenerator.forBlock['custom_block'] = function(block, generator) {
// Collect argument strings.
const fieldValue = block.getFieldValue('MY_FIELD');
const innerCode = generator.statementToCode(block, 'MY_STATEMENT_INPUT');
// Return code.
return 'my code string';
}
Collect argument strings
All block-code generators require collecting the values of fields and collecting the code of inner blocks.
// Collect field values.
const fieldValue = block.getFieldValue('MY_FIELD');
// Collect inner block code strings.
const innerCode = generator.statementToCode(block, 'MY_STATEMENT_INPUT');
If you reference the code of an inner block multiple times, you should add argument caching to your block.
Return values
The return type of a statement block-code generator is a code string.
return 'my code string';