Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to organise Partial Views in a file structure?

I will potentially have many Partial Views for my application which can be grouped in a folder structure. It seems I ought to do this otherwise I will a View Folder with loads of files. So I assume I should have something like:

Views -> 
  Group1 -> 
    PartialView1
    PartialView2

What would the HTML.Partial call look like?

 HTML.Partial("~/Views/Group1/MyPartialView.cshtml",Model)

Another idea I had was to have the one Partial View file with conditionals Code blocks, but I suspect this goes against everything that PartialViews are about.

Finally is there any difference in performance if one has many small Partial Views versus one large Partial View with multiple conditional components? I guess I am thinking that one file load into memory and compilation to code as opposed to multiple small file loads.

Thanks.

EDIT: More Info.

I have a generic controller that I am using to render different parts of a report, so all sections for an "introduction" chapter would be rendered using "Introduction" partials ie "Introduction.Section1", "Introduction.Section2". In my scenario I do not believe I have common sections across chapters, so I could go with the "file." idea, but the Views folder would be large, hence why I am considering the use of subfolders.

EDIT: Thanks all. Some tremendous ideas here. I went with the folder idea in the end since I use this approach elsewhere. However I do realise I need to use absolute pathing, but this is not an issue.

like image 803
SamJolly Avatar asked Apr 16 '13 14:04

SamJolly


Video Answer


3 Answers

As long as they're in the Views directory somewhere, it shouldn't really matter. If you put it in a location other than Views/{controller} or Views/Shared, then you'll need the fully qualified location, including Views and the extension, so @Html.Partial("~/Views/Group1/PartialView1.cshtml").

Personally, if you have a lot of partials that are used in a single controller, I'd leave them in the {controller-name} directory (with a leading underscore as @IyaTaisho suggested). But if they're used by in multiple controllers, and you need to group them, I'd group them under Views/Shared/{groupName}.

Regarding one big vs. many small partials, I'd say go with many small ones. There might be a reason to do one big one now and then, but in general, I believe a partial should be as simple as possible. Remember you can always have nested partials, so if you have shared functionality or layout among many partials, you can break it into a parent partial and many child partials underneath.

like image 71
Joe Enos Avatar answered Dec 24 '22 14:12

Joe Enos


I usually add an _ in front of a partial. Example would be have a main View called Home.cshtml. The pieces (partials) on the page would have something like this: _header.cshtml, _footer.cshtml, etc.

like image 40
IyaTaisho Avatar answered Dec 24 '22 12:12

IyaTaisho


You could use a parent child file naming convention like:

header.html
header.login.html
header.searchbar.html

You could even take it a step further:

contact.helpdesk.html
contact.office.html

Re-using partials is much less frequent than unique partials, so you could use a convention for re-usable partials like:

global.partial1.html
global.partial2.html
  • Limitations are a large file directory.

  • Benifits are easy to skim, easy to sort.

like image 26
Dan Kanze Avatar answered Dec 24 '22 13:12

Dan Kanze