 | HelpersDefineSection Method |
Assigns content to a named section.
Namespace: HandlebarsDotNet.MvcAssembly: HandlebarsDotNet.Mvc (in HandlebarsDotNet.Mvc.dll) Version: 1.0.0-beta
Syntaxpublic static void DefineSection(
TextWriter writer,
HelperOptions options,
Object context,
params Object[] arguments
)
Public Shared Sub DefineSection (
writer As TextWriter,
options As HelperOptions,
context As Object,
ParamArray arguments As Object()
)
public:
static void DefineSection(
TextWriter^ writer,
HelperOptions^ options,
Object^ context,
... array<Object^>^ arguments
)
static member DefineSection :
writer : TextWriter *
options : HelperOptions *
context : Object *
arguments : Object[] -> unit
Parameters
- writer
- Type: System.IOTextWriter
The TextWriter provided by HandlebarsDotNet - options
- Type: HelperOptions
The HelperOptions provided by HandlebarsDotNet - context
- Type: SystemObject
The context (model) provided by HandlebarsDotNet - arguments
- Type: SystemObject
The arguments from the view, provided by HandlebarsDotNet
Remarks
This helper is among the ones registered if
RegisterSectionsHelpers is called. If so, it is registered as
definesection but you can choose your own name for this:
var hbsve = new HandlebarsViewEngine();
hbsve.RegisterHelper("definesection", HandlebarsDotNet.Mvc.Helpers.DefineSection);
It works like Razor's
@section statement. For a nice write-up, see
https://weblogs.asp.net/scottgu/asp-net-mvc-3-layouts-and-sections-with-razor.
Usage
{{#definesection [name] [mode="replace"|"append"|"prepend"]}}
content here
{{/definesection}}Arguments
- name
- string (required) - The name of the section.
- mode
- string (required) - The operation mode. Specifying mode is not required but if specified it must be one of three values: "replace" (default), "append" or "prepend".
Description
This helper allows splitting up the normal contigous HTML contents in view files so a section can be rendered at a specified location within a layout.
Normally the whole contents from the view is rendered by
{{{body}}} in a layout file, but this helper takes it out of the normal flow
so it can be rendered at the specified point by
{{rendersection "..."}} in the layout.
See the example for how it can be used.
The
mode attribute can take different values:
- mode="replace"
- This ignores any previously defined section with this name and just sets the contents. This is the default.
- mode="append"
- Appends the content to any previously defined section with this name.
- mode="prepend"
- Prepends the content to any previously defined section with this name.
For the
append and
prepend modes the named section doesn't have to be defined beforehand. In those cases it works as
mode="replace".
Examples{{!< default}}
{{#definesection "sidebar"}}
For more info, see<br />
<a href="link1.html">Link 1</a><br />
<a href="link2.html">Link 2</a><br />
{{/definesection}}
~/Views/_Layouts/default.hbs
{{#if (issectiondefined "sidebar")}}
<div id="sidebar">
{{{rendersection "sidebar"}}}
</div>
{{else}}
<p>Default sidebar content...</p>
{{/if}}
See Also