Umbraco 7: Macros vs Partial Views - umbraco

I'm new to Umbraco, wanted to know when does one use Macros over Partial Views and vice versa.
My understanding is that, both are used to create reusable views on different content pages.
Can someone please help me get an understanding of the same. I'm using Umbraco 7.2.1
Thanks.

Macros are generally reusable elements you want the content editor to use in rich text editors or the grid.
Partial views are reusable elements you as a developer use in your templates.
Macro's have some other benefits over partial views like built in caching, parameters that can be defined in the backoffice and they can hide exceptions from the end user.

Related

MVC editor templates in different assembly

I'd like to create a common assembly that I can use in several MVC projects (MVC 4 preferred). This assembly has to provide editor templates for primitive types. For example, a date picker. How can I do this?
I know of two ways to achieve similar goals, but I don't like those. One way is to forget about MVC's templating system and provide custom HtmlHelper extensions, like #Html.MyOwnDatePickerFor(x=>x.Field). I know of some UI frameworks that do this. I don't like it because it is not transparent (#Html.EditorFor() won't pick it up automatically) and it feels weird to have an editor template system in MVC and not use it. The other way is the actual MVC template system, by creating partial views named like EditorTemplates/datatype.cshtml (or .ascx), but it doesn't work because they have to be in the MVC project, I don't know how I can share these between multiple projects.
Any suggestions?
We have a few classes that provide the equivalent of editorfor but surrounded by different HTML. We have one for #Html.CMSEditorFor(m => m.Name) that uses the same HTML as our content management system and one for #Html.BootstrapEditorFor(m => m.Name) that churns out HTML in a nice Bootstrap compatible way.
I can't claim any credit for the idea though it comes directly from here.
ASP.NET MVC 3 Custom HTML Helpers- Best Practices/Uses
You're looking for RazorGenerator. It's made by the Razor team to allow you to compile Razor views just for this purpose: reusing the views by including the assembly in other projects.

Is it possible to use razor layouts with Orchard CMS and bypass the theming

A little bit of context
We have a recent MVC4 web application used by a lot people that we would like to port to Orchard CMS. Even though this is an "admin" type of application, we don't want it to be part of the admin section of Orchard. This application is currently live and we would like the move to Orchard to be as invisible as possible to the end-user.
Where we are now
We followed this answer as how to Integrate existing ASP.NET MVC application with Orchard CMS but we use layouts (Razor not Orchard) and it is not working (by default) with the way Orchard renders pages (the views returned by the controllers actions are displayed without the layouts - _ViewStart.cshtml is ignored). I have tried to specify the path of the layout in the View method but it doesn't work or the path is not good since we are in a module. Is there a way to tell Orchard to use a _layout.cshtml file and bypass Orchard theming?
It is possible to bypass Orchard themes. Note that I am doing this with MvcMailer, so the outputted e-mail is "templated" HTML with a Layout. I cannot definitively say this works on regular pages, but you can try it. Just slap this in your view:
#inherits System.Web.Mvc.WebViewPage<object>
#{
Layout = "_Layout.cshtml";
}
Caveat: you cannot use the newer #model directiv. I just fall back to the old style and put my model where "object" is:
#using Namesapce.ViewModels
#inherits System.Web.Mvc.WebViewPage<MyViewModel>
Otherwise, it's pretty straightforward to create a Theme and override any of Orchard's shapes. There are two ways I do it:
Create a straightforward theme, override shapes (say you want a
different Login, just create Login.cshtml in the Theme's ~/View
folder), use alternates (use Layout-homepage.cshtml to override the
layout for the homepage), have your .css, etc.;
Use a couple of tricks to override the theme with code from a module, and create
your own views and shapes in a theme folder. You can, for example, have a Layout.cshtml file in your theme with code that displays a shape. However, the shape itself (.cshtml file) lives in your module, which is great for using a dashboard type theme that will
call a shape that is named the same in your different modules but
shows different content. The code for this is a bit more in-depth. Just ask if you need samples.
tl;dr No, it is not possible.
I don't like answering my own questions but since Bertrand Le Roy commented my question instead of answering it (no hard feelings) and he is part (or was?) of the Orchard team, I will answer with what I know, until someone comes with a better answer.
From what I understand, to be part of an Orchard project is to embrace it all the way, there is no in between. The custom (Orchard) view engine (which works with zones, layouts, widgets, shapes, etc.) cannot be bypassed (fallback to default MVC engine) in a module. As Bertrand suggested, I should convert my project to Orchard theming.

Why Views and partial views have the same cshtml file extension in Razor?

I'm learning some basic stuffs in razor. I'm a little confused about the extension file: views and partial views seem to share the same extension cshtml. I thought that was a mistake on my behalf, but I've re-checked the checkbox, I get the same extension for both types of files.
Besides, with webform view engine, it's easier to locate views by their colors or/and extensions (View are white-.aspx, Partial are yellow-.ascx, Master are white/blue-.master)
If that's the way it has to be now, why would views and partial views share the same extension? Is there any reason they made them all have the same extension?
Thanks for helping
Razor views and partial views don't need a seperate file extension because both use the same base class.
In contrast, WebForms (ASPX) pages, user controls, and master pages all derive from different base classes which is why the distinction was necessary. It's simply a result of design decisions made during the creation of Asp.Net WebForms.

mvc rebuild strongly typed view

is there any way to re-build a strongly typed view when the model class has new fields added?
At present I have not modified the initial generated view so deleting and re-creating is not a problem.
When I start to customize it to my liking I will lose all changes and I was wondering if there was a good way to manage this?
Thanks
ASP.NET MVC offers two types of scaffolding, each with its own advantages:
The first kind of scaffolding is design-time scaffolding, which is done through the Add View dialog and T4 templates. The advantage of this is that the code is entirely generated and you can completely customize it. The disadvantage is that if you change your model you have to regenerate your view (by deleting it and adding back a new one).
The second kind of scaffolding is runtime scaffolding, which is done through the Html.EditorFor() and Html.DisplayFor() methods in your view. The advantage of this is that if your model changes then the scaffolding will be automatically generated at runtime. The disadvantage is that you cannot directly customize the rendering. You can, however, give this scaffolding many hints using DataAnnotations attributes such as [DisplayText], [UIHint], and so forth, so it is quite flexible - but it is not nearly as flexible as being able to 100% customize the rendering.
To customize the rendering of runtime scaffolding (editor templates and display templates) you can find more info on Brad Wilson's blog series.
You can manually add code to your view to reflect any changes in the model.
Default scafolding is there just to give you something.
One thing you might want to look at is t4 templates using wich mvc tooling generates your default views. google for it there are examples of how you can copy those to your project and modify to have mvc generate views you want instead of default ones.

Strategies for dealing with CSS in ASP.Net MVC UserControls

I have just started playing with the ASP.Net MVC framework, and today I created a simple UserControl that uses some CSS. Since the CSS was declared in a separate file and included in the View that called the UserControl, and not in the UserControl itself, Visual Studio could not find any of the CSS classes used in the UserControl. This got me thinking about what would be the most appropriate way of dealing with CSS in UserControls.
Declaring the CSS in the View that is using the UserControl gives more flexibility if the same control is used in different contexts and needs to be able to adapt to the style of the calling View.
Having the UserControl supply its own CSS would lead to a more clear separation, and the Views would not need to know anything about the HTML/CSS generated by the UserControl, but at the cost of a fixed look of the control.
Since I am totally new to the framework, I'm guessing people have already come to some good conclusions about this.
So, would you have the UserControl handle its own CSS, should it depend on the CSS declared in the calling View, or is there another, better solution?
If you look at a skinable toolkit like Yahoo UI it documents the classes used by each control and then provides a single skin file for the entire toolkit. By swapping out the single skin file you can change looks for your entire site.
I would assume that 99.9% of the time you would want to custom skin your controls and not have them come predefined with a look and feel.
As an example here are the CSS defines for Yahoo's TabView control
It should always be in your global CSS really. If you pass this on to a designer, you dont want to have to explain which control defines x style, etc.
A quick point... it's ok for your Views to be aware of HTML... that's what they are for. What I would recommend (if you want to be ubber cool), is to add a parameter to your "MVC UserControl" that specifies the class name. Example:
<%= MyHelperClass.Marquee("This text will scroll!!!", "important-text") %>
I'm of course pretending that "important-text" is the class name that I want to add to my control.
I am assuming that when you say "UserControl", you're referring to an example like in that link above.

Resources