1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-09 11:29:58 +00:00
TiddlyWiki5/editions/tw5.com/tiddlers/variables/Global Definitions.tid
jeremy@jermolene.com 6847906bea Docs typo
2022-09-16 16:33:04 +01:00

66 lines
2.6 KiB
Plaintext

title: Global Definitions
created: 20220909111836951
modified: 20220909111836951
tags: Concepts Reference Variables
!! Introduction
<<.from-version "5.3.0">> Globals are [[variables|Variables]] that are available everywhere, without needing a [[<$set>|SetWidget]], [[<$vars>|SetWidget]] or [[<$let>|SetWidget]] widget.
!! Defining Globals
Previously, definitions of procedures, widgets, functions and macros were made available globally by tagging them with `$:/tags/Macro`. While this mechanism is still supported, it suffers from several disadvantages and so is not recommended for new applications. The new way to define global procedures, widgets, functions and macros is to place them in tiddlers titled with the name of the global prefixed with `$:/global/`.
For example, the global variable `foo` would be defined in a tiddler called `$:/global/foo`. Accessing the variable `<<foo>>` then acts as a shortcut for accessing the underlying global variable tiddler.
!! Special Fields
The following special fields are used to define the behaviour of the global:
* `_parameters` defines the parameters expected by procedures, widgets and functions
* `_is_procedure`, `_is_widget`, `_is_function`, `_is_macro`: any one of these fields may be set to `yes` to indicate the type of the definition
<<.note """The `_parameters` field is only strictly necessary for functions and macros. Procedures and widgets can instead choose to use the <<.wlink ParametersWidget>> widget (or a pragma) within the body of the definition""">>
!! Viewing Globals
The current global definitions are listed in the sidebar in the "More" -> "Globals" tab.
By default, global definition tiddlers are displayed with a custom template that makes it easier to see the full definition.
!! Local Variables within Globals
Note that global definitions can include local variables that are defined before the body of the global. These local variables will not be visible externally.
For example:
```
title: $:/globals/foo
_is_procedure: yes
_parameters: (param1:"value",param2:"value")
\procedure renderTitle(title)
<div class="mytitle"><$text text=<<title>>/></div>
\end
\function myfn(a)
[[a]getvariable[]addprefix[!]]
\end
<$list filter=<<param1>>>
<<renderTitle "first">>: <$text text=<<param2>>/>
<<renderTitle "second">>: <$text text=<<myfn param2>>/>
</$list>
```
It is possible to allow the caller to override these local definitions by using the new syntax for conditional definitions. For example, here we only define the function `myfn` if the variable `myfn` is not already defined:
```
\?function myfn(a)
[[a]getvariable[]addprefix[!]]
\end
```