B
B
bsbak2017-04-06 18:26:14
go
bsbak, 2017-04-06 18:26:14

How to get to a variable from any template?

You have to pass a value from different handler functions to each template!
Example:

//MainHandler
user := session.Get("userInfo")
//...
template.Execute(wr, struct{"user", user})
//-------------------------
//ProfileHandler
user := session.Get("userInfo")
//...
template.Execute(wr, struct{"user", user})
//------------------------------
// и т.д.

Why not do the same thing in each handler, how to be?
For example, in php you can save data to the session and take it in any template
//при регистрации установили в сессию юзерИнфо
$_SESSION['userInfo'] = $userInfo;

//потом в любом шаблоне берем данные
<div><?=$_SESSION['userInfo']['username']?></div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
index0h, 2017-04-06
@index0h

You don't have to want it. The session example is shit code. Passing explicitly what will be used is correct in most cases.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question