Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twig global variable doesnt work #24

Closed
nzhtrtlc opened this issue Feb 14, 2017 · 5 comments
Closed

Twig global variable doesnt work #24

nzhtrtlc opened this issue Feb 14, 2017 · 5 comments

Comments

@nzhtrtlc
Copy link

nzhtrtlc commented Feb 14, 2017

Hi im adding global variable in my login controller.

  if($user){
            $user_credentials["user_session"] = [
                "user_id" => $user->user_id,
                "fullName" => $user->name. " ".$user->surname,
                "email" => $user->email,
                "logged_in" => true
            ];
            $this->session->set_userdata($user_credentials);
            $this->twig->addGlobal('user_session',$user_credentials["user_session"]);
            redirect('dashboard');
        }

In my dashboard.twig file im calling {{ user_session.fullName}} but it doesnt work.
BUT
If i use addGlobal method in my dashboard i can see the fullName variable. In this case it doesnt work for another controllers, works for only dashboard. But i need to use it in every controller.

@kenjis
Copy link
Owner

kenjis commented Feb 14, 2017

This is not a bug in Twig or codeigniter-ss-twig. It is specification.

redirect() makes your browser request another page (in your case, dashboard).
HTTP is stateless protocol. And PHP also shares nothing basically.
So if you access another page, PHP don't know all variables in the previous page at all.

Twig's global also means global in a single request.

But i need to use it in every controller.

Yes.

@kenjis kenjis closed this as completed Feb 14, 2017
@nzhtrtlc
Copy link
Author

Thank you for your answer.
Is there any way that i can define global variable for twig which is works in every request ?

@kenjis
Copy link
Owner

kenjis commented Feb 15, 2017

If you need one variable in all pages, for example, you can set it in your MY_Controller constructor.
See https://codeigniter.com/user_guide/general/core_classes.html#extending-core-class.

@nzhtrtlc
Copy link
Author

Thanks this one can solve my problem. But i think you can add this feature to project then we can reach the session with session key in twig. So we do not need to add any variable or make our own controllers.

@kenjis
Copy link
Owner

kenjis commented Feb 16, 2017

I intend to keep this library simple wrapper for Twig.
I don't add functionality which original Twig does not have.
Twig doesn't care about session.

You can extend my Twig class to add your own needs.
See #23 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants