Reputation: 672
On one of my pages, I'm trying to go grab the contents of another page from a different controller, and then take that HTML and use it to make a little "widget" on the first page.
If I was grabbing off the current page, I could just do:
my $html = $c->response->body();
But since it is in another controller, I can't figure out how to get it.
An additional (potential) complication is that the action in question needs an arg passed to it.
I tried my $html = $c->visit('/action/')->body();
- a shot in the dark - which I troubleshooted in another recent post. But that just seems to take over the current action, rather than allowing me to just grab the response from it.
The only thing I could think of (and this is untested) is storing the response of page 1, doing my $c->visit
, storing that response, then taking page 1's original response and outputting that right at the end. Seems overly convoluted, but that's the best I can think of.
Any ideas?
Upvotes: 0
Views: 149
Reputation: 1513
Catalyst::Plugin::SubRequest might do what you're looking for. As RET says, it may be more prudent to refactor your app a bit, but there are some occasions when rendering a sub-request can be useful
Upvotes: 1
Reputation: 9188
I answered your other question before seeing this one. The latter part of my answer is germane here, I suspect.
It sounds to me like the logic that is in /assets/widget needs to be located in the Model rather than the Controller, so that it can be used by whatever function requires it.
And/or you need to break your templates down into (reusable) components, so that whatever content you planned to embed could be done as part of a single rendering process.
[%- IF foo;
PROCESS widget.tt;
END; -%]
Or you model your widgets differently, so that instead of making them part of your main page rendering, you generate them using an AJAX approach, so that each widget makes its own call to your application, gets back some JSON, and renders itself (probably via jQuery - an example of that is way beyond the scope of the question).
Hope that helps.
Upvotes: 2