Added documentation to all Razor templates

This commit is contained in:
hartvig
2011-03-14 18:14:37 -01:00
parent 252a3061eb
commit 5a6313182e
4 changed files with 109 additions and 50 deletions

View File

@@ -1,3 +1,16 @@
@*
LIST SUBPAGES BY LIMIT AND DATETIME
===================================
This snippet shows how easy it is to combine different queries. It lists the children of the currentpage which is
visible and then grabs a specified number of items sorted by the day they're updated.
How it works:
- It uses the Take() method to specify a maximum number of items to output
- It adds a OrderBy() to sort the items. You can even combine this, for instance OrderBy("UpdateDate, Name desc")
NOTE: It is safe to remove this comment (anything between @ * * @), the code that generates the list is only the below!
*@
@inherits umbraco.MacroEngines.DynamicNodeContext
@{ var numberOfItems = 10; }

View File

@@ -1,2 +1,16 @@
@*
MACRO PARAMETERS
===================================
This snippet is a very simple example on how to grab values specified via Macro Parameters. Macro Parameters are
'attributes' that can be added to Macros (doesn't make sense when Razor is used inline) that makes it possible to
re-use macros for multiple purposes. When you add a Macro Parameter to a Macro, the user can send different values
to the Macro when it's inserted. Macro Parameters in Razor can be accessed via the Parameter property.
How it works:
- In this example it'll output the value specified in a Macro Parameter with the alias 'Who'.
NOTE: It is safe to remove this comment (anything between @ * * @), the code that generates the list is only the below!
*@
<h3>Hello @Parameter.Who</h3>

View File

@@ -20,17 +20,17 @@ NOTE: It is safe to remove this comment (anything between @ * * @), the code tha
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var level = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.Level);
var ulClass = String.IsNullOrEmpty(Parameter.UlClass) ? "" : String.Format(" class=\"{0}\"", Parameter.UlClass);
var parent = @Model.AncestorOrSelf(level);
if (parent != null) {
<ul@Html.Raw(ulClass)>
@foreach (var item in parent.Children.Where("Visible")) {
var selected = Array.IndexOf(Model.Path.Split(','), item.Id.ToString()) >= 0 ? " class=\"selected\"" : "";
<li@Html.Raw(selected)>
<a href="@item.Url">@item.Name (@item.Id)</a>
</li>
}
</ul>
}
var level = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.Level);
var ulClass = String.IsNullOrEmpty(Parameter.UlClass) ? "" : String.Format(" class=\"{0}\"", Parameter.UlClass);
var parent = @Model.AncestorOrSelf(level);
if (parent != null) {
<ul@Html.Raw(ulClass)>
@foreach (var item in parent.Children.Where("Visible")) {
var selected = Array.IndexOf(Model.Path.Split(','), item.Id.ToString()) >= 0 ? " class=\"selected\"" : "";
<li@Html.Raw(selected)>
<a href="@item.Url">@item.Name</a>
</li>
}
</ul>
}
}

View File

@@ -1,51 +1,83 @@
@*
HOW TO DO PAGING
=================================
Intro
This an example of how to do paging of content including a Google style page navigation. You likely want to
modify the query (first line, assigned to the 'pagesToList' variable) and the output made within the foreach
loop (<li> ... </li>)
How to customize:
-
How to Customize for re-use (only applies to Macros, not if you insert this snippet directly in a template):
- You can customize the number of items per page by adding a Macro Parameter with the alias of "ItemsPerPage"
- You can customize the labels of previous/next by adding Macro Parameters with the alias of "PreviousLabel" and
"NextLabel"
How it works:
-
- The pages to display is added to the variable 'pagesToList'. To change what pages to list, simply update the query
- The next part assigns the number of items and the previous/next labels using either default values or Macro Parameters
- Then it's using a bit of math to calculate how many pages and what should currently be displayed
- In the first <p /> element, a summary is printed. This could likely be removed
- In the <ul /> the magic happens. Notice how it's using Skip() and Take() to jump to the relevant items and iterate
over the number of items to display
- In the end it added a Google style page navigation (<<Previous 1 2 3 4 Next >>)
NOTE: It is safe to remove this comment (anything between @ * * @), the code that generates the list is only the below!
NOTE: It is safe to remove this comment (anything between @ * * @), the code that generates the list is only the below!
*@
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var pagesToList = @Model.Children;
var itemsPerPage = 3;
var numberOfItems = pagesToList.Count();
int currentPage = 1;
if (!int.TryParse(HttpContext.Current.Request.QueryString["Page"], out currentPage)) {
currentPage = 1;
}
currentPage--;
// THERE'S A BUG IN THIS COUNTER!
var numberOfPages = Math.Ceiling((decimal)(@numberOfItems / @itemsPerPage));
var pagesToList = @Model.Children;
<p>
Total Items: @numberOfItems <br />
Items per Page: @itemsPerPage<br />
Pages: @numberOfPages;<br />
Current Page: @(currentPage)
</p>
// configuration
var itemsPerPage = String.IsNullOrEmpty(Parameter.ItemsPerPage) ? 3 : int.Parse(Parameter.ItemsPerPage);
var previousLabel = String.IsNullOrEmpty(Parameter.PreviousLabel) ? "Previous" : Parameter.PreviousLabel;
var nextLabel = String.IsNullOrEmpty(Parameter.NextLabel) ? "Next" : Parameter.NextLabel;
<ul>
@foreach(var item in pagesToList.Skip(currentPage*itemsPerPage).Take(itemsPerPage))
{
<li>@item.Name</li>
}
</ul>
// paging calculations
var numberOfItems = pagesToList.Count();
int currentPage = 1;
if (!int.TryParse(HttpContext.Current.Request.QueryString["Page"], out currentPage)) {
currentPage = 1;
}
currentPage--;
var numberOfPages = numberOfItems % itemsPerPage == 0 ? Math.Ceiling((decimal)(numberOfItems / itemsPerPage)) : Math.Ceiling((decimal)(numberOfItems / itemsPerPage))+1;
var Pages = Enumerable.Range(1, (int)numberOfPages);
foreach(var number in Pages) {
if (number-1 != currentPage) {
<a href="?page=@number">@number</a>
} else {
@number
}
@Html.Raw("&nbsp&nbsp");
}
}
<p>
Total Items: @numberOfItems <br />
Items per Page: @itemsPerPage<br />
Pages: @numberOfPages;<br />
Current Page: @(currentPage)
</p>
<ul>
@foreach(var item in pagesToList.Skip(currentPage*itemsPerPage).Take(itemsPerPage))
{
<li>@item.Name</li>
}
</ul>
<p class="pagingPages">
@{
// Google style paging links
if (currentPage > 0) {
<a href="?page=@(currentPage)">&laquo; @previousLabel</a>
} else {
<span class="pagingDisabled">&laquo; @previousLabel</span>
}
var Pages = Enumerable.Range(1, (int)numberOfPages);
foreach(var number in Pages) {
if (number-1 != currentPage) {
<a href="?page=@number">@number</a>
} else {
@number
}
@Html.Raw("&nbsp&nbsp");
}
if (currentPage < Pages.Count()-1) {
<a href="?page=@(currentPage+2)">@nextLabel &raquo;</a>
} else {
<span class="pagingDisabled">@nextLabel &raquo;</span>
}
}
</p>
}