Resources
Reuse Shared Templates
Partial templates are cshtml
files which you can use multiple times. Typically they will contain something which you need in many places, like a menu or a data-visualizer which gets data as a parameter, and then creates a different table each time. Here's how to use it with Html.Partial(...)
.
Passing Parameters/Values
If you pass values to the Razor Control on older Base classes such as Razor14
, the parameters arrive on the DynamicModel
object.
On the new typed base class RazorPro
it will be in the MyModel
object (see examples below)
Just render the same, external file into here
This uses @Html.Partial
to get the file line.cshtml
3 times.
Output
Passing in Parameters/Values
The next example passes in a word to the sub-template, so that can change what it does based on the variable. The called template will get the parameters on DynamicModel
(see code below)
Output
Passing in Parameters/Values Pro
This is the same, but using the RazorPro
(new v16). The called template will get the parameters on MyModel
(see code below)
Output
Source Code of this file
Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials. Click to expand the code
@inherits Custom.Hybrid.Razor14 @using ToSic.Razor.Blade; <!-- unimportant stuff, hidden --> <div @Sys.PageParts.InfoWrapper()> @Html.Partial("../shared/DefaultInfoSection.cshtml") <div @Sys.PageParts.InfoIntro()> <h2>Reuse Shared Templates</h2> <p> Partial templates are <code>cshtml</code> files which you can use multiple times. Typically they will contain something which you need in many places, like a menu or a data-visualizer which gets data as a parameter, and then creates a different table each time. Here's how to use it with <code>Html.Partial(...)</code>. </p> <h3>Passing Parameters/Values</h3> <p> If you pass values to the Razor Control on older Base classes such as <code>Razor14</code>, the parameters arrive on the <code>DynamicModel</code> object. </p> <p> On the new typed base class <code>RazorPro</code> it will be in the <code>MyModel</code> object (see examples below) </p> </div> </div> <h3>Just render the same, external file into here</h3> <p>This uses <code>@@Html.Partial</code> to get the file <code>line.cshtml</code> 3 times.</p> @Html.Partial("line.cshtml") @Html.Partial("line.cshtml") @Html.Partial("line.cshtml") <h3>Passing in Parameters/Values</h3> <p>The next example passes in a word to the sub-template, so that can change what it does based on the variable. The called template will get the parameters on <code>DynamicModel</code> (see code below)</p> @Html.Partial("box.cshtml", new { Label = "Hello, this is the first line" }) @Html.Partial("box.cshtml", new { Label = "Second line!", Color = "red" }) @Html.Partial("box.cshtml", new { Label = "I'm last!", Color = "blue" }) <h3>Passing in Parameters/Values Pro</h3> <p>This is the same, but using the <code>RazorPro</code> (new v16). The called template will get the parameters on <code>MyModel</code> (see code below)</p> @Html.Partial("box.cshtml", new { Label = "Hello, this is the first line" }) @Html.Partial("box.cshtml", new { Label = "Second line!", Color = "red" }) @Html.Partial("box.cshtml", new { Label = "I'm last!", Color = "blue" }) @* Footer *@ @Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })