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

Adding context to twig extension #55

Merged
merged 2 commits into from
Mar 6, 2013
Merged

Adding context to twig extension #55

merged 2 commits into from
Mar 6, 2013

Conversation

smurfy
Copy link
Contributor

@smurfy smurfy commented Mar 5, 2013

This pull requests addes the ability to set the context in twig.

i mainly need groups in the extension but only that would be bad for people which want to use versions aswell.

if this solution is accepted i could create a patch for serializerBundels Templating aswell.

@schmittjoh
Copy link
Owner

I guess you pass the context to the template, or how do you create it?

@smurfy
Copy link
Contributor Author

smurfy commented Mar 5, 2013

yea exactly, another way would be better i guess maybe an array option like

{{ object|serializer('json', { 'groups' : ['foo', 'bar']}) }}

@schmittjoh
Copy link
Owner

That would be an option yeah, and should work well for the built-in things.

Another idea, what do you think about something like:

{{ object|serialize('json', serialization_context().groups(['foo', 'bar'])) }}

@smurfy
Copy link
Contributor Author

smurfy commented Mar 5, 2013

I like it, its a bit long, but better than assign the context from the controller.
And it should work even if more options get added to the context, without adding logic to the extension.

@schmittjoh
Copy link
Owner

Great, could you update your PR?

*/
public function serialize($object, $type = 'json')
public function serialize($object, $type = 'json', $context = null)
Copy link
Owner

Choose a reason for hiding this comment

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

Please also add a type hint to the parameter.

@smurfy
Copy link
Contributor Author

smurfy commented Mar 5, 2013

sure, i will do it tomorrow.

{{ data|serialize('json', serialization_context().setGroups(['foo', 'bar'])) }}

{{ data|serialize('json', serialization_context().setVersion('1.0')) }}
@smurfy
Copy link
Contributor Author

smurfy commented Mar 6, 2013

Ok updated my code. had to change groups to setGroups and possible version to setVersion.

Sample

{{ data|serialize('json', serialization_context().setGroups(['foo', 'bar'])) }}

{{ data|serialize('json', serialization_context().setVersion('1.0')) }}

schmittjoh added a commit that referenced this pull request Mar 6, 2013
Adding context to twig extension
@schmittjoh schmittjoh merged commit ef8f4ec into schmittjoh:master Mar 6, 2013
@schmittjoh
Copy link
Owner

Thanks, merged!

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.

2 participants