From 5a6313182ecc515e6b495dc61157723f1b79da5e Mon Sep 17 00:00:00 2001 From: hartvig Date: Mon, 14 Mar 2011 18:14:37 -0100 Subject: [PATCH] Added documentation to all Razor templates --- .../cshtml/ListSubPagesByDateAndLimit.cshtml | 13 +++ .../templates/cshtml/Macro-Parameters.cshtml | 14 +++ .../templates/cshtml/Navigation.cshtml | 26 ++--- .../scripting/templates/cshtml/Paging.cshtml | 106 ++++++++++++------ 4 files changed, 109 insertions(+), 50 deletions(-) diff --git a/umbraco/presentation/umbraco/scripting/templates/cshtml/ListSubPagesByDateAndLimit.cshtml b/umbraco/presentation/umbraco/scripting/templates/cshtml/ListSubPagesByDateAndLimit.cshtml index 66f9d58907..2fa3234997 100644 --- a/umbraco/presentation/umbraco/scripting/templates/cshtml/ListSubPagesByDateAndLimit.cshtml +++ b/umbraco/presentation/umbraco/scripting/templates/cshtml/ListSubPagesByDateAndLimit.cshtml @@ -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; } diff --git a/umbraco/presentation/umbraco/scripting/templates/cshtml/Macro-Parameters.cshtml b/umbraco/presentation/umbraco/scripting/templates/cshtml/Macro-Parameters.cshtml index c124e1c8ad..bf54810cc9 100644 --- a/umbraco/presentation/umbraco/scripting/templates/cshtml/Macro-Parameters.cshtml +++ b/umbraco/presentation/umbraco/scripting/templates/cshtml/Macro-Parameters.cshtml @@ -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! +*@ +

Hello @Parameter.Who

\ No newline at end of file diff --git a/umbraco/presentation/umbraco/scripting/templates/cshtml/Navigation.cshtml b/umbraco/presentation/umbraco/scripting/templates/cshtml/Navigation.cshtml index 159be9928f..a9d2f0f114 100644 --- a/umbraco/presentation/umbraco/scripting/templates/cshtml/Navigation.cshtml +++ b/umbraco/presentation/umbraco/scripting/templates/cshtml/Navigation.cshtml @@ -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) { - - @foreach (var item in parent.Children.Where("Visible")) { - var selected = Array.IndexOf(Model.Path.Split(','), item.Id.ToString()) >= 0 ? " class=\"selected\"" : ""; - - @item.Name (@item.Id) - - } - -} + 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) { + + @foreach (var item in parent.Children.Where("Visible")) { + var selected = Array.IndexOf(Model.Path.Split(','), item.Id.ToString()) >= 0 ? " class=\"selected\"" : ""; + + @item.Name + + } + + } } diff --git a/umbraco/presentation/umbraco/scripting/templates/cshtml/Paging.cshtml b/umbraco/presentation/umbraco/scripting/templates/cshtml/Paging.cshtml index f9f71a7851..71d112cfe0 100644 --- a/umbraco/presentation/umbraco/scripting/templates/cshtml/Paging.cshtml +++ b/umbraco/presentation/umbraco/scripting/templates/cshtml/Paging.cshtml @@ -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 (
  • ...
  • ) -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

    element, a summary is printed. This could likely be removed +- In the

      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 (<>) -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; -

      - Total Items: @numberOfItems
      - Items per Page: @itemsPerPage
      - Pages: @numberOfPages;
      - Current Page: @(currentPage) -

      + // 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; -
        - @foreach(var item in pagesToList.Skip(currentPage*itemsPerPage).Take(itemsPerPage)) - { -
      • @item.Name
      • - } -
      + // 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) { - @number - } else { - @number - } - @Html.Raw("  "); - } -} \ No newline at end of file +

      + Total Items: @numberOfItems
      + Items per Page: @itemsPerPage
      + Pages: @numberOfPages;
      + Current Page: @(currentPage) +

      + +
        + @foreach(var item in pagesToList.Skip(currentPage*itemsPerPage).Take(itemsPerPage)) + { +
      • @item.Name
      • + } +
      + +

      + @{ + // Google style paging links + if (currentPage > 0) { + « @previousLabel + } else { + « @previousLabel + } + + var Pages = Enumerable.Range(1, (int)numberOfPages); + foreach(var number in Pages) { + if (number-1 != currentPage) { + @number + } else { + @number + } + @Html.Raw("  "); + } + + if (currentPage < Pages.Count()-1) { + @nextLabel » + } else { + @nextLabel » + } + } +

      +}