0 votes

RazorTemplates

Sugar for RenderPage (and Html.Partial - see below)

- an efficient way of rendering razor templates
It picks up changes immediately and requires no app restarts

Demonstration http://www.screenr.com/4Sz7

Newest version (0.97 BETA - for Umbraco 4.7+ (not 6)), more convention based sugar:

Is it fast enough? It should be, it is built in RenderPage behaviour (and same as Partial in MVC): The razor pages are compiled on first use and kept in memory after that. Even if you have a complex template structure it should add minimum overhead.

@using RazorTemplates

 @{var tmpl = new Templates(Model);}
 @tmpl.Header

   // renders /macroscripts/{doctypealias}/header.cshtml (with model header)

  // fallbacks to
  // /macroscripts/shared/header.cshtml
  // /macroscripts/{doctypealias}/_default.cshtml
  // /macroscripts/shared/_default.cshtml

which migth simply look like:
<h1>@PageData[0]</h1>

PageData[0] is the "template view model"

Usage:
@using RazorTemplates
@{ dynamic tmpl = new Templates(); }
@tmpl.Render(someObject)

It checks type name of the object - or NodeTypeAlias if that property exists and renders the .cshtml with that name sending someObject to PageData[0]
Default template location : /macroScripts (= the standard umbraco razor path)

Alternative usage:
@tmpl.SomeRazorFileName(someModel)

The optional parameter(s) can be used in the .cshtml by the use of PageData[], for example:
@{var viewModel = (MyModelType)PageData[0];}

Also - RenderPage keeps the context, so you can use the existing Model

This means you can save templates for your Document types in Umbraco and render the correct template just by using @template.Render(myNode) renders /macroScripts/MyNodeTypeAlias.cshtml

If you like to render different templates at different times, just specify template folder to find the templates in
Templates("~/wherever") keeps the path as specified
Templates("/relativeToTemplateRoot") uses path relative to /macroScripts
Templates("relativeToCurrent") uses path relative to current path (i.e. subtemplates in same folder)
Templates() uses /macroScripts
Templates("") uses current path

Usage in MVC:

Version 0.90 is for Umbraco 6 and includes both rendering for WebPages (based on RenderPage) and MVC (based on Html.Partial). Usage in MVC:

 

@{var t = Html.Templates();}
@t.Render(Model)

 

Screenshots

Package owner

Jonas Eriksson

Jonas Eriksson

Jonas has 1825 karma points

Package Compatibility

This package is compatible with the following versions as reported by community members who have downloaded this package:
Untested or doesn't work on Umbraco Cloud
Version 8.18.x (untested)

You must login before you can report on package compatibility.

Previously reported to work on versions:

Package Information

  • Package owner: Jonas Eriksson
  • Created: 07/03/2013
  • Current version 0.96
  • License MIT
  • Downloads on Our: 1.8K