Template-First Definitions (MVC Razor)

Fluent Parts allow you to define a part directly from within a Razor view file. A number of simple examples of these type of parts are available in ~/Dinamico/Themes/Default/Views/ContentParts 

How N2 Finds and Enumerates Fluent Parts

Enumeration of the Dinamico templates and parts are done in the GlobalMvcStarter.cs file that's included in the default distribution of Dinamico. The default code looks in the ~/Dinamico/Themes/ folder. Find it on Github here: https://github.com/n2cms/n2cms/blob/master/src/Mvc/Dinamico/Dinamico/GlobalMvcStarter.cs

 

        public static void RegisterViewEngines(ViewEngineCollection viewEngines) {
            viewEngines.RegisterThemeViewEngine<RazorViewEngine>("~/Dinamico/Themes/");
            viewEngines.DecorateViewTemplateRegistration();
        }

 

The base type of the fluent part is defined as a model of type M decorated with [PartDefinition] and there is a controller with [Controls(typeof(M))]. For example:

Sample: Content Part Controller

namespace Dinamico.Controllers {
    [Controls(typeof(Models.ContentPart))]
    public class ContentPartsController : ContentController<Models.ContentPart> {
        public override ActionResult Index() {
		    return PartialView((string)CurrentItem.TemplateKey, CurrentItem);
        }
    }
}
namespace Dinamico.Models {
    /// <summary>This part model is the base of several "template first" definitions located in /dinamico/default/views/contentparts/ </summary>
    [PartDefinition, WithEditableTemplateSelection(ContainerName = Defaults.Containers.Metadata)]
    public class ContentPart : PartModelBase
    { /* ... */ }
}

Sample: Raw HTML part 

@model Dinamico.Models.ContentPart
@{ 
    Content.Define(a => {
        a.Title = "Raw HTML";
        a.Image("/N2/Resources/Icons/Html.png");
        a.Text("HTML").Configure(txt => { 
            txt.Columns = 60;
            txt.Rows = 10;
            txt.TextMode = System.Web.UI.WebControls.TextBoxMode.MultiLine;
        });
    });
}
@if (Content.Has.HTML) { <div>@Html.Raw(Content.Data.HTML)</div> }