Best practice question - layout templates vs macros
Hi - I have a "special" page in my umbraco 7 solution, of whom there will only be one instance of.
I am currently building the special layout directly into the corresponding layout page. However, the page content is driven by the child-nodes to the page, so the layout page will have to iterate over these children to produce the page content.
Would I be better of putting this iteration logic into a macro?
I am especially concerned about performance. Would the page be able to benefit from Umbraco caching when the logic for generating the contents is placed directly in the layout file?
That is a very good question! I think generally when using MVC templates the best performance comes from using simple logic directly in the Views and Partial Views rather than Macros. This is because the Macro engine itself has a performance overhead compared to native ASP.NET MVC. Macros only really need to be used when you want to allow editors to insert them in the back office (e.g. via RTE). There is a blog post discussing this further here
But as you have rightly mentioned, what about caching, well Partial Views can be cached, you can find documentation about how to do this here.
So I would generally only use Views and a cached Partial Views for the best out of the box performance and only use Partial View Macros when I really need to.
There is a lot more you can do with caching and Umbraco and there a is fabulous video of a session from CodeGarden 2014 that goes through all of the options here.
Best practice question - layout templates vs macros
Hi - I have a "special" page in my umbraco 7 solution, of whom there will only be one instance of.
I am currently building the special layout directly into the corresponding layout page. However, the page content is driven by the child-nodes to the page, so the layout page will have to iterate over these children to produce the page content.
Would I be better of putting this iteration logic into a macro?
I am especially concerned about performance. Would the page be able to benefit from Umbraco caching when the logic for generating the contents is placed directly in the layout file?
Kind regards
Thomas
Hi Thomas,
That is a very good question! I think generally when using MVC templates the best performance comes from using simple logic directly in the Views and Partial Views rather than Macros. This is because the Macro engine itself has a performance overhead compared to native ASP.NET MVC. Macros only really need to be used when you want to allow editors to insert them in the back office (e.g. via RTE). There is a blog post discussing this further here
But as you have rightly mentioned, what about caching, well Partial Views can be cached, you can find documentation about how to do this here.
So I would generally only use Views and a cached Partial Views for the best out of the box performance and only use Partial View Macros when I really need to.
There is a lot more you can do with caching and Umbraco and there a is fabulous video of a session from CodeGarden 2014 that goes through all of the options here.
Hope that's all helpful for you.
Jeavon
Hi Jeavon - many thanks for your swift and thorough reply. :-)
Thomas
You're very welcome, I hope all the links are useful, the video from CodeGarden is worth a watch :-)
is working on a reply...