<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="https://shazwazza.com/rss/xslt"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Shazwazza</title>
    <link>https://shazwazza.com/</link>
    <description>My blog which is pretty much just all about coding</description>
    <generator>Articulate, blogging built on Umbraco</generator>
    <image>
      <url>/media/0libq25y/frog.png?rmode=max&amp;v=1da0e911f4e6890</url>
      <title>Shazwazza</title>
      <link>https://shazwazza.com/</link>
    </image>
    <item>
      <guid isPermaLink="false">1165</guid>
      <link>https://shazwazza.com/post/partial-view-macros-in-umbraco-v5/</link>
      <category>Umbraco</category>
      <title>Partial View macros in Umbraco v5</title>
      <description>&lt;h2&gt;Disclaimer&lt;/h2&gt; &lt;p&gt;&lt;em&gt;This post is about developing for Umbraco v5 (Jupiter) which at the time of this post is still under development. The technical details described below may change by the time Umbraco Jupiter is released. If you have feedback on the technical implementation details, please comment below.&lt;/em&gt;  &lt;h1&gt;Macro Types&lt;/h1&gt; &lt;p&gt;In Umbraco v5, there are &lt;em&gt;currently&lt;/em&gt; 3 types of Macros:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Xslt  &lt;li&gt;Partial View  &lt;li&gt;Surface Action&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The Xslt macro will remain very similar to the v4 Xslt macro, but the 2 other types are brand new. This post will cover the new &lt;strong&gt;Partial View Macro.&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;A Partial View in MVC is sort of synonymous with a User Control in WebForms. Its simply just a Razor view as a cshtml file. If you like Umbraco v4’s Razor macros, then you’ll be pleased to know that Razor is absolutely native to v5 and the Partial View Macro will be one of your best tools for working with v5.&lt;/p&gt; &lt;h1&gt;Creating a Partial View macro&lt;/h1&gt; &lt;p&gt;The first thing you’ll have to do is create the partial view, which you will need to save in the &lt;strong&gt;~/Views/Umbraco/Partial&lt;/strong&gt; folder. Next, a partial view macro needs to inherit from a custom view type, so the declaration at the top of your view will need to be:&lt;/p&gt;&lt;pre class="csharpcode"&gt;@inherits PartialViewMacro&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;Then, just like in v4, you’ll need to create a new macro in the &lt;em&gt;Developer&lt;/em&gt; section of the Umbraco back office. The &lt;em&gt;Macro Property&lt;/em&gt; interface looks very similar in v5:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/image_32.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="https://shazwazza.com/media/articulate-import/image_thumb_30.png" width="421" height="356"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You’ll notice its slightly simplified as compared to v4. The 2nd drop down list dynamically changes based on the &lt;em&gt;Macro Type&lt;/em&gt; drop down list value.&lt;/p&gt;
&lt;p&gt;Once you’ve saved the macro, that’s it, your Macro is now created!&lt;/p&gt;
&lt;h1&gt;Using a Partial View macro&lt;/h1&gt;
&lt;p&gt;The razor syntax for rendering a macro in v5 is:&lt;/p&gt;&lt;pre class="csharpcode"&gt;@Html.UmbracoMacro(&lt;span class="str"&gt;"blah"&lt;/span&gt;)&lt;/pre&gt;
&lt;p&gt;As noted before, a Partial View Macro inherits from the view type: &lt;strong&gt;PartialViewMacro . &lt;/strong&gt;This view object contains a &lt;em&gt;Model&lt;/em&gt; property of type: &lt;strong&gt;Umbraco.Cms.Model.PartialViewMacroModel &lt;/strong&gt;which has a couple of properties you can use to render data to the page:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; Content CurrentNode { get; }
&lt;span class="kwrd"&gt;public&lt;/span&gt; dynamic MacroParameters { get; }&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;The &lt;em&gt;CurrentNode&lt;/em&gt; property is very similar to v4’s currentPage parameter in Xslt. It will allow you to traverse the tree and render any content you like. Here’s &lt;em&gt;one&lt;/em&gt; way that you could list the child names from the current node:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Names of child nodes&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
@foreach (var child in Model.CurrentNode.ChildContent().AsDynamic())
{
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;p&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        Child node name: &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;b&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;@child.Name&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;b&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;p&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
}&lt;/pre&gt;
&lt;h1&gt;Dynamic parameters&lt;/h1&gt;
&lt;p&gt;As you can see above, there’s a &lt;strong&gt;MacroParameters&lt;/strong&gt; property on the &lt;strong&gt;PartialViewMacroModel&lt;/strong&gt; which is a dynamic property just like MVC’s ViewBag. Passing data into the MacroParameters property can be done in your Macro declaration like so:&lt;/p&gt;&lt;pre class="csharpcode"&gt;@Html.UmbracoMacro(&lt;span class="str"&gt;"blah"&lt;/span&gt;, &lt;span class="kwrd"&gt;new&lt;/span&gt; { IsCool = &lt;span class="str"&gt;true&lt;/span&gt;, MyParameterName = &lt;span class="kwrd"&gt;new&lt;/span&gt; MyObject() })&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;This means that you can pass any type of object that you’d like into your macro with any parameter name. Then to use the parameters that you’ve passed in, you can reference them directly by name inside your macro:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ul&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;@Model.MacroParameters.IsCool&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;@Model.MacroParameters.MyParameterName.ToString()&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ul&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;Macro parameters via the Macro Editor&lt;/h1&gt;
&lt;p&gt;A cool new feature in v5 is the new Macro parameter editor. It can now pre-populate all macro parameters found in Xslt, Partial Views and Surface controllers. Better still, is that the parameter editor has been completely rebuilt with Knockout JS so you can add/remove as many parameters as you like at once and then finally post your changes back when hitting save.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/image_33.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="https://shazwazza.com/media/articulate-import/image_thumb_31.png" width="475" height="275"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Oh, and another great feature in v5 is that the editors for macro parameters are actually just v5 &lt;a href="http://shazwazza.com/post/Umbraco-Jupiter-Plugins-Part-1.aspx" target="_blank"&gt;Property Editors&lt;/a&gt; that are attributed as Macro Parameter Editors. &lt;/p&gt;
&lt;h1&gt;Strongly typed parameters&lt;/h1&gt;
&lt;p&gt;You may be wondering how the Macro Editor populates parameters from a Partial View Macro since we’ve seen that Partial View Macro model only supports dynamic (non stongly typed) properties… The answer is in Razor. Razor views allow you to define public properties, methods, etc… inside of the Razor markup. So if we want to have strongly typed parameters for our Partial View Macros which can be set based on Macro parameters in the rich text editor, all we have to do is declare them like:&lt;/p&gt;&lt;pre class="csharpcode"&gt;@inherits PartialViewMacro

@functions {

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsCool { get; set; }
    
}&lt;/pre&gt;
&lt;p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
Now, the Partial View has a discoverable property which our Macro Editor will be able to see and automatically populate the parameter list. This property will also be set with any value that is selected in the Macro Parameter Editor when inserting one into the rich text box. Another thing to note is that if you are simply injecting parameter values using the &lt;strong&gt;Html.Macro &lt;/strong&gt;helper, then this property will be set so long as a dynamic property of the same name exists in the macro’s MacroParameter model properties.&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:08 Z</pubDate>
      <a10:updated>2023-03-23T15:08:08Z</a10:updated>
    </item>
  </channel>
</rss>