{"id":8384,"date":"2020-07-09T16:29:14","date_gmt":"2020-07-09T16:29:14","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8384"},"modified":"2020-07-09T17:43:04","modified_gmt":"2020-07-09T17:43:04","slug":"creating-blazor-fragments","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/creating-blazor-fragments\/","title":{"rendered":"Creating Blazor fragments"},"content":{"rendered":"<p>At some point you may need to dynamically create elements of mark-up in a Blazor application. For example, you might create a reusable class library for returning components, icons or the likes depending upon the user. <\/p>\n<p>Ofcourse we can do this from the <em>@code<\/em> block within a page or we might prefer to simply return mark-up from a function.<\/p>\n<p>Let&#8217;s look at a simple example method which, depending upon the <em>id<\/em> sent to it returns a different icon, here&#8217;s an abridge snippet of the code<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic static RenderFragment GetIconForId(int id)\r\n{\r\n   var iconType = typeof(ListIcon);\r\n   switch (id)\r\n   {\r\n      case 0:\r\n         iconType = typeof(HomeIcon);\r\n         break;\r\n      case 1:\r\n         iconType = typeof(NotificationImportantIcon);\r\n         break;\r\n      \/\/ etc.\r\n   }\r\n   \r\n   return builder =&gt;\r\n   {\r\n      builder.OpenComponent(0, iconType);\r\n      builder.CloseComponent();\r\n   };\r\n}\r\n<\/pre>\n<p>As you can see we return a <em>RenderFragment<\/em> which is actually just a delegate which takes a <em>RenderTreeBuilder<\/em> argument. From this <em>builder<\/em> we create\/append a child component, the first value is a sequence (a position of the instructions in the source code) followed by the type of the child component to add.<\/p>\n<p>If the case above, the <em>HomeIcon<\/em> is an Icon from <em>Skclusive.Material<\/em>, which is ultimately an <em>Microsoft.AspNetCore.Components.IComponent<\/em>. <\/p>\n<p>So anything that takes a <em>RenderFragment<\/em> will now display the icon that&#8217;s returned via the method in our example.<\/p>\n<p>We can also use <em>AddAttribute<\/em> to our component, see <em><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/microsoft.aspnetcore.components.rendering.rendertreebuilder?view=aspnetcore-3.1\" rel=\"noopener noreferrer\" target=\"_blank\">RenderTreeBuilder<\/a><\/em> for more information.<\/p>\n<p>Basically it&#8217;s not dissimilar (as you&#8217;ve expect) to create XML documents using the XmlDocument classes etc.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At some point you may need to dynamically create elements of mark-up in a Blazor application. For example, you might create a reusable class library for returning components, icons or the likes depending upon the user. Ofcourse we can do this from the @code block within a page or we might prefer to simply return [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[305],"tags":[],"class_list":["post-8384","post","type-post","status-publish","format-standard","hentry","category-blazor"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8384","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8384"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8384\/revisions"}],"predecessor-version":[{"id":8387,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8384\/revisions\/8387"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}