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.
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.
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
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
You could try caching by using
@Html.CachedPartial("YourPartialView", Model.Content, 3600)
3600 = seconds for caching
Good luck
//martin
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
Many thanks guys
is working on a reply...