Mercurial > libervia-web
changeset 1286:63328c793a9e
pages: new method `exposeToScripts` to make variables accessible to scripts in browser
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 19 Jun 2020 16:47:51 +0200 |
parents | 37a582b0fe53 |
children | 1f26d8c2afc1 |
files | libervia/server/pages.py |
diffstat | 1 files changed, 19 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/libervia/server/pages.py Fri Jun 19 16:47:51 2020 +0200 +++ b/libervia/server/pages.py Fri Jun 19 16:47:51 2020 +0200 @@ -45,10 +45,11 @@ from sat.core.log import getLogger from sat_frontends.bridge.bridge_frontend import BridgeException -from libervia.server.constants import Const as C -from libervia.server import session_iface -from libervia.server.utils import quote, SubPage -from libervia.server.classes import WebsocketMeta +from .constants import Const as C +from . import session_iface +from .utils import quote, SubPage +from .classes import WebsocketMeta +from .classes import Script log = getLogger(__name__) @@ -541,6 +542,20 @@ ) self.pageError(request, C.HTTP_FORBIDDEN) + def exposeToScripts( + self, + request: server.Request, + **kwargs: str + ) -> None: + """Make a local variable available to page script as a global variable + + No check is done for conflicting name, use this carefully + """ + template_data = request.template_data + scripts = template_data.setdefault("scripts", utils.OrderedSet()) + for name, value in kwargs.items(): + scripts.add(Script(content=f"var {name}={value!r};")) + def registerURI(self, uri_tuple, get_uri_cb): """Register a URI handler