Welcome to the Book Club example!
This is the Homepage template, you can see it at /Views/Umbraco/Homepage.cshtml on your filesystem. It contains some example markup for developers.
Putting values into your templates
This template is a standard View from the Model-View-Controller pattern on which Umbraco 5 is built.
It uses Razor syntax, and in addition to the standard objects available to you in Views, we've added a couple of our own.
DynamicModel
First up we have DynamicModel
. This allows you to access the fields of your piece of content in short-hand syntax.
For example, this page is a Homepage type, so it has a Name field. By using the code @DynamicModel.Name
in this template, we get: Homepage
Let's put another field here, 'BodyText'. Using the same style code @DynamicModel.BodyText
, we get: <p>Andreas er kul!</p>.
Actually, BodyText
is a long field, so we truncated that using another new concept: the Umbraco
helper object and its Truncate
method.
First a standard one: Model
. Model
is the statically-typed way of accessing data.
Model
Like regular MVC Views, you have a Model object to play with too. It's a style of accessing the same data as DynamicModel
, and it's really just about your preference.
This is a "statically-typed" object, meaning you will get Intellisense in Visual Studio or WebMatrix if you choose to use it. Since DynamicModel
has some of the handy shortcuts, its properties like 'BodyText'
above are not known until the page is generated, so whilst it's shorter to write, Visual Studio can't help you.
To get at the parts of your content, Model
can be used with the Field<T>(string fieldName)
helper method, like so: @Model.Field<string>("siteName")
. This gives: My Site.
Hierarchy navigators
In the latest update we've added more hierarchy navigators to the model so that you can grab parents, children, ancestors and descendants of your data similarly to how you can in Umbraco 4. Here's some examples:
- @DynamicModel.Parent
- @DynamicModel.Children
- @DynamicModel.Ancestors
- @DynamicModel.AncestorsOrSelf
- @DynamicModel.Descendants
- @DynamicModel.DescendantsOrSelf
Because @DynamicModel is a .NET dynamic
object, "extension methods" like Count() can't be used in their regular fashion, so instead of myDynamicObjects.Count() you'd have to run Enumerable.Count(myDynamicObjects). However, to keep some of this convenience, we've added these methods directly to the collection you get when you use one of these navigators:
- @DynamicModel.Children.Count() (for your current site, this returns: 7)
- @DynamicModel.Children.Any()
- @DynamicModel.Children.First()
- @DynamicModel.Children.FirstOrDefault()
- @DynamicModel.Children.Last()
- @DynamicModel.Children.LastOrDefault()
- @DynamicModel.Children.Single()
- @DynamicModel.Children.SingleOrDefault()
- @DynamicModel.Children.ElementAt(int)
- @DynamicModel.Children.ElementAtOrDefault(int)
- @DynamicModel.Children.IndexOf(item)
Querying: A quick example query using Linq
In the current release, we've started to implement querying. At the moment, you can only do a query from the root rather than querying only the children. But it's still a powerful step.
Let's query for Books in the club: 5
Or how about a distinct list of Publishers in the club: John Wiley & Sons, APRESS, Packt Publishing, Eyrolles
Let's do that as a list:
- John Wiley & Sons
- APRESS
- Packt Publishing
- Eyrolles
An example of querying using a string
For those coming from Umbraco 4.7, or if you're not comfortable or familiar with lambda expressions, we are working on dynamic queries using strings
Let's get the first book published by APRESS: it is "Pro ASP.NET MVC 3 Framework" and it has 824 pages
Here's the code for this:
@{var book = Hive.Content.First("publisher == \"APRESS\"");} <p>Let's get the first book published by APRESS: it is <strong>"@book.Name"</strong> and it has <strong>@book.NumberOfPages</strong> pages</p>
To avoid injection attacks or ugly escaping, you can also use parameters in your string query, like:
@{var firstItem = Hive.Content.First("publisher == @0", "APRESS");}
Other tips, including parents and children
- The current page was retrieved from Hive provider 'nhibernate', and matches Hive group content:///.
- Its path is: /
Let's render a macro (a "partial view" in MVC)
Child content retrieved from the Model
Child node name: Books, of type Books Page.
Child node name: Installing modules, of type Content Page.
Child node name: Getting started, of type Content Page.
Child node name: Faq, of type Faq.
Child node name: Contact, of type Content Page.
Child node name: Latest from the Umbraco Blog, of type Content Page.
Child node name: A no template content node, of type Content Page.
Child content retrieved from the DynamicModel
Child node name: Books, of type Books Page.
Child node name: Installing modules, of type Content Page.
Child node name: Getting started, of type Content Page.
Child node name: Faq, of type Faq.
Child node name: Contact, of type Content Page.
Child node name: Latest from the Umbraco Blog, of type Content Page.
Child node name: A no template content node, of type Content Page.