由于您定义了 place_name 常量,因此无需在多个位置手动将“旧金山”更改为“湾区”。使用 place_name 常量对 San Francisco 的引用将替换为 Bay Area,因此 Looker 将在“探索”菜单和“探索”标题中显示 Bay Area Users 和 Bay Area Orders。
示例:对多个字段的负值应用相同的格式
假设您希望负数据值在图表或查询中显示时以红色显示并用括号括起来。
将此格式设置为 LookML 常量的值后,您只需使用 Liquid 变量和 HTML 即可指定一次格式。然后,您可以随时引用该常量,将该格式应用于字段。
例如,您可以创建一个名为 negative_format 的常量,以便将此格式应用于字段:
constant: negative_format {
value: "{% if value < 0 %}
<p style='color:red;'>({{rendered_value}})</p>
{% else %}
{{rendered_value}}
{% endif %}"
}
此代码会创建常量 negative_format,用于指定负数据值应使用红色字体并用圆括号括起来。然后,您可以使用 html 参数将此格式应用于数据集中的维度和测量。
例如,您可以创建 type: sum 的总金额衡量标准,并将 @{negative_format} 指定为 html 参数的值:
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-05。"],[],[],null,["# Maximizing code reusability with DRY LookML: Defining a string once to use throughout your LookML project\n\n| **Note:** This page is part of the [Looker cookbook: Maximizing code reusability with DRY LookML](/looker/docs/best-practices/dry-lookml-cookbook).\n\nYou can use the LookML `constant` parameter within your project's manifest file to define a string that can be used throughout your project. LookML constants can be useful when you need to define a specific string --- such as a number, a name, or HTML formatting for field values --- and reuse that value throughout your project.\n\nThis page includes the following examples of using LookML constants to define and maintain reusable strings values in once place:\n\n- [Using the same string in the labels of multiple Explores](#example_using_the_same_string_in_the_labels_of_multiple_explores): This technique helps you save time if you reuse common strings, such as words, phrases, or location names in your definitions.\n- [Applying the same formatting to negative values for multiple fields](#example_applying_the_same_formatting_to_negative_values_for_multiple_fields): This technique helps you save time by defining conditional formatting specifications to use on multiple fields.\n\nIngredients\n-----------\n\n- Your project's [manifest file](/looker/docs/lookml-project-files#project_manifest_files)\n- The LookML [`constant`](/looker/docs/reference/param-manifest-constant) parameter\n- `@{constant_name}` syntax to reference existing constants\n- The LookML [`html`](/looker/docs/reference/param-field-html) parameter\n- [Liquid variables](/looker/docs/liquid-variable-reference)\n\nPrerequisites\n-------------\n\n- [A configured LookML model](/looker/docs/generating-a-model#when_you_need_to_configure_models)\n- [Permissions to develop LookML](/looker/docs/admin-panel-users-roles#permission_sets)\n\nExample: Using the same string in the labels of multiple Explores\n-----------------------------------------------------------------\n\nSuppose you want to create two Explores, labeled **San Francisco Users** and **San Francisco Orders** in the UI, but you don't want to manually type the text for each label.\n\nTo do this, you can define a constant `place_name` with the value `\"San Francisco\"` in the [project manifest file](/looker/docs/other-project-files#project_manifest_files) for your project: \n\n constant: place_name {\n value: \"San Francisco\"\n }\n\nThis constant can then be referenced in any part of your project where a string is accepted, using the syntax `@{place_name}`. In this example, you can define the `users` and `orders` Explores, specifying `\"@{place_name} Users\"` and `\"@{place_name} Orders\"` as values for the [`label` parameter](/looker/docs/reference/param-explore-label), as in the following example: \n\n\n explore: users {\n label: \"@{place_name} Users\"\n }\n\n explore: orders {\n label: \"@{place_name} Orders\"\n }\n\nIn this example, Looker displays **San Francisco Users** and **San Francisco Orders** in the Explore menu and in the titles of the Explores, rather than the default **Users** and **Orders** labels.\n\nSuppose you want to update all the references to **San Francisco** to **Bay Area**.\n\nRather than having to update each reference manually, you only have to make a single update to the `place_name` constant in the manifest file for your project: \n\n constant: place_name {\n value: \"Bay Area\"\n }\n\nSince you defined the `place_name` constant, you don't have to manually change **San Francisco** to **Bay Area** in multiple places. The references to **San Francisco** with the `place_name` constant will be replaced with **Bay Area** , so Looker will display **Bay Area Users** and **Bay Area Orders** in the Explore menu and in the titles of the Explores.\n\nExample: Applying the same formatting to negative values for multiple fields\n----------------------------------------------------------------------------\n\nImagine that you want negative data values to be displayed in red and within parentheses wherever they appear in charts or queries.\n\nBy setting this formatting as the value for a LookML constant, you can specify the formatting just once by using [Liquid variables](/looker/docs/liquid-variable-reference) and [HTML](/looker/docs/reference/param-field-html). Then, you can reference the constant whenever you want to apply that formatting to a field.\n\nFor example, you can create a constant called `negative_format` that you can use to apply this formatting to a field: \n\n\n constant: negative_format {\n value: \"{% if value \u003c 0 %}\n \u003cp style='color:red;'\u003e({{rendered_value}})\u003c/p\u003e\n {% else %}\n {{rendered_value}}\n {% endif %}\"\n }\n\nThis code creates the constant `negative_format`, which specifies that negative data values should have a red font and be surrounded by parentheses. You can then apply this formatting to dimensions and measures in your dataset by using the [`html` parameter](/looker/docs/reference/param-field-html).\n\nFor example, you can create **Total Amount** measure of [`type: sum`](/looker/docs/reference/param-measure-types#sum) and specify `@{negative_format}` as the value for the `html` parameter: \n\n\n measure: total_amount {\n type: sum\n value_format_name: usd\n sql: ${amount} ;;\n html: @{negative_format} ;;\n }\n\nIn your table, negative values for the **Total Amount** measure will be formatted as specified in the `negative_format` constant definition, with a red font and surrounded by parentheses.\n\nIf you want to apply the same formatting to negative values for other fields, you can reference the `negative_format` constant in the `html` parameter for those fields."]]