[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間: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."]]