You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docusaurus/docs/how-to-guides/data-source-plugins/add-support-for-variables.md
+9-9
Original file line number
Diff line number
Diff line change
@@ -203,15 +203,15 @@ That's it! You can now try out the plugin by adding a [query variable](https://g
203
203
204
204
To interpolate template variables, you need to import the `getTemplateSrv()` function from the `@grafana/runtime` package:
205
205
206
-
```
206
+
```ts
207
207
import { getTemplateSrv } from'@grafana/runtime';
208
208
```
209
209
210
210
The `getTemplateSrv()` function returns an instance of `TemplateSrv` which provides methods for working with template variables. The most important one, `replace()`, accepts a string containing variables as input and returns an interpolated string, where the variables have been replaced with the values that the users have selected.
211
211
212
212
For example, if you have a variable called `instance`, the following code replaces the variable with its corresponding value:
213
213
214
-
```
214
+
```ts
215
215
getTemplateSrv().replace("I'd like $instance, please!");
216
216
217
217
// I'd like server-1, please!
@@ -234,16 +234,16 @@ In the previous example, the variables only had one value, `server-1`. However,
234
234
235
235
For example, which of these different formats would suit your use case?
236
236
237
-
```
237
+
```ts
238
238
{server-1, server-2, server-3} (Graphite)
239
239
["server-1", "server-2", "server-3"] (JSON)
240
240
("server-1"OR"server-2"OR"server-3") (Lucene)
241
241
```
242
242
243
243
Fortunately, the `replace()` method lets you pass a third argument to allow you to choose from a set of predefined formats, such as the CSV format:
244
244
245
-
```
246
-
getTemplateSrv().replace("I'd like $instance, please!", {}, "csv");
245
+
```ts
246
+
getTemplateSrv().replace("I'd like $instance, please!", {}, 'csv');
247
247
248
248
// I'd like server-1, server-2, server-3, please!
249
249
```
@@ -262,7 +262,7 @@ After reviewing the advanced variable format options, you may find that you want
262
262
263
263
You can pass an interpolation function to `replace()` instead of a string as the third argument. The following example uses a custom formatter function to add an `and` before the last element:
0 commit comments