How to make second default _Layout view - asp.net-mvc-4

I'm working on ASP.NET MVC 4 application which has user part and admin panel. For the user part I use the default _Layout view but in my Views folder I have subfolder Admin where I plan to set all my views for the admin panel and I want a different layout for them, let's say - _AdminLayout. For now I use:
#{
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
on top of each view in the Admin folder, but I wonder if there's a way to set _AdminLayout as default layout only for those views that are inside this folder and thus get rid of the explicit declaration for each view?

~/Views/Admin/_ViewStart.cshtml
#{
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
You can place a _ViewStart in any of the controller view folders to override the parent one. Convention makes it look to the most local path first, then proceed to Shared.
As an aside, you may want to look in to using areas to keep it compartmentalized, but that's really up to you and how you want to structure your project.

Related

Intellij - Hide full path in Scopes view

I've started to use the Scopes view in Intelij, since the favorites got removed with the latest update (2021.3).
Somehow in the scopes view there is always the full folder path visible. In my example: below the folder test.foo.bar.baz there are the two folders test.foo.bar.baz.obj1 and test.foo.bar.baz.obj2. I would prefer to have below the folder test.foo.bar.baz only the two folder obj1 and obj2 as in the project view.
Scope View:
Project View:
Is it possible to make the Scopes view look like the Project view?
Thanks for your help!
Show Options Menu (it's the cog button at the top right of the Scope View) and disable Tree Appearance | Flatten Packages

Why _Layout.cshtml placed in ~/Views/Shared folder by default?

Why _Layout.cshtml placed in ~/Views/Shared folder by default?
I ask this question because I put this file in ~/Views folder and change the Layout value in the _ViewStart.cshtml
#{
Layout = "~/views/_Layout.cshtml";
}
and it still work.
Is it for a special purpose that _Layout.cshtml placed in ~/Views/Shared folder by default?
When the layout page is referenced by its full path, you are right that it makes no difference where it is, as long as it's somewhere under the ~/Views folder.
One situation where it would matter (but it often doesn't), is when in your controller action you call the overload of "View()" that allows you to specify an alternate layout (master) page. If that's the case, and the name specified is a "simple" name, such as "MobileLayout", the default search path will be first the current controller's view folder, and then the Shared views folder.
Another much simpler reason is that it "makes sense" in terms of conventions, regardless of any technical requirements.
By convention, the /views/shared folder is for views that are, well, shared by different controllers, such as layouts and reusable partials. As you have seen, there's no reason you can't put layouts whereever you want, as long as you reference them properly.
If you put the _Layout.cshtml in Views folder then you have to specify its file extension explicitly in Layout="_Layout.cshtml".
If you put the _Layout.cshtml in Views/Shared or Views/XXX folder then you must NOT specify its file extension explicitly in Layout="_Layout".

MVC4 don't use master layout on redirect

I have a controller that returns the view using MVC4 however for this specific view I don't want it to use the master layout I have in place. Is this possible to achieve?
put
#{
Layout = null;
}
on the top of your view file. You can also provide path to any other layout file.
you can also change your return statement in given action to
return PartialView();
so it will not attempt to wrap result of your action with layout file

separate header, content and footer in yii

I have a login form in my header section of the website. If user is logged in than insted of the login form user profile details will be shown. The question is how to separate header footer and content into different views and call them from one controller? Or maybe there is another solution...Thanks for help.
In your header view you could write something like this.
<?php if(Yii::app()->user->getId()): ?>
<?php $this->renderPartial('//world/_header_user')); ?>
<?php else: ?>
<?php $this->renderPartial('//world/_header_guest')); ?>
<?php endif; ?>
Using the Model-View-Controller (MVC) design pattern, the look of a Yii-based site is naturally controlled by the View files. These files are a combination of HTML and PHP that help to create the desired output. Specific pages in a site will use specific View files. In fact, the View files are designed to be broken down quite atomically, such that, for example, the form used to both create and edit an employee record is its own file, and that file can be included by both create.php and update.php. As with most things in OOP, implementing atomic, decoupled functionality goes a long way towards improving reusability. But the individual View files are only part of the equation for rendering a Web page. Individual view files get rendered within a layout file. And although I’ve mentioned layouts a time or two in my writings on Yii, it’s a subject that deserves its own post.
To be clear, layouts are a type of View file. Specifically, whereas other View files get placed within a directory for the corresponding Controller (i.e., the SiteController pulls from views/site), layout files go within views/layouts. But while the other View files are associated with individual Controllers (and therefore, individual pages), layouts are communal, shared by all the pages. Simply put, a layout file is the parent wrapper for the entire site’s templating system. I’ll explain
ypu can see more details
http://www.larryullman.com/2012/05/16/working-with-layouts-in-yii
The easiest way is probably to use a different layout, which you just switch on login. If not, showing partials / components based on Yii::app()->user->isGuest also works well.
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.

how to share pariail templates in different modules

for example, I hava a partial template named _header.php, I want to use it in both Post, Forum module, How can i write renderPartial(), to load this template.
placed your partial view to app layouts dir.
<?php
$this->renderPartial('//_header.php');
?>
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.
reference getViewFile()