Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jeroen Oostwouder 104 posts 300 karma points
    Feb 09, 2016 @ 10:38
    Jeroen Oostwouder
    0

    Best practice for re-usable blocks

    Hi, I'm having some trouble desciding what's the best way to do the following, and I'm hoping someone could be of any assistence.

    I have a App_Code\Helpers.cshtml that contains (ofcourse) some helper-functions.

    For instance, a function that renders a list of blocks for a given list of content-items.

    I came a cross of limitations.

    • I can't use things such as GetPropertyValue() on an IPublishedContent
    • I don't have access to @Html to use the .Raw() or .RenderPartial() functions.
    • I need to create an instance of the @UmbracoHelper in every function.
    • Because this file is compiled on runtime, a single change makes the first page-visit really slow when developing. Even the first page-visit within the backend takes quite some time after an edit.

    Just to name a few.

    I try to use PartialMacro's when possible, but I think the possible parameter for a PartialMacro are limited. I can't create a PartialMacro that accepts a List<DynamicPublishedContent> for instance. As far as I know.

    What would be the best way to use these functions? Any suggestions?

  • Daniel 60 posts 174 karma points
    Feb 09, 2016 @ 18:05
    Daniel
    0

    You should be able to send the list to a Partial like:

    @Html.Partial("...pathtoview.cshtml", new List<IPublishedContent>())
    

    if you then in your Partial start with:

    @model List<IPublishedContent>
    

    Then you should be able to do something like:

    @if(Model.Any())
    {
        <ul>
            @foreach(var content in Model)
            {
                <li>@content.Name</li>
            }
        </ul>
    }
    

    That's one way to do it, there are almost unlimited options though!

  • Jeroen Oostwouder 104 posts 300 karma points
    Feb 10, 2016 @ 08:01
    Jeroen Oostwouder
    0

    But that is just a single list. How about adding more parameters? Is that also possible this way?

  • Daniel 60 posts 174 karma points
    Feb 10, 2016 @ 08:21
    Daniel
    100

    Of course, that becomes very clean!

    Model:

    public class MyPartialViewModel {
        public IEnumerable<String> MyStrings { get; set; } 
        public String MyEmail { get; set; } 
        public Int32 MyLargeNumber { get; set; }
    }
    

    In controller:

    ...
    model.MyPartialViewModel = new MyPartialViewModel {
        MyEmail = "[email protected]",
        MyLargeNumber = 12345678,
        MyStrings = "words and more words".Split(' ')
    };
    

    View:

    @Html.Partial("...pathtoview.cshtml", Model.MyPartialViewModel)
    

    Partial View:

    @model MyPartialViewModel
    
    <strong>My email is:</strong>
    <p>@Model.MyEmail</p>
    
    <strong>My large number is:</strong>
    <p>@Model.MyLargeNumber</p>
    
    <strong>My strings are:</strong>
    @foreach(var str in Model.MyStrings)
    {
        <p>@str</p>
    }
    
  • Jeroen Oostwouder 104 posts 300 karma points
    Feb 10, 2016 @ 08:40
    Jeroen Oostwouder
    1

    Ow, that is nice.

    I still need to use the whole MVC process more in umbraco I guess. I would place the "controller"-code above in my view at this point. :)

    Thank you, this clears up a lot.

  • Carl Bussema 38 posts 140 karma points
    Feb 09, 2016 @ 18:28
    Carl Bussema
    0

    Partial Views seem good. Pretty much the perfect use case for "Given a type of input, render some HTML."

    You can also write your own extension methods in a class file: one the one hand, it's a pain to write HTML in a class file but on the other hand, it's compiled so it's fast!

    public static class MyHelpers {
     public static IHtmlString RenderList(this HtmlHelper html, IEnumerable<IPublishedContent> contentList) {
        ... //use TagBuilder or StringBuilder to build a string based on the input
      }
    }
    
  • Jeroen Oostwouder 104 posts 300 karma points
    Feb 10, 2016 @ 07:36
    Jeroen Oostwouder
    0

    Hm, I've somehow allways seen partial-files as a part of a view. Not as a method of re-using a block of code.

    I will have a look at that. Thank you!

Please Sign in or register to post replies

Write your reply to:

Draft