Copied to clipboard

Flag this post as spam?

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


  • Ian 23 posts 113 karma points
    Apr 04, 2015 @ 09:13
    Ian
    0

    Using Macros everywhere

    hi there

    our small team has just started out using Umbraco - looks great so far.

    One of the devs has used Umbraco before, the rest of us are noobs.

    The umbraco-experienced dev is saying that it is best practise to use macros and macro-partials everywhere in our templates, to somehow 'decouple the code', eg each of our template .cshtmls just has calls to macros in, rather than containing any html. He says they did it that way on a previous project.

    However, this is not my understanding of what macros are really for though, and we've having a bit of an argument over it.  He doesnt seem to be able to explain it beyond the above though.

    Itd be a great help if more experienced ppl can tell us whether it is good practise to use macros eveywhere, rather than coding in razor or even just straight mvc partials.

    tia

    Ian

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Apr 04, 2015 @ 09:41
    Jan Skovgaard
    0

    Hi Ian

    If your coworker has been working with older versions of Umbraco he's right that using macros is the way to go since it makes it possible to re-use code in different context. A macro can be placed directly in a template and can be used in a rich text editor for instance. That was before Umbraco started to use MVC but was based on webforms.

    However since Umbraco started to support MVC and is using MVC as default on v7 installations there are more ways of doing things as you describe above. If you need to make some re-usable code I'd say that you should consider using a partial view or a macro depending on how the code is going to be used. If it should be possible to pass some parameters that a content editor should be able to fill in then a partial view macro is the way to go. But if it's code the main navigation I guess you can just as easily place it in a partial view, which you then reference in the master template or you can choose to make it as a partial view macro.

    When coding Razor directly in a template I only think that makes sense if you know that the code should only be used in this particular template and that it should not be re-used in other templates. If it's something that should be re-used then go with partial views or a macro I'd say.

    I must admit I can't remember quite how caching works with partial views - But when using macros it's also possible to add caching on them.

    I don't think it's a matter of being right or wrong but doing what feels right in the context - I think that people are doing this in many different ways. But the above is just my humble take on things.

    /Jan

  • Martin 114 posts 313 karma points
    Apr 04, 2015 @ 10:43
    Martin
    1

    You could try caching by using @Html.CachedPartial("YourPartialView", Model.Content, 3600)

    3600 = seconds for caching

    Good luck
    //martin

  • Bjarne Fyrstenborg 1282 posts 3994 karma points MVP 8x c-trib
    Apr 04, 2015 @ 11:04
    Bjarne Fyrstenborg
    1

    Regarding the answer from @Martin, you can find the documentation here: https://our.umbraco.org/documentation/Reference/Mvc/partial-views

    If the output of your partial view is dynamic, e.g. if content change per request, you should be aware of the cached output will still be returned.

    /Bjarne

  • Ian 23 posts 113 karma points
    Apr 09, 2015 @ 14:59
    Ian
    0

    Many thanks guys

Please Sign in or register to post replies

Write your reply to:

Draft