How to use T in layout.html in Py4web?

80 views
Skip to first unread message

Ruslan Gareev

unread,
Oct 7, 2019, 9:06:40 AM10/7/19
to web2py-users
Using T in Layout.html is make me to copy-paste code from common.py to layout.html
[[from py4web import Translator]]
[[from ..settings import T_FOLDER]]
[[T = Translator(T_FOLDER)]]

<html>
  some code
 
...
<a href="[[=URL('auth/register')]]">[[=T('Sign in')]]
...
</html>
How to make that no repeat code?

Massimo Di Pierro

unread,
Oct 11, 2019, 2:19:17 AM10/11/19
to web2py-users
No. You should not define T in your view and your should not define T in your actions.

T should be defined outside actions (for example it is defined in common.py in _scaffold).

Your actions should declare that they need it and pass it to the view:

@action('index')
@action.uses(index.html)
@action.uses(T)
def index():
     return dict(T=T)

now index.html can see and use T

Ruslan Gareev

unread,
Oct 11, 2019, 12:46:49 PM10/11/19
to web...@googlegroups.com
Thanks. But how about auth controller?
When i open an auth page, with T in layout.html, it falls with error: "T is undefined".

пятница, 11 октября 2019 г., 11:19:17 UTC+5 пользователь Massimo Di Pierro написал:

Massimo Di Pierro

unread,
Oct 12, 2019, 1:08:48 AM10/12/19
to web2py-users
Good point. Need to change the auth object so you can pass it. Let me think about it.
Reply all
Reply to author
Forward
0 new messages