Passing data#
Some widgets contain fixed text, others can show dynamic contents For example:
Const("Hello, {name}!")
will be rendered asHello, {name}!
Format("Hello, {name}!")
will interpolate with window data and transformed to something likeHello, Tishka17!
So, widgets can use data. But data must be loaded from somewhere. To do it Windows and Dialogs have getter
attribute.
Getter can be either a function returning data or static dict or list of such objects.
So let’s create a function and use it to enrich our window with data.
Note
In this and later examples we will skip common bot creation and dialog registration code unless it has notable differences with quickstart
from aiogram.filters.state import StatesGroup, State
from aiogram_dialog import Window, Dialog
from aiogram_dialog.widgets.kbd import Button
from aiogram_dialog.widgets.text import Const, Format
class MySG(StatesGroup):
main = State()
async def get_data(**kwargs):
return {
"name": "Tishka17",
}
dialog = Dialog(
Window(
Format("Hello, {name}!"),
Button(Const("Useless button"), id="nothing"),
state=MySG.main,
getter=get_data, # here we set our data getter
)
)
It will look like:
Since version 1.6 you do not need getter to access some common objects:
dialog_data
-contents of corresponding field from current context. Normally it is used to store data between multiple calls and windows withing single dialogstart_data
- data passed during current dialog start. It is also accessible usingcurrent_context
middleware_data
- data passed from middlewares to handler. Same asdialog_manager.data
event
- current processing event which triggered window update. Be careful using it, because different types of events can cause refreshing same window.