2
votes

I'm using Umbraco 4.7 to develop a website. There are certain common elements across some pages like 'Contact Details', 'Latest News', 'Latest Products', etc. For example, one page can have contact details while other may have news. I want to create some sort of macros (HTML or Razor) and want to add these on the required pages. But I also want this content to be editable by content editor. How shall I create such editable macros?

1

1 Answers

3
votes

I will assume that you are familiar with Razor and creating macros. If not, there is a reasonable article here.

There are several ways to do this, but each depend on the type of content you wish to display. For example, a 'Latest news' macro can be created with only code by just specifying in the razor to select the top 3 news articles, ordered by date descending. Likewise, if the contact details data already exists on another page (e.g. About us or Contact us), then you can just use the Razor to locate the page and pull the field values you wish to display in your macro.

However, as soon as you want to have some control over which items are displayed in your macro, you will need to create some additional means of controlling this. In the case of your products, you will probably want to select which are displayed in the macro, and therefore across the site. Therefore, you can:

  • Add a yes/no field to each product (e.g. called 'Display in side bar') and this can then be used the macro to filter the products display; or
  • Create a new document type called 'Side bar products' that contains a uComponents multi-node picker that will allow you to select products. You can then create a node from this document type and in your macro you will use Razor to query the products in this node. (You will obviously have to ensure that this node is not displayed in the navigation of the site!)

This second option is by far the better option as it is easier to maintain, provides a little scalability and also allows you to potentially have multiple 'Side bar products' nodes, so you could have differing sets of products in different sections of the sites. The first option is certainly easier to implement, but can get difficult to maintain since you have to remember which product is ticked.