با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
بیشتر مقادیر فیلد آماده هستند تا فوراً به رشته کد شما الحاق شوند. با این حال، برخی از مقادیر فیلد قبل از قابل استفاده شدن نیاز به کار اضافی دارند.
رشته ها
رشته ها باید با quote_ یا multiline_quote_ نقل قول شوند قبل از اینکه بتوان آنها را به هم متصل کرد. این توابع فرار از کاراکترهای خاص زبان را انجام می دهند، مانند جایگزینی ' با \' در جاوا اسکریپت.
// For a single line text field.conststr=generator.quote_(block.getFieldValue('STR'));// For a multiline text field.conststr=generator.multiline_quote_(block.getFieldValue('STR'));
متغیرها
برای فیلد متغیر داخلی، getFieldValue یک شناسه متغیر برمیگرداند، نه نام متغیر. برای به دست آوردن نام متغیر واقعی، getVariableName در تولید کننده کد فراخوانی کنید. این یک نام منحصر به فرد و قانونی را برمی گرداند. در میان چیزهای دیگر، getVariableName :
کاراکترهای غیر ASCII را به ASCII تبدیل می کند. این امر ضروری است زیرا کاربران می توانند نام متغیرها را به زبان خود وارد کنند. برای مثال، « متغير » را به « _D9_85_D8_AA_D8_BA_D9_8A_D8_B1 » تبدیل می کند.
اطمینان حاصل می کند که نام متغیرها از قوانین تعیین شده توسط زبان های برنامه نویسی پیروی می کنند. برای مثال، فاصله ها را به زیرخط تبدیل می کند و پیشوند my_ به نام متغیرهایی که با یک رقم شروع می شوند اضافه می کند.
تضادها را با کلمات رزرو شده یا نام متغیر یا تابع دیگر برطرف می کند. به عنوان مثال، آن را forfor2 تبدیل می کند.
برای فیلد کشویی داخلی، getFieldValue یک رشته زبان خنثی را برمیگرداند که ممکن است مستقیماً در کد قابل استفاده نباشد. برای مثال، یک منوی کرکره ای حاوی عملگرهای مقایسه ممکن است 'EQUALS' ، 'LESS' یا 'GREATER' را برگرداند. این می تواند برای جستجوی رشته ای که در کد استفاده می شود استفاده شود.
برای فیلد چک باکس داخلی، getFieldValue'TRUE' یا 'FALSE' را برمیگرداند. نحوه استفاده از این به معنای چک باکس بستگی دارد. به عنوان مثال، ممکن است از آن برای انشعاب در حین تولید کد استفاده کنید.
تاریخ آخرین بهروزرسانی 2025-07-25 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-25 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eFields in Blockly define user-editable values (strings, numbers, colors, etc.) for code generation.\u003c/p\u003e\n"],["\u003cp\u003eAccess field values using \u003ccode\u003egetFieldValue\u003c/code\u003e, transform them into usable strings (e.g., quoting strings, scrubbing variable names), and concatenate them into the code.\u003c/p\u003e\n"],["\u003cp\u003eUse \u003ccode\u003equote_\u003c/code\u003e or \u003ccode\u003emultiline_quote_\u003c/code\u003e to properly format string values for code generation.\u003c/p\u003e\n"],["\u003cp\u003eUse \u003ccode\u003egetVariableName\u003c/code\u003e to ensure variable names are valid and avoid conflicts in the generated code.\u003c/p\u003e\n"],["\u003cp\u003eRefer to specific block type documentation for details on returning the generated code.\u003c/p\u003e\n"]]],["Code generation with fields involves retrieving user-inputted values, such as strings or numbers, from fields. `getFieldValue` accesses these values, which vary by field type. Strings need quoting via `quote_` or `multiline_quote_`, while variables require scrubbing with `getVariableName` to ensure they are ASCII and don't clash with reserved words. Finally, the processed field value is inserted into the code string. The completed code string is returned depending on the specific type of block.\n"],null,["# Transform field values\n\nMost field values are ready to be concatenated to your code string immediately.\nHowever, some field values require extra work before they are usable.\n\nStrings\n-------\n\nStrings need to be quoted with either `quote_` or `multiline_quote_` before they\ncan be concatenated. These functions perform language-specific character\nescaping, such as replacing `'` with `\\'` in JavaScript. \n\n // For a single line text field.\n const str = generator.quote_(block.getFieldValue('STR'));\n\n // For a multiline text field.\n const str = generator.multiline_quote_(block.getFieldValue('STR'));\n\n| **Note:** the `quote_` and `multiline_quote_` methods follow an earlier naming convention with underscores, but they are public.\n\nVariables\n---------\n\nFor the built-in variable field, `getFieldValue` returns a variable ID, not a\nvariable name. To get an actual variable name, call `getVariableName` in the\ncode generator. This returns a name that is both unique and legal. Among other\nthings, `getVariableName`:\n\n- Converts the non-ASCII characters to ASCII. This is necessary because users can enter variable names in their own language. For example, it converts \"`متغير`\" to \"`_D9_85_D8_AA_D8_BA_D9_8A_D8_B1`\".\n- Ensures variable names follow the rules set forth by programming languages. For example, it converts spaces to underscores and adds the prefix `my_` to variable names that start with a digit.\n- Resolves conflicts with reserved words or other variable or function names. For example, it converts `for` to `for2`.\n\n const identifier = generator.getVariableName(block.getFieldValue('VAR'));\n\nDropdowns\n---------\n\nFor the built-in dropdown field, `getFieldValue` returns a language-neutral\nstring that might not be directly usable in code. For example, a dropdown\ncontaining comparison operators might return `'EQUALS'`, `'LESS'`, or\n`'GREATER'`. This can be used to look up a string that is used in code. \n\n const OPERATORS = {\n EQUALS: '==',\n LESS: '\u003c',\n GREATER: '\u003e',\n };\n const operator = OPERATORS[block.getFieldValue('OPERATOR')];\n\nCheckboxes\n----------\n\nFor the built-in checkbox field, `getFieldValue` returns `'TRUE'` or `'FALSE'`.\nHow you use this depends on the meaning of the checkbox. For example, you might\nuse it for branching while generating code."]]