submit form from one model to another view in Yii - yii

How do I post from one controller into another view?
I have a Review model and a Product model. The Review form is displayed in the Product view through a widget, but how do I submit the form itself? Right now, it doesn't do anything. I can submit through review/create, but not through the Product View.
Or am i suppose to do the post in the widget?

You can achieve it if you put code like below on components/ReviewWidget.php . I supposed you have Review as model and its respective controller and views file on default locations.
<?php
class ReviewWidget extends CWidget{
public function init() {
return parent::init();
}
public function run(){
$model = new Review;
if (isset($_POST['Review'])) {
$model->attributes = $_POST['Review'];
$model->save();
}
$this->renderFile(Yii::getPathOfAlias('application.views.review'). '/_form.php',array(
'model' => $model,
));
}
}
Then, call above widget on any where on view like below ,
<?php $this->widget('ReviewWidget'); ?>
It will handle item creation only. You have to create code to item update by yourself.

In your controller action you must use function renderPartial
$this->renderPartial('//views/reviw/_form',array('data' => $data ) );
First argument of this function is used to determine which view to use:
absolute view within a module: the view name starts with a single slash '/'. In this case, the view will be searched for under the
currently active module's view path. If there is no active module,
the view will be searched for under the application's view path.
absolute view within the application: the view name starts with double slashes '//'. In this case, the view will be searched for
under the application's view path. This syntax has been available
since version 1.1.3.
aliased view: the view name contains dots and refers to a path alias. The view file is determined by calling
YiiBase::getPathOfAlias(). Note that aliased views cannot be themed
because they can refer to a view file located at arbitrary places.
relative view: otherwise. Relative views will be searched for under the currently active controller's view path.
Also you can use this function in your views. But the most convenient way to reuse views is to create widgets.

Related

How Can I Render A Partial View Inside an Editor Template?

I am using an edit template for Kendo scheduler called ScheduleEditorTemplate.cshtml which his located in Views/Shared/EditorTemplates
I have a partial view named _POC.cshtml and I need to display that partial view inside the ScheduleEditorTemplate.cshtml. This is a read-only partial view, the user will only view what is there.
When I use the RenderPartial method as follows I get an "invalid template" exception when I try to open the ScheduleEditorTemplate.cshtml from the scheduler. It works without the partial view included.
How can I render a partial view with a different view model into an editor template?
Here's my code to render the partial view within the ScheduleEditorTemplate.cshtml
#{Html.RenderPartial("_POC");}
I am already using this partial view in another view so I know it loads and works correctly there.
I tried this also with the same exception generated:
#Html.Partial("~/Views/EmployeeSchedule/_POC.cshtml")
I can share the code from _POC.cshtml but there's nothing special there; just kendo controls.
#{Html.RenderPartial("_POC");}
And use Ajax to load this partial view:
<script type="text/javascript">
$(document).ready(function () {
$("#div").load('#(Url.Action("POC","ControllerName"))', function(){});
});
</script>

Controllers sharing parts: How to include a controller's output from within another controller's view in Prestashop >= 1.5?

In the shop's customer section I would like to render the user account menu (which we can see by default when reaching /my-account URL) as a side column on some others controllers like the ones associated to "/my-adresses", "/identity" pages ..
I thought I would have to create another controller which purpose would be to gather menu infos and only render the menu <ul> list. Then I could override Controllers such as MyAccountController, IdentityController to include this former Ctrl and then render its content as part of the views of those two other controllers views.
So how one can load a specific controller from another in order to render shared views between pages ? Which is the right/clean way to do that ?
I heard about $this->getController() but I did not find any snippet or implementation of what I'd like to achieve. I new to Prestashop but even if the code seems clear, I don't get the point here.
Thank you !
After getting a bit deeper into sources, I ended up moving the menu (initially part of the MyAccountControllerCore alias my-account template) into a brand new Controller "MyAccountMenuController", in /override/controllers/front/.
<?php
// In /override/controllers/front/MyAccountMenuController.php
// The "exposer" controller
public function display()
{
// Do what ever you want to pass specific variables
if (! $this->template) {
throw new Exception(get_class($this) . '::display() : missing template.');
}
$this->context->smarty->display($this->template);
return true;
}
I am now able to import this menu by adding the following snippet inside the initContent() method of each controller that are part of the customer account section (this means that each of them must be overridden, for more info about overriding controllers, see documentation):
<?php
// In /override/controllers/front/MyAccountController.php
// The "consumer" controller
public function initContent() {
// ...
// Importing the customer area's menu
$menuController = $this->getController('MyAccountMenuController');
ob_start();
$menuController->run();
$this->context->smarty->assign('myAccountMenu', ob_get_clean());
}
I don't think the original purpose of getController method (located in ControllerCore class) was meant to include another controller output, at least in Prestashop 1.5. Still, to me this approach is far cleaner than duplicate views code.
If you have a better (cleaner) approach to implement such a mechanism, please let me know !
Any thoughts ?

FluentBootstrap - Acces to the form object from partial views

I have a view that renders a form with code similar to the following:
#using( var form = Bootstrap.Form().SetHorizontal( 3 ).AddCss( Css.ColSm8, Css.ColMdOffset2 ).Begin() )
{
#form.DisplayFor( m => m.Name )
// bla bla bla
#Html.Action( "Details", "Fare", new { entity = Model.FareId } )
}
How can I access the form object in the partial view, so that the same layout is applied to the whole form?
A lot of work was spent ensuring that the Bootstrap control stack would carry-over into partial views. You have two options here:
The first is to just pass the form object to the partial/action as part of the model. In your case, you would just add it as another property in the anonymous model object you're sending to the action.
You don't need to use the form instance to make FluentBootstrap recognize you're in a form. It's just a convenience to make calling the extensions appropriate to a form easier. You can also just call something like Bootstrap.DisplayFor(x => x.Name) right from the global Bootstrap object in your partial and it will respect any settings you've placed in the containing form defined in the containing view.

Render strongly type partial view in layout MVC4 razor

I have some issues with rendering strongly type partial view in layout view.
partial view (menu) is band with model Menu
this partial view I want to render in _layout.chtml, so it 'll be avliable in all views.
I want to make avalible the partial view(menu) in all pages/view. the problem I face is where to put the action for partial view to populate it from DB on page load.
thanks
---------------------- My code is--------------------
partial view inside shared folder.
#model List<Menu>
#foreach(var item in Model){// here is the html/model item inside to display}
--------------------------------------
HomeView.chtml inside home folder
#model List<homemodel>
.... here goes html code/ plus homemodel loop/data etc.
------------------------------
HomeController{
public ActionResult HomeView()
{
.........return view();
}
public PartialViewResult partialmenu()
{
// data from db
return partialview(partialobject as list);
}
------------------------
layoutview.chtml
--html code---
{# Html.renderpartial("partialview");}
.. html code...
I want to make available the partial view(menu) in all pages/view. the problem I face is where to put the action for partial view to populate it from DB on page load.
Write in layout: Html.RenderAction('MyMenu') OR Html.Action('MyMenu') and then populate it from any source. Your action will return strongly typed model.
where to put the action for partial view to populate it from DB on
page load.
every view has it's own controller..regardless if it is partial or not...therefore you could populate your view on it's own controller...

OpenCart Breadcrumb in header.tpl

Can someone tell me how can I have the breadcrumb nav within the header.tpl and not in the product.tpl of opencart?
I've just had to figure this out for a new site we're building and I've come up with the following; use at your own risk (I'll report back if I run into any major problems, but I can't foresee any... famous last words)
Basically the breadcrumbs are built in the controllers and we need the resulting $breadcrumbs array in the header controller. Modify system/engine/controller.php as follows:
[...snip...]
protected function render() {
foreach ($this->children as $child) {
$this->data[basename($child)] = $this->getChild($child,array('parent_data'=>&$this->data));
}
[...snip...]
This will send all the data in the parent controller, before render() was called, to every controller/method of the $children. Then we just need to pick this up in the header controller as follows:
<?php
class ControllerCommonHeader extends Controller {
protected function index($args=array()) {
// parent data
$this->data['parent_data'] = $args['parent_data'];
[...snip...]
And we can access everything in the template with $parent_data['whatever']. In this case, $parent_data['breadcrumbs'] will be the array of breadcrumbs that I can loop over with the code I've removed from each page.tpl and added to my header.tpl.
Due to the way 1.5.X is coded, you'll need to rewrite every controller and add a method back to the document class to allow passing from the product controller to the header controller. Is there any particular reason you want to do so?
if all else fails just hack the css, something like
.breadcrumb {
margin-left: -270px;
margin-top: -65px;
}
will move the breadcrumb up and to the left.