how to share pariail templates in different modules - yii

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()

Related

Localization of view without using #Localizer

I am using localization for view as given below.
<p>#Localizer["Use this area to provide additional information."]</p
I need to create a global resource file from where I can localize the string of view without using ViewModels folder in resource folder.
Resources/ViewModels/Account/RegisterViewModel.fr.resx
Is there any way to use localization of view without using IViewLocalizer or without using #Localizer in view?
here is how to do it :
on your resx file properties, add "PublicResXFileCodeGenerator" for Custom tool.
It will generate the "designer file".
You need to do it only for main language file.
After that, on your view in cshtml, you can use it like this :
//my resource file is named Welcome.resx
<h1>#Welcome.Title</h1>

How to have methods in common for several (partial) views

in my asp.net core web project i have two partial views that are displaying products. the first one shows them in a big layout, while the second one displays them in a smaller, regular layout.
both partial views need a method that composes a SEO-friendly url (using the product name and further information which are provided from the ViewModel). this method should somehow be accessible to the both views.
where can i place it inside of the asp.net core application? are there any particular conventions that must be followed?
#Html.Partial("ViewName")
Uses a view in current folder with this name. If none is found, searches the Shared folder
#Html.Partial("ViewName.cshtml")
A view with this name must be in the same folder
#Html.Partial("~/Views/Folder/ViewName.cshtml")
#Html.Partial("/Views/Folder/ViewName.cshtml")
Locate the view based on the application root. Paths that start with "/" or "~/" refer to the application root
#Html.Partial("../Account/LoginPartial.cshtml")
Locate the view using relative paths

How to make second default _Layout view

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.

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".

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.