Using Umbraco and Razor Syntax to add attributes to body tag for CSS targetting | Prolific Notion, Certified Umbraco Developer


/ Published in: ASP
Save to your folder(s)

Using Umbraco and Razor Syntax to add attributes to body tag for CSS targetting | Prolific Notion, Certified Umbraco Developer


Copy this code and paste it in your HTML
  1. Using Umbraco and Razor Syntax to add attributes to body tag for CSS targetting
  2. On 22 February 2011, In Umbraco, by Simon Dingley
  3.  
  4. I never found a solution that I was happy with for adding unique id’s to a page body tag that didn’t feel like a hack. I often need to do it for very specific CSS targeting so I introduce to you my current solution using the new Razor syntax inside of a master template in Umbraco.
  5. ?
  6. 1
  7. 2
  8. 3
  9. 4
  10. 5
  11. <umbraco:macro runat="server" language="razor">
  12.  
  13. <body id="@Model.Id">
  14.  
  15. </umbraco:macro>
  16. UPDATED: The example above has now been updated to simplify it and require just a single piece of code inside of the inline-macro “@Model.Id” to output the current page id.
  17. Essentially all this is doing is appending the current node id to a string (in my case just the letter ‘p’) and adding it to the id attribute of the html body tag.
  18. I tend to also need to take this one step further and include the id of a parent page in case styling is applied to a section of the content tree, I do this by adding a class to the body tag which contains the section id. The solution for this that I have used successfully on a current project is as follows:
  19. ?
  20. 01
  21. 02
  22. 03
  23. 04
  24. 05
  25. 06
  26. 07
  27. 08
  28. 09
  29. 10
  30. 11
  31. 12
  32. 13
  33. 14
  34. 15
  35. 16
  36. 17
  37. 18
  38. 19
  39. 20
  40. 21
  41. <umbraco:macro runat="server" language="razor">
  42.  
  43. @{
  44. var node = Model;
  45. var sid = "s" + Model.Id;
  46. var level = 2;
  47.  
  48. while(node.Level > 1)
  49. {
  50. if (node.Level == level)
  51. {
  52. // Add any logic in here then create your class
  53. sid = "s" + node.Id;
  54. }
  55. node = node.Parent;
  56. }
  57. }
  58.  
  59. <body id="@Model.Id" class="@sid">
  60.  
  61. </umbraco:macro>
  62. As in the previous example I am appending the current page id to a string and adding it to the id attribute of the body tag. I am also using a while loop to walk up the content tree to a specific level in the content tree, which in this case is level 2 and I am getting the id of the page at the specified level and appending it to a string(which in this case is ‘s’) and adding it to the class attribute of the body tag.
  63. So what have I gained by doing this? I can now apply specifically targeted CSS styling to individual pages or to an entire section of the content tree.
  64. I would welcome any comments from those more familiar with Razor in Umbraco on the performance overhead of doing this on each page load since I am including this in my master page.
  65. * Credit goes to Jonas Eriksson for his tip on the Razor syntax for accessing parent nodes and to Aaron Powell for his introduction to “Using Razor in Umbraco 4” blog post.

URL: http://www.prolificnotion.co.uk/using-umbraco-and-razor-syntax-to-add-attributes-to-body-tag-for-css-targetting/#

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.