{{# const srcTable = Table.findOne({ name: query.srcTable }) }}
{{# const view = View.findOne({ name: query.view_name }) }}
{{# const destTable = Table.findOne({ id: view.table_id }) }}

The extra state formula when embedding or linking to a view is used
to influence the row that will be included in that views display, 
in addition to any selection based on a chosen relationship.

The formula takes the form of a JavaScript object where the object 
keys are fields (variable names) on the table of the embedded view,
and the values are the JavaScript expressions for those fields which set the criteria 
for the rows to be included. For example, if you would like to display 
all rows where the Active field is true you would use this as the extra
state formula: `{ active: true }`. This is an object where the only
key is `active` and its value is `true`. You can have more than one 
key, for example: 

```
{ active: true, age: {gt: 21}, manager: user.id }
``` 
(See below for using the `user` variable and greater-than filters)

The keys you can use are the variable names on the {{destTable.name}} table:

| Field | Variable name | Type |
| ----- | ------------- | ---- |
{{# for (const field of destTable.fields) { }} | {{ field.label }} | `{{ field.name }}` | {{ field.pretty_type }} | 
{{# } }}

In the value expressions (to the right of the colon) you can use 
literals (e.g. 17 for numbers or true for booleans; string literals 
must be enclosed in quotes (single, double or 
[backtick](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals))). 
You can also as the value use a more complex JavaScript expression.

For examples of how you can use these values for greater than/less than, 
or-conditions or case insensitivity, see [here](/admin/jsdoc/classes/_saltcorn_data.models_table.Table.html#querying-table-rows).

You can refer to the logged in user using the variable name user. 
This is an object and you must use the dot to access user fields,
e.g. user.id for the logged in users id. A very common scenario 
is to restrict rows to show to those that belong to the current user. 
If you have a key to users field called `theUser`, you would use this 
as the extra state formula: `{theUser: user.id}`

The user object has other fields than the primary key identifier.
For your login session the user object is:

```
{{JSON.stringify(user, null, 2)}}
```
{{# const userkey = oneOf(Object.keys(user)) }}

For example to use the `{{userkey}}` field on the user object, write `user.{{userkey}}`
It should be taken into account that if the users table row has changed,
you should call `refresh_user_session` action to update the session variables
(or the user should re-login).

{{# if(srcTable) { }}
You can also use variables from the currently displayed row in the value expressions, 
in this case the table {{srcTable.name}}:

| Field | Variable name | Type |
| ----- | ------------- | ---- |
{{# for (const field of srcTable.fields) { }} | {{ field.label }} | `{{ field.name }}` | {{ field.pretty_type }} | 
{{# } }}

{{# } }}

The state values from the query string can also be accessed, by preceeding the 
variable name by a dollar sign (in pages and Show views). For instance if your URL ends in `?id=45&foo=1`, 
you can access the value of `foo` (here 1) as `$foo`. In Filter views, there is no 
underlying row and the purpose of the view is fundamentally to manipulate the state
so the state in extra state formulae is accessed without dollar sign; in the 
previous example `foo` instead of `$foo`.

Use `session_id` to refer to the current session ID string (unique for the user's session
whether the are logged in or not).
