Difficulty in deciding where to put the code in yii view helper - yii

I'm new to yii development.
In my system I'm trying to create a submenu system. The submenu will be shown based on the
controller. The submenu will be separate views, that I'll load in main layout.
I want to separate the logic of loading the submenu view from the main layout. But, I'm not sure where to write it.
Does Yii has view helpers like in RoR. Or, should I write it as a component?
Please give suggestions.
Thanks.

I'd just create a component for this, and instantiate that with the relevant menu options from the controllers. If present in all controllers, implement support in a BaseController and just set up an array of items in the child controllers.

Your default generated Yii application has a parent Controller in protected/components/Controller.php. If you need to access additional parameters in layout, add public properties to Controller, set them in your child controller, and use them in your view/layout files.
In your case, add a function to this parent Controller that returns a rendered submenu (with a renderPartial call for example) and call this function from your layout.

Related

How can i make all the view pages in a controller to refer to a single layout page

I want to know if it's possible to let all the view pages in a controller access a single layout page and in which i have multiple controllers with their many view pages such that i need a single specific layout setting corresponding to the specific controller.further if that's not enough to explain i'm adding that we know that there's a viewstart file in the views folder which contains the reference of the layout to be used in all of that controller's views,i want here that a seperate viewstart file be contained in separate controller which point to the layout setting as required if possible then please show me that in answer to the query how to do that!
From what I understand you would like a layout for all views in a specific controller rather than the whole project (i.e. using the _viewStart in the root of the Views folder).
There are a couple of options:
1.
Reference the layout in each view. e.g.
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
2.
Create a _viewStart.cshtml in the specific view folder that is associated to the controller.
E.g. if the controller was called BaconController there would be a folder under Views called Bacon. A _viewStart file in a folder will always override the default one in the root of Views.

What is the best practice of propagating variable value in Yii

I have a menu which pass parameters when a user click on one of the items menu.
When a user click an item menu, a view is lauched and call another view which call another view...
Can you tell me what is best practice on passing parameter from view to view.
Do I have to pass from view 1 to view2 and so on ..(as I did but I dont find it very clean) or it is another better way to do this.
I must say that I cannot put the value at the tpp of my controller because at that moment I do not know which menu wil be clicked.
Thanks for your reponse
I assume you are talking about views inside other views http://www.larryullman.com/2011/02/15/rendering-view-files-in-yii/ otherwise the question does not make a lot of sense.
The view has access to all the controller variables. So you can define a variable for the controller and you will be able to access it all the time in any of the nested views with $this->variable. You can actually put it at the top of the controller because that never changes while you are creating the views. That is 1 request, it should not change while you are processing the views.
If you are trying to make some ninja html, creating html with ajax based on what multiple menus the user clicks you can always save something in the session/cache and use that part but cleaner is to pass the argument through the entire process.

Two ViewModels for a single View at MvvmCross

Is it possible to use two ViewModels for a single View at MvvmCross?
The reason is an existing Core library which already has navigation in place and a working app on top of it. Now I need to create another app and leverage the only View for two ViewModels (in core lib there is one view navigated to another using ShowViewModel and I just want to stay at the same view and change my datacontext).
Thank you for any suggestions.
Yes
you can continue to use ShowViewModel and change the way navigation happens by overriding the presenter - see Custom Presenters in the wiki
or you can use a different navigation mechanism entirely - there's nothing forcing you to use ShowViewModel

render the same MVC4 partial view from Jquery but use different controller methods

I have 3 html anchor tags.
depending on what link is clicked, i want to render the same view, but pointing to a different controller action.
How would i render the partial view and point the view to a different action on the same controller.
Use RenderAction().
#Html.RenderAction("Details", "User", new {id = Model.Id})
Some help about how to render partial views.
This post on StackOverflow explains the difference between Html.Action and Html.RenderAction, that may interest you.
UPDATE:
For doing this in JavaScript I redirect you to this question: How to call Html.RenderAction() with parameters declarated in jscript?

Ideal way to design a Rails dashboard with flexible widgets

I've been struggling with the right way to design a Rails model/controller for a dashboard/portal that can support easy/clean widget development. I've already (as a diversion) wrote a jQuery/jQuery-UI/CSS basic view that has worked out the column/order HTML and controls to reorder/minimize/remove the widgets, and now I'm coming back to how best to design the model and controller.
As a central place to find all widget information, I have a Widget model. (as a non-ActiveRecord for now) Ideally I'd like to have the Widget Index view simply take the Widgets from the Controller, and the model would contain widget attributes needed to render the view. This would include widget "partial" and "controller" attributes that the view could use to render a specific widget's partial view in my Dashboard view.
I don't want to have to overload my Widget controller with a bunch of potential widget specific data that their partial view might use. Instead I want to have the partial render itself, by just specifying the widget's partial and controller to use. Then the partial view and controller can do whatever they need to for that widget.
Widget Controller Index --> render #widgets --> widget1 widdget1_controller/partial view1
\ \-> widget2 widdget2_controller/partial view2
\--> widget3 widdget3_controller/partial view3
The Dashboard model would contain column, order, partial view, controller.
The Widget specific partial Views and Controllers would would located under a sub-folder of the controllers and views/widget folders to keep things clean.
I'm hoping this will help when trying to add AJAX support. Each widget controller can handle their own Controller Actions (like editing the widget or saving something) and I can have the client browser make AJAX calls to the partial for updated information without having to call the original Widget Controller, etc...
What do you guys think? Do you have a better suggestion?
This might be to late now, but there is an extension on cells called Apotomo, which basically extends cells with actions and ajax to make little widgets. It's pretty awesome, and they have tutorials on doing the whole widget thing you're talking about. If this is to late, I would guess it would be pretty easy to port your cells code over to the widgets as they're very similar.
I think Cells is what you looking for.