Yii2 Render view from extension - model-view-controller

I have Yii2 application which has a regular controller with regular action and its view trying to render a view that's part of an extension. My view is in the 'views/controllerName' folder and I'm trying to reach a view which is in 'vendor/providerName/extensionName/views/extensionController'. What is the right way to do that?
I'm tried the regular render() method with different strings like: extensionController/extensionView, /extensionController/extensionView, //extensionController/extensionView but I keep getting an error message that the file is not found in the main view folder which is not where I want the framework to look at first place.

e.g.
echo $this->render('#vendor/firephp/test');
so in your case
echo $this->render('#vendor/providerName/extensionName/views/extensionController');

Related

What is renderAjax(), how is it different from render()?

When I use normal return this->render('create', ['model' => $model]) my pop-up window goes all haywire. When I change to return $this->renderAjax('create', ['model' => $model]); everything is magically in their correct places. I have looked around quite a bit to read about renderAjax() but there seem to be absolutely nothing out there. Can someone tell me what it does? I know ajax but from what I know it usually has nothing to do with css or bootstrap.
To know the difference between render() and renderAjax() first you need to understand how render() works.
Basically when render() is called every piece of JS and CSS code and file references registered with the view is gathered in several arrays to be rendered later on in proper places - these places are stored in the layout and their code is rendered by calling beginPage(), head(), beginBody(), endBody() and endPage().
You can point where the JS code should be rendered by setting the second parameter in related methods - like for example:
$this->registerJs("alert()", \yii\web\View::POS_HEAD);
renders
<script type="text/javascript">alert()</script>
in layout where the method $this->head() is.
Everything is working fine until you want to render only a main part of view without the layout. Without it (and its methods like beginPage()) the JS and CSS references cannot be rendered in the previous way and that is why this fancy jQuery code rotating the square does not work - JS library has not been included there.
When you are calling $this->render() from within the view or just calling $this->renderPartial() from the controller exactly this is happening - layout is not applied.
renderAjax() comes now to the rescue.
This method doesn't care about layout because it calls beginPage(), head(), beginBody(), endBody() and endPage() methods by itself. Thanks to this every JS piece of code can be attached to the rendered view and the jQuery library can rotate this square once again even when it needs to be done inside an AJAX popup.
render() public method
Renders a view.
The view to be rendered can be specified in one of the following formats:
path alias (e.g. "#app/views/site/index");
absolute path within application (e.g. "//site/index"): the view name starts with double slashes. The actual view file will be looked for under the view path of the application.
absolute path within current module (e.g. "/site/index"): the view name starts with a single slash. The actual view file will be looked for under the view path of the current module.
relative view (e.g. "index"): the view name does not start with # or /. The corresponding view file will be looked for under the view path of the view $context. If $context is not given, it will be looked for under the directory containing the view currently being rendered (i.e., this happens when rendering a view within another view).
renderAjax() public method
Renders a view in response to an AJAX request.
This method is similar to render() except that it will surround the view being rendered with the calls of beginPage(), head(), beginBody(), endBody() and endPage(). By doing so, the method is able to inject into the rendering result with JS/CSS scripts and files that are registered with the view.
renderAjax()
render()

Call a regular view in Html.Partial?

I have a regular view I'd like to use in another page, appearing magically in a jQuery-like accordion if the expand button is clicked. If I call it using:
Html.Partial(A_non_partial_view, new view_model_used_by_the_non_partial_view())
...does that have a chance of working, or is MVC not plumbed that way? (I'm using MVC 3 if that helps.)
You can, but only if it is in the same controllers views folder or in the shared views folder. otherwise you will have to specify that path fully which isn't practically really.
If you use Html.Action or Html.RenderAction, then that action will need to return a PartialView otherwise it will push out a full html page again with head tags etc etc

Rendering a partial in the parent directory in Ramaze

How do I render a partial that's in a different directory (in my case, the parent) than the current view?
<%=render_partial :sidebar%> #looks in the current dir and works as expected
<%=render_partial "/view/sidebar"%> #doesn't work!
Thanks!
You have to specify the right controller that is responsible for the right view:
TheRightController.render_partial :sidebar
If you don't specify the controller class, render_* works for the current action (controller) only, except render_full that does real internal HTTP request.
So, the answer is: If you need shared templates, just create special controller, i.e. called Shared, without any action methods inside, just with many templates in an appropriate view folder and call Shared.render_partial.
Shared.render_partial works like internal request. It renders contents of the controller's action and even the action's method is executed. If you want to render just the view (without executing Shared's action method), use Shared.render_view instead.
Moreover, you can use the internal requesting to prepare some data in the Shared controller's method. For instance, if your sidebar consists of #articles, let's load them in the Shared's sidebar() method. You don't need to load #articles in any other controller that displays the sidebar! You only call "Shared.render_partial :sidebar" in there. This is how to build widget-like web with Ramaze :-)
I found following api in apidock.com, maybe useful for u
# Renders a collection of partials located in a view subfolder
# outside of our current controller. In this example we will be
# rendering app/views/shared/_note.r(html|xml) Inside the partial
# each element of #new_notes is available as the local var "note".
render :partial => "shared/note", :collection => #new_notes
#rebnoob may use (without view directory name, because Rails search on app/view directory)
<%= render "/sidebar" %>
instead of
<%=render_partial "/view/sidebar"%> #doesn't work!

Compile-time error: rendering a partial view within a partial view

I am trying to render a partial page inside a partial page.
So i have in my layout page a call to my partial CreateMenu and here i pass the model from the layout page. This works perfect.
Now inside the CreateMenu partial i am trying to call MenuItem with the same syntax but then it fails. Visual studio shows the path as red (i know to 100% that it exists).
How can i render a partial from inside a partial.
MenuPartial's call to the render:
#Html.Partial("~/Models/Default/UserControls/_MenuItem.cshtml", Model.Modules[i])
Model.Modules[i] consists of MvcModule objects.
MenuItem:
#model Models.Default.Classes.MvcModule
<li class="#{if (Model.CanExpand) {<text>fullwidth</text>} else {<text>nodrop</text>}} first_fullwidth">
...
This results in a compilation error:
Compiler Error Message: CS0115:
"ASP._Page_Models_Default_UserControls__MenuItem_cshtml.Execute()":
Es wurde keine passende Methode zum
Überschreiben gefunden.
Line 46: public override void Execute() {
Sorry for the German text. I have tried to get it to output English instead but VS 2010 refuses to change the settings =/
I don't think this is a nested partial issue. You should be able to nest partials without any problem. It looks like the partial you're trying to render is in the ~/Models/Default/UserControls directory. This isn't a place the default view engine looks for views. Try copying the web.config file from your Views directory into the Models directory.
If it were me, I would try to avoid storing views outside of the Views directory if at all possible to avoid weird issues like this.

CodeIgniter jQueryUI dialog form example

I am trying to use CodeIgniter and jQuery-ui dialog to create a modal window with form to update user information.
The process should be like:
1. Press a button on a view page.
2. A modal window pops up.
3. Inside the window is a form that a user can fill.
4. If the user filled something before, the information should be shown in corresponding field
5. Click the update button on the modal window to save the changes to database.
Can anyone provide a good sample of this process?
I used ajax to pass the data but it didn't work when I was trying to update the data to the database. It would be nice if an example of how to pass data from ajax to php and how php handle that.
Thanks,
Milo
well the jquery bit for post(), get(), ajax() works the same in any measure you would normally use it.. key difference here is with CI you can't post directly to a file-name file-location due to how it handles the URI requests. That said your post URL would be the similar to how you would access a view file normally otherwise
ie: /viewName/functionName (how you've done it with controllers to view all along. post, get, ajax doesnt have to end in a extension. I wish I had a better example then this but I can't seem to find one at the moment..
url = '/home/specialFunction';
jQuery.get(url, function(data) {
jQuery("#div2display").html(data);
});
in the case of the above you notice despite it not being a great example that. you have the url with 2 parameters home and specialFunction
home in this case is the controller file for home in the control folder for the home file in views the specialFunction is a "public function" within the class that makes the home controller file. similar to that of index() but a separate function all together. Best way I have found to handle it is through .post() and a callback output expected in JSON cause you can form an array of data on the php side json_encode it and echo out that json_encode and then work with that like you would any JSON output. or if your just expecting a sinlge output and not multiples echoing it out is fine but enough of the end run output thats for you to decide with what your comfortable doing currently. Hopefully all around though this gives you some clairity and hopefully it works out for you.

Resources