Skip to content

Commit

Permalink
Added support for rendering sections.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdiamond committed Feb 11, 2012
1 parent 5d87b08 commit c8e047f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
7 changes: 6 additions & 1 deletion Nustache.Core/Template.cs
Expand Up @@ -6,7 +6,12 @@ namespace Nustache.Core
public class Template : Section
{
public Template()
: base("#template") // I'm not happy about this fake name.
: this("#template") // I'm not happy about this fake name.
{
}

public Template(string name)
: base(name)
{
}

Expand Down
4 changes: 3 additions & 1 deletion Nustache.Core/TemplateDefinition.cs
@@ -1,6 +1,8 @@
namespace Nustache.Core
{
public class TemplateDefinition : Section
public class TemplateDefinition : Template // This derives from Template so that it can
// be returned from a TemplateLocator.
// Should we make it implement an interface instead?
{
public TemplateDefinition(string name)
: base(name)
Expand Down
6 changes: 5 additions & 1 deletion Nustache.Mvc3.Example/Views/Home/Index.mustache
Expand Up @@ -2,4 +2,8 @@

<p>
{{ Message }}
</p>
</p>

{{<Section}}
<p>This is a nested section.</p>
{{/Section}}
3 changes: 3 additions & 0 deletions Nustache.Mvc3.Example/Views/Shared/_Layout.mustache
Expand Up @@ -10,5 +10,8 @@

<body>
{{>Body}}

<p>Now including section defined in the view:</p>
{{>Section}}
</body>
</html>
15 changes: 11 additions & 4 deletions Nustache.Mvc3/NustacheView.cs
Expand Up @@ -19,6 +19,8 @@ public NustacheView(ControllerContext controllerContext, string viewPath, string

public void Render(ViewContext viewContext, TextWriter writer)
{
var viewTemplate = GetTemplate(_viewPath);

if (!string.IsNullOrEmpty(_masterPath))
{
var masterTemplate = GetTemplate(_masterPath);
Expand All @@ -28,18 +30,23 @@ public void Render(ViewContext viewContext, TextWriter writer)
{
if (name == "Body")
{
return GetTemplate(_viewPath);
return viewTemplate;
}
else
{
var section = viewTemplate.GetTemplateDefinition(name);
// TODO: Figure out how to render sections.
// Wouldn't those be defined in the view like in Razor?
if (section != null)
{
return section;
}
}
return null;
});
}
else
{
var viewTemplate = GetTemplate(_viewPath);
viewTemplate.Render(viewContext.ViewData.Model ?? viewContext.ViewData, writer, null);
}
}
Expand Down

0 comments on commit c8e047f

Please sign in to comment.