# Context

There are no global variables in the Gentee language. One of the ways to exchange data is a special associative array of strings (*context*). Any function can safely add key-value pairs there or get a value by key. In addition, the context has the ability to substitute other existing values from the context. For example, if the pairs *"a": "String A"* and *"b": "String B"* has been defined, then *"#a# and #b#"* will return *"String A and String B"*. The functions and operators for working with the context are described below.

* [Ctx( str input ) str](/stdlib/context.md#ctx-str-input-str)
* [CtxGet( str key ) str](/stdlib/context.md#ctxget-str-key-str)
* [CtxIs( str key ) bool](/stdlib/context.md#ctxis-str-key-bool)
* [CtxSet( str key, str val ) str](/stdlib/context.md#ctxset-str-key-str-val-str)
* [CtxSet( str key, bool b ) str](/stdlib/context.md#ctxset-str-key-bool-b-str)
* [CtxSet( str key, float f ) str](/stdlib/context.md#ctxset-str-key-float-f-str)
* [CtxSet( str key, int i ) str](/stdlib/context.md#ctxset-str-key-int-i-str)
* [CtxValue( str key ) str](/stdlib/context.md#ctxvalue-str-key-str)

## Operators

| Operator           | Result | Description                                                               |
| ------------------ | ------ | ------------------------------------------------------------------------- |
| **#** ident        | str    | The same as *CtxGet(key)*, where *ident* is the context key.              |
| **##** str         | str    | The same as *Ctx(str)*. Specify any expression that returns a string.     |
| ident **#=** str   | str    | The same as *CtxSet(str key, str s)*, where *ident* is the context key.   |
| ident **#=** bool  | str    | The same as *CtxSet(str key, bool b)*, where *ident* is the context key.  |
| ident **#=** float | str    | The same as *CtxSet(str key, float f)*, where *ident* is the context key. |
| ident **#=** int   | str    | The same as *CtxSet(str key, int i)*, where *ident* is the context key.   |

```go
run str {
  str s = ` #AºB#`
  AºB #= `ººº`
  b #= 71
  CD #= `#AºB# #b# == ` 
  return #CD + #b + ##s
}
// ººº 71 == 71 ººº
```

## Functions

### Ctx(str input) str

The *Ctx* function replaces substrings **#keyname#** in *input* string with the value of the corresponding key, if it exists.

```
run str {
    CtxSetBool(`qq`, true)
    CtxSetFloat(`ff`, 3.1415)
    CtxSet(`out`, "it is #qq# that PI equals #ff#")
    return Ctx("#out#. #notexist#")
}
// it is true that PI equals 3.1415. #notexist#
```

### CtxGet(str key) str

The *CtxGet* function gets the value of the *key* key, replaces all occurrences of other keys in it, and returns the resulting string. If the specified key is missing, an empty string is returned.

```
func init {
   CtxSet(`a1`, `end`)
   CtxSet(`a2`, `=#a1#=`)
   CtxSet(`a3`, `+#a2#+#a1#`)
}

run str {
    init()
    return CtxGet(`a3`)
}
// +=end=+end
```

### CtxIs(str key) bool

The *CtxIs* function returns *true* if there is a value with the specified key in the context. Otherwise, it returns *false*.

### CtxSet(str key, str val) str

The *CtxSet* function adds a key and a value to the context. If the key already exists, it will be assigned a new value. The function returns the value of the key.

### CtxSet(str key, bool b) str

The *CtxSet* function adds a key and a logical value *b* to the context. A boolean value will be converted to the string *true* or *false*. The function returns the value of the key.

### CtxSet(str key, float f) str

The *CtxSet* function adds a key and a floating-point number *f* to the context. The number will be converted to a string. The function returns the value of the key.

### CtxSet(str key, int i) str

The *CtxSet* function adds a key and an integer *i* to the context. The number will be converted to a string. The function returns the value of the key.

### CtxValue(str key) str

The *CtxValue* function returns the value of the *key* key as is. Unlike the **CtxGet** function, it does not replace occurrences of other keys. If the specified key is missing, an empty string is returned.

```
run str {
    CtxSet(`test`, `?value`)
    CtxSet(`param`, `#test# ==`)
    return CtxValue(`param`) + CtxValue(`nop`) + CtxGet(`param`)
}
// #test# ==?value ==
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.gentee.org/stdlib/context.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
