I try to call controller/view in another view. I have a homepage Default:index using a block view of my controller Event and I want to put this block.html.twig in my folder of my controller. In my controller Event, I want an action block, in this way I keep the logic of events, in my controller Event.
How can I do for in Event:index.html.twig call my controller/view ?
I saw the helper render, but I think it makes many requests to include the result.
You seem to be on the right track. When calling sub-renders from a view, you have several options, as detailed here: http://symfony.com/doc/current/book/templating.html
The one I think you're looking for is:
{{ render(controller('YourBundle:Event:index')) }}
which will call the controller action and relevant view.
As an aside, if you want a sub-render, but require no controller logic, use
{{ include('YourBundle:Event:index.html.twig') }}
as this seems to be a lot more lightweight.
If you need to use any of these with parameters, normal format is used.
Related
I my application i have a listview representing News-items. On each row end i want to add a plus-sign button and by clicking it, a little inline form should pop up in a bubble (via bootstrap dropdown). To render this form, my actually approach is to render this via the render(controller(...)) mechanism in the field template.
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
{{ render(controller(...)) }}
{% endblock %}
The render controller call renders a form according to the official symfony documentation ...
The rendered form sends it data back to the commentAction in the same controller.
In theory it works fine, but practical its slow as hell. My site runs in timeout after 60 seconds just because in the listview are default 64 items shown and each item calls this render controller function ...
Is there a clean way to render a little extra form not by invoking a expensive render controller call?
I think it could work with manipulating the listAction method in the controller (injecting here the extra form?) ... but this does not seem a clean solution.
Any ideas?
Thanks
Edit:
Ok, i think i found out, where the bottleneck is ... the example above was simplified. In reality i load a collection into the form for every row ... in the form you can choose, which author created the news. There are at least 500 Authors in that list ... so displaying these authors for a single new, everything is fine. But rendering the choose options 64 times is too much. Is there a way to make this faster? Some caching mechanism?
I think in this case, instead of rendering all forms at once, just load the form via AJAX once user click on the button. Another solution could be to replace collection for some autocomplete somyou dont need to render all data at once.
I have Homepage presenter i want to view what inside it in homepage. how can i open it in webaddress ? i already try to open it
with this http://localhost/sandbox/www/homepage/action it wont open
What the different between method action and render.. forexample i have this two method…
actionOut() and renderOut() in the Homepage Presenter when i route it it nette cannot make any different between two..
This depends on your routing. The default one allows you to access all presenters by pattern <module>.<presenter>/<action>. So the question is if the Homepage presenter is in any module, what's the routing, ... You can take a look at debugbar, where you can find a routing panel that may help you understand which route was matched. Also, you didn't specify, what is the exact error.
I encourage you take a look at the lifecycle of the Presenter. Basically, the action method is run always, even if you redirect in a signal method. The render method is run only when the presenter is going to render the current action. The action method should take care of getting the primary resource and "storing" it to the presenter's property, not the template! The render method takes the resource and pushes it to the template. If the resource method is not available, you should call $this->error() in your action method.
I have this piece of code in several Twig templates:
{% if is_granted('IS_AUTHENTICATED_REMEMBERED') == false %}
{{ render(controller('FOSUserBundle:Security:login')) }}
{% include 'FOSMapyetBundle:Registration:register.html.twig' with {form:form} %}
{% endif %}
As you may see it receive the parameter form from controllers but here comes the problem in every site I like to use the code lets said in templates I need to pass the parameter form to the view. By now it's only on 15 templates but this make me think, what about if tomorrow should be 20 or 60 or even 100 templates? So what is the best solution to handle this? Twig Extensions? Symfony Services? Any advice or help on this one?
A kernel event listener may work for this. Render your mini template inside the listener and then "inject" it into the final response, similar to how the debug toolbar works:
https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php#L106
Note that this is the approach you may want if you want to modify all pages universally. You can apply checks inside the listener to ignore ajax requests, redirection, and the like. You could also probably implement a simple map that knows which routes should have the response modified.
If you want to explicitly render the template in specific pages, then I agree with Maerlyn.
When you need logic like this, your best option is to render a sub-controller, like you already do with your login form.
Create a RegistrationController in your bundle that creates the form and returns the rendered register.html.twig, then embed it like the line above the include.
You can use Response event, and inject your peace on code at the end of the template.
http://api.symfony.com/2.4/Symfony/Component/HttpKernel/KernelEvents.html
I have a menu and I used a cookie in order to set the menu as selected by refreshing. The menu is common to all classes, so I put it in the _layout.cshtml, but the same time I want a controller for this layout because there some function to set the menu as highlight. Can I create a controller for this?
Yes, you can call #Html.Action or #Hmtl.RenderAction to invoke a controller from the view. But this borders on mixing of concerns, as now your view is actively calling your controller.
The better approach is often to pass the appropriate data to the view as part of your view model.
Somewhat related answer of mine
RenderAction method documentation
But the same time I want a controller for this layout because there
some function to set the menu as highlight.
If you are doing something basic like highlighting the current page, there may be a simpler solution. You could put the current page ID into the ViewBag and retrieve that value in your main _Layout file and use it to select the appropriate item.
Partial actions may be what you looking for here - this article may be some help. It relates to MVC2 but the principles remain the same.
create an action for menu, and put menu ui in the view, and then call Html.Action("menu action name","controllern name") on layout page.
I'm having trouble figuring out how to do the following:
On every page (or every page I so desire), I'd like to put a common control widget (e.g. think - Search functionality that contains a textbox+button). What's the best way to do this, and who handles the submit button (assuming it is a submit button)?
i.e. what does my ViewUserControl look like? Does it have a form? does it use jQuery onclick""? Does it post to the main View's action method, or can I redirect it to another Controller/Action?
I have tried using RenderAction of a "Search.ascx" which contains a Form, and is handled by my SearchController... but in the SearchController, it then tries to call RedirectToAction... and I get a complaint about RedirectActions not allowed on Child Actions.
I'm a bit lost on what to do next, so suggestions greatly welcome!
Ray
You seem to be on the right track (using ViewUserControl and RenderPartial). But with the information you have provided, it is not easy to see what you other problems are (RenderAction , ...)
It is easy:
Create a UserControl (.ascx) and get a form in there with URL being /search/..., something that you can get back.
In your views, call RenderPartial and provided the view name
Create your controller to receive the post from your search. This is not the same controller as your parent view controller.
The best way to have the HTML elements show up is to put them in a master page, or in a partial that is referenced by your master page. I would have it be it's own form and submit to your SearchController.
Let me know if you want more particulars.
RenderPartial is the way to go here. Your control.ascx will consist of it's form and submit button.
What's the best way to do this
Probably a partial view. An .ascx file.
and who handles the submit button
(assuming it is a submit button)?
The partial view
what does my ViewUserControl look
like? Does it have a form?
Yes, it has a form. It should be as self contained as possible.
does it use jQuery onclick""? Does it
post to the main View's action method,
or can I redirect it to another
Controller/Action?
Well, whatever fits your exact scenario best. It should probably post to whichever action is most appropriate. That is not likely to be the main view's action, since the partial is reused in different parent views.
I have tried using RenderAction of a
"Search.ascx" which contains a Form,
and is handled by my
SearchController... but in the
SearchController, it then tries to
call RedirectToAction... and I get a
complaint about RedirectActions not
allowed on Child Actions.
You'll probably want to render it using RenderPartial in the parent view:
<%: Html.RenderPartial("MyPartialView.ascx") %>
Ok, I figured out what my problem was. The replies above are correct. I have my own Search.ascx user control and SearchController, I also used RenderPartial, but what stumped me was that I forgot to explicitly specify the controller/action... so then I was fiddling around with onclick events and Url.Action on my button instead.
<% using (Html.BeginForm("MySearchAction", "MySearchController")) { %>
Thanks to all who replied.