GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing docs.servicestack.net — Markdown Razor Markdown RazorMarkdown ConfigAn alternative to enabling the Markdown Razor View Engine is to use new
Providing a static API that can be used to transform Markdown to HTML with:
Alternative Markdown implementations can be used by providing an adapter for the
Markdown Razor View EngineMarkdown Razor is the first HTML and Text (i.e. Markdown) view engine built into ServiceStack. The pages are simply plain-text Markdown surrounded by MVC Razor-like syntax to provide its enhanced dynamic functionality. ConfigureMarkdown Razor support is available by regitering the
Extensible with custom base classes and HelpersMarkdown Razor is extensible in much the same way as MVC Razor is with the ability to define and use your own custom base class, Helpers and HtmlHelper extension methods. This allows you to call util methods on your base class or helpers directly from your templates. You can define a base class for all your markdown pages by implementing MarkdownViewBase and register it in your AppHost with:
If a WebHostUrl is specified, it replaces all ~/ in all static website and Markdown pages with it. The MarkdownGlobalHelpers allow you to define global helper methods available to all your pages. This has the same effect of declaring it in your base class e.g:
Which you can access in your pages via @Ext.MyHelper(Model). Declaring instance methods on your custom base class allows you to access them without any prefix. MarkdownViewBase base classBy default the MarkdownViewBase class provides the following properties and hooks:
See this websites CustomMarkdownPage.cs base class for an example on how to effectively use the base class to Resolve dependencies, inspect generated variables, generate PagesMenu and other dynamic variables for output in the static website template. Compared with ASP.NET MVC Razor SyntaxFor the best way to illustrate the similarities with ASP.NET MVC Razor syntax I will show examples of the Razor examples in ScottGu's introductory Introducing "Razor" - a new view engine for ASP.NET Note: more context and the output for each snippet and example displayed is contained in the Introductory Example and Introductory Layout Unit tests. For reference most features of Mardown Razor view engine are captured in the Core Template Unit Tests Hello World Sample with RazorThe following basic page:
Can be generated in MVC Razor with:
And Markdown Razor with:
Loops and Nested HTML SampleThe simple loop example:
With MVC Razor:
With Markdown Razor:
Parens-FreeAt this point I think it would be a good to introduce some niceties in Markdown Razor of its own. Borrowing a page out of BrendanEich proposal for CoffeeScript's inspired Parens free syntax for JS.Next - you can simply remove the parens from all block statements e.g:
Produces the same output, and to go one step further you can remove the redundant var as well :)
Which makes the Markdown Razor's version a bit more wrist-friendly then its MVCs cousin :) If-Blocks and Multi-line StatementsIf statements in MVC Razor:
If statements in Markdown Razor:
Multi-line and Multi-token statements
Markdown Razor doesn't support multi-line or multi-token statements, instead you are directed to take advantage for variable syntax declarations, e.g: Markdown replacement for Multi-line Statements
Integrating Content and CodeDoes it break with email addresses and other usages of in HTML? With MVC Razor
With Markdown Razor
Both View engines generate the expected output, e.g:
Identifying Nested ContentWith MVC Razor
With Markdown Razor
Markdown Razor doesn't need to do anything special with text blocks since all it does is look for the ending brace '}'. This means if you want to output a brace literal '{' then you have to double escape it with '{{' or '}}'. HTML EncodingMarkdown Razor follows MVC Markdown behaviour where by default content emitted using a @ block is automatically HTML encoded to better protect against XSS attack scenarios. If you want to avoid HTML Encoding you have the same options as MVC Razor where you can wrap your result in @Html.Raw(htmlString) or if you're using an Extension method simply return a MvcHtmlString instead of a normal string. Markdown also lets you mix and match HTML in your markdown although any markdown between the tags
does not get converted to HTML. To tell Markdown Razor to evaulate the contents inside html
<tag>...</tag>'s need to prefixed with
If we didn't prefix Layout/MasterPage Scenarios - The BasicsMarkdown Razor actually deviates a bit from MVC Razor's handling of master layout pages and website templates (we believe for the better :). Simple Layout ExampleMVC Razor's example of a simple website template
Rather then using a magic method like
By default we use convention to select the appropriate website template for the selected view where it uses the nearest default.shtml static template it finds, looking first in the current directory than up parent directories. Your View page names must be unique but can live anywhere in your /View directory so you are free to structure your website templates and view pages accordingly. If for whatever reason you need more granularity in selecting website templates than we provide similar options to MVC for selecting a custom template: Select Custom Template with MVC Razor
With Markdown Razor
Note: In addition to @Layout we also support the more appropriate alias of @template. Layout/MasterPage Scenarios - Adding Section OverridesMVC Razor allows you to define sections in your view pages which you can embed in your Master Template: With MVC Razor:
And you use in your website template like so:
With Markdown Razor:Markdown Razor supports the same @section construct but allows you to embed it in your template via the standard variable substitution convention, e.g:
And these sections and body can be used in the website template like:
Encapsulation and Re-Use with HTML HelpersIn order to encapsulate and better be able to re-use HTML Helper utils MVC Razor includes a few different ways to componentize and re-use code with HTMLHelper extension methods and declarative helpers. Code Based HTML HelpersHtmlHelper extension methods with MVC Razor:
Since we've ported MVC's HtmlHelper and its Label, TextBox extensions we can do something similar although to make this work we need to inherit from the MarkdownViewBase<TModel> generic base class so we know what Model to provide the strong-typed extensions for. You can do this using the @model directive specifying the full type name:
Whilst we ported most of MVC HtmlHelper extension methods as-is, we did rip out all the validation logic which appeared to be unnecessarily complex and too coupled with MVC's code-base. Note: Just as it is in MVC the @model directive is a shorthand (which Markdown Razor also supports) for: @inherits Namespace.BaseType<Namespace.ModelType> Whilst we don't support MVC Razors quasi C# quasi-html approach of defining declarative helpers, we do allow you to on a per instance basis (or globally) import helpers in custom Fields using the @helper syntax:
You can register Global helpers and a custom base class using the MarkdownGlobalHelpers and MarkdownBaseType AppHost Config options as shown at the top of this article. SummaryWell that's it for the comparison between MVC Razor and Markdown Razor as you can see the knowledge is quite transferable with a lot of cases the syntax is exactly the same. As good as MVC Razor is with its wrist-friendly and expressive syntax, we believe Razor Markdown is even better! Where thanks to Markdown you can even dispense with most of HTML's boiler plage angle brackets :) We think it makes an ideal solution for content heavy websites like this one. Unlike ASP.NET's MVC Razor, Markdown Razor like all of ServiceStack is completely Open Source and as such we welcome the contribution from the community via new features, Unit and regression tests, etc. Community ResourcesOn This Page
|
| You are visitor 01402809 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |