Inner blocks
Stay organized with collections
Save and categorize content based on your preferences.
Inner blocks are the blocks attached to your value and statement inputs.
Individual block-code generators need to handle the concatenation of their inner
blocks so the code gets added in the correct place.
import {javascriptGenerator, Order} from 'blockly/javascript';
javascriptGenerator.forBlock['my_custom_block'] = function(block, generator) {
// Generate innner block code.
const statement = generator.statementToCode(block, 'MY_STATEMENT_INPUT');
const value = generator.valueToCode(block, 'MY_VALUE_INPUT', Order.ATOMIC);
// Concatenate the string.
const code = `some code ${statement} ${value} some more code`;
// Return the code.
return code;
}
The code of inner blocks attached to statement inputs can be generated using
statementToCode
. This calls the
statement block's block-code generator and handles
indenting code.
const statement = generator.statementToCode(block, 'MY_STATEMENT_INPUT');
You only need to call statementToCode
for the inner block directly connected
to a statement input.
The code of inner blocks attached to value inputs can be generated using
valueTocode
. This calls the
value block's block-code code generator and handles
adding parentheses around inner blocks' code when necessary.
See the parentheses documentation for more information about how
to control parentheses.
const value = generator.valueToCode(block, 'MY_VALUE_INPUT', Order.ATOMIC);
Concatenate code
After you've gotten your inner block's code string, you can concatenate it in
the correct place with your code string.
const code = `some code ${statement} ${value} some more code`;
Return code
Different types of blocks require the code string to be returned in different
ways, so check out their individual pages for more information:
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-09-18 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-09-18 UTC."],[[["Inner blocks attached to custom blocks need to have their generated code properly concatenated within the custom block's code."],["`statementToCode` is used to generate and properly indent code for inner blocks in statement inputs, while `valueToCode` handles code generation and parenthesization for inner blocks in value inputs."],["The code for inner blocks is retrieved as strings which are then concatenated with the main block's code to produce the final output."],["Different block types have different return requirements, so refer to specific documentation for blocks with or without outputs to ensure correct code generation."]]],["Inner blocks' code is generated using `statementToCode` for statement inputs and `valueToCode` for value inputs. `statementToCode` handles indenting, while `valueToCode` manages parentheses. Individual generators are needed to concatenate these inner block codes into the correct placement within the custom code string. The final step is returning this complete code string, with different block types needing distinct return methods, which depends on if they have outputs or not.\n"]]