Skip to content

Deprecate the global variables in Twig in favor of Twig functions #13356

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

Closed
wants to merge 1 commit into from

Conversation

fabpot
Copy link
Member

@fabpot fabpot commented Jan 10, 2015

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? yes
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR n/a

I propose to deprecate the app global variable and replace it with Twig functions:

  • app.request -> request()
  • app.session -> session()
  • app.user -> user()
  • app.environment -> env()
  • app.debug -> is_debug()

@Tobion
Copy link
Contributor

Tobion commented Jan 10, 2015

Is it possible to chain function calls, i.e. session().get('foo').? I don't think so. So if not, it would require to assign it to a variable first which makes things rather verbose compared to the global variable.

@fabpot
Copy link
Member Author

fabpot commented Jan 10, 2015

@Tobion, yes, you can chain them.

@linaori
Copy link
Contributor

linaori commented Jan 10, 2015

In case of session().get('foo'), I'd rather see something simpler such as session('foo'). Same goes for request('foo'). Both would be equivalent to $request->get('foo') and $session->get('foo').

@Tobion
Copy link
Contributor

Tobion commented Jan 10, 2015

@iltar symfony 2 has no magic anymore and this doesn't work generically. Why would it be a shortcut for get and not some other method...

@linaori
Copy link
Contributor

linaori commented Jan 10, 2015

@Tobion It's not magic and why shouldn't it work?

public function getRequest($name = null)
{
    $request = $this->request_stack->getCurrentRequest();
    return $name ? $request->get($name) : $request;
}

Edit: Why get and not something else? Because why would you set something in the request in your template? That's a bad practice IMO. If you want something else:
{{ request().attributes.get('foo') }}

@fabpot
Copy link
Member Author

fabpot commented Jan 10, 2015

@iltar, because you do many things with a Request besides getting a value from the query/request/cookie/...

@linaori
Copy link
Contributor

linaori commented Jan 10, 2015

@fabpot in the controller I would agree. In the template I think you don't want to have that all available.

@wouterj
Copy link
Member

wouterj commented Jan 10, 2015

Doing a grep app.request in a fresh installation of the CMF sandbox, I see lots of things except app.request.get. I agree with @fabpot and @Tobion here.

However, I tend to agree a bit with what @hhamon said in the previous PR:

Having functions to access global objects sounds strange to me as Twig functions are meant to generate/create values if I'm not mistaking. These functions always return the same values everytime they're called.

@fabpot
Copy link
Member Author

fabpot commented Jan 14, 2015

ping @symfony/deciders Good idea or not?

@stof
Copy link
Member

stof commented Jan 14, 2015

@iltar IMO, it is important to be able to get a param from the query string or from the attributes only. Allowing to get it from the query string when you expect it to be in the attributes can even be a security issue in your app depending of what you do with it, because the user can put what he wants in the query string. So calling $request->get() directly is a -1 from me.
For the session access, there is the same issue: your proposal forbids accessing flash messages (or requires adding another function for that)

* {@inheritdoc}
* Returns the current user.
*
* @return mixed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

object|null actually

@linaori
Copy link
Contributor

linaori commented Jan 14, 2015

@stof Session wise it depends on whether you do or do not use the SessionInterface as a base for this function..

@Tobion
Copy link
Contributor

Tobion commented Jan 14, 2015

I also agree with the objection

Having functions to access global objects sounds strange to me as Twig functions are meant to generate/create values if I'm not mistaking.

@fabpot what was your main argument/idea for the change?

@fabpot
Copy link
Member Author

fabpot commented Jan 14, 2015

@Tobion I'm not convinced myself that this is a good idea. Closing now.

@fabpot fabpot closed this Jan 14, 2015
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

Successfully merging this pull request may close these issues.

5 participants