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

TemplateView #6443

Merged
merged 9 commits into from
Apr 18, 2017
Merged

TemplateView #6443

merged 9 commits into from
Apr 18, 2017

Conversation

CarsonF
Copy link
Member

@CarsonF CarsonF commented Mar 3, 2017

In regards to #6382

Working on how this can be done without breaking BC, but it's proving challenging.

The only response method called in Frontend was the X-XSS-Protection header in record route. So I moved it out.

Now you can do something like:

class MyFrontend extends Frontend
{
    protected function render($template, array $context = [], array $globals = [])
    {
        return new TemplateView($template, array_replace($context, $globals));
    }
}

and then replace the controller.frontend service with that class.

Open to ideas about how to integrate this further. I don't think changing Controller\Base::render directly is wise though, since others can base their controllers on it. But maybe if it's something just for Frontend / Backend classes that would be ok. I'm not sure if a separate shortcut even makes much since given how terse the line actually is.

@CarsonF
Copy link
Member Author

CarsonF commented Mar 3, 2017

TemplateView allows template & context to be modified before it is rendered.
TemplateResponse allows read-only access to template & context but also allows modification to the response headers or body (string).

Let me give some examples of how this can be used.

Say you want to modify a record:

$app->view(function (TemplateView $view, Request $request) {
    if ($view->context->has('record')) {
        $record = $view->context->get('record');

        // modify record

        $view->context->set('record', $record);
    }
}, 10 /* above default */);

Say you want to add a header for a certain record:

$app->after(function (Request $request, Response $response) {
    if ($response instanceof TemplateResponse) {
        $context = $response->getContext();
        $record = isset($context['record']) ? $context['record'] : null;

        if (/* if record is foo */) {
            $response->headers->set('Foo', 'Bar');
        }
    }
});

Either of these could be used to retrieve the record and do something with it externally.

@SvanteRichter
Copy link
Contributor

👍 This looks great!

@GwendolenLynch
Copy link
Contributor

FWIW the conceptual approach, as it stands, has my 👍

@GwendolenLynch
Copy link
Contributor

Welcome to Monday, @CarsonF … Your friendly, weekly, "already-asking-too-much-of-a-person" nudge on this 🤗

@GwendolenLynch GwendolenLynch added this to the Bolt 3.3 - Feature release milestone Mar 13, 2017
@bobdenotter
Copy link
Member

No movement on this for 5 weeks. Please don't forget, @CarsonF. :-)

@CarsonF
Copy link
Member Author

CarsonF commented Apr 11, 2017

There is, it's just local. Haven't forgotten.

…y/template_view" config option is true.

Added deprecation notice if still using TemplateResponse.
… they just can't be accessed from the response.
…. If false globals won't be assigned as twig globals.

If globals are not needed this can be set to false. More code will come later to remove dependency on current twig globals (record and pager).
@CarsonF
Copy link
Member Author

CarsonF commented Apr 18, 2017

This is ready for review now.

@GwendolenLynch GwendolenLynch changed the title [WIP] TemplateView TemplateView Apr 18, 2017
Copy link
Member

@bobdenotter bobdenotter left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@bobdenotter
Copy link
Member

As discussed during the dev-meeting: :shipit:

@bobdenotter bobdenotter merged commit a5e93b8 into bolt:release/3.3 Apr 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants