Yes this can be done again. What do you want the class name to be? The name of the page/node, the name of the template that the page uses, or something else?
This is for V4 of Umbraco, in V5 which RC2 was released yesterday. You can write Razor directly in the template without the need for the <umbraco:macro> tags which will make it easier/cleaner to read.
OK simply remove the value from the ID if you don't need or replace it with another value you would want to use. Not sure why the value of .Template is not ouputting anything so another semantic value you could use for the class is as follows:
Using the NodeTypeAlias is the node/page's document type alias that you created for the page in the settings section of the umbraco back office and the snippet on the end is obviously just converting the value to lowercase and replace the spaces in that value for hyphens.
A few good Razor resources that the Umbraco community has done:
Ahh the formatting on my snippet got screwed slightly. You need to add a space inbetween the first "" in .replace().
So .replace(" ","-") which says find any spaces in my string and replace them with the hyphen as the first parameter was just "" it threw an error as it was being told not to look for anything.
Set body class
I have one master page and many content pages.
How would I set the body class of the master page based on the content page loaded?
e.g.
body class="homepage"
body class="inner-landing-page"
Yes this can be done again. What do you want the class name to be?
The name of the page/node, the name of the template that the page uses, or something else?
Cheers,
Warren
The template alias would be fine.
Well not to repeat what has been said for here is a blog post from Simon in the community,
http://www.prolificnotion.co.uk/using-umbraco-and-razor-syntax-to-add-attributes-to-body-tag-for-css-targetting/
This is for V4 of Umbraco, in V5 which RC2 was released yesterday. You can write Razor directly in the template without the need for the
<umbraco:macro> tags which will make it easier/cleaner to read.
So this ouputs the unique node ID of the page on the ID of the body tag and we set the class attribute to be the template alias
Warren :)
I think I may have tried that, the result it:
I really name the template name, the id isn't great semantically.
OK simply remove the value from the ID if you don't need or replace it with another value you would want to use.
Not sure why the value of .Template is not ouputting anything so another semantic value you could use for the class is as follows:
Using the NodeTypeAlias is the node/page's document type alias that you created for the page in the settings section of the umbraco back office and the snippet on the end is obviously just converting the value to lowercase and replace the spaces in that value for hyphens.
A few good Razor resources that the Umbraco community has done:
http://our.umbraco.org/projects/starter-kits/razor-estate-agents-starter-site
http://our.umbraco.org/projects/developer-tools/razor-dynamicnode-cheat-sheet
http://our.umbraco.org/projects/developer-tools/razor-dynamicnode-cheat-sheet
Cheers,
Warren
Thanks again, however:
Error loading Razor ScriptString cannot be of zero length. Parameter name: oldValue
Whereas:
Returns the Alias which has spaces stripped out anyway I believe?
That does the trick anyway, so thanks again!!
Correction - added ToLower()
Ahh the formatting on my snippet got screwed slightly. You need to add a space inbetween the first "" in .replace().
So .replace(" ","-") which says find any spaces in my string and replace them with the hyphen as the first parameter was just "" it threw an error as it was being told not to look for anything.
Warren
is working on a reply...