As I get wiser on Umbraco I want to share the information in this blog post. The tips are related to Umbraco CMS version 7,2+.
Settings
Set the default language for pages
A site has a default language which will be used for all pages/items created under the website. I installed a default package “Fanoe”, which was set to english – this is how I changed it to danish.
- Open Settings > Languages
- Click the
...
to the right of “Languages” and chooseCreate
- Follow the guide, choosing the language you want and end by pressing “Create”
You may then right-click on the top website node (Say: “Home”) and choose Culture > Language
to change the Language to your newly created language.
Razor
What is Html.RenderPartial?
If you inside a template see something like this:
@{ Html.RenderPartial("BottomNavigation"); }
You may wonder what does this do? The tag means:
Render the partial view called “BottomNavigation.cshtml” and insert the output here
You will find the partial view here: “Settings > Partial Views
”. In the file system you will find the razor template for the partial view here: “/Views > Partials
”.
Global helpers and functions
Sometimes you have functionality which you reuse across templates and even across solutions. You can work more DRY if you say store some utility global helpers on say GitHub and pull them in on each new solution you implement.
It is actually really easy – you simply create a file App_Code/GlobalHelpers.cshtml
:
@helper Icon(string type) { }
And then use it like this:
<span class=“text-info”>@GlobalHelpers.Icon(“map-marker”) Aarhus</span>
You may think that you will have to name them “GlobalHelpers.cshtml”, but actually you are free to name them as you like, and even put them in sub folders. The name will be converted to a namespace, so if I had a helper here: App_Code/Netsi/Util/a.cshtml
with a helper named convert.
You can then access the code in your templates like this:
@Netsi.Util.convert()
UmbracoHelpers
var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
Sequrity
bool isSomeoneLoggedIn = System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
var member = System.Web.Security.Membership.GetUser();
Examples
Task: Get all pages which have a carousel image on them in an unordered list
This example is based on pages which have a property “carouselImage”. If the user have added a carousel image, the page should appear in an unordered list. I will query the code and use “Where” Linq option to filter out pages which have no chosen carousel image:
var pages = Model.Content.Children.Where(x => x.IsVisible()) .Where(x => x.GetPropertyValue("carouselImage", "")!=""); // This one will not look in the full tree, so you might use this one instead: /* var pages = Umbraco.TypedContentAtRoot().SelectMany(x => x.DescendantsOrSelf()) . Where( x = > x . IsVisible( ) ) . Where( x = > x . GetPropertyValue( "carouselImage" , "" ) ! = "" ) ; */ if (pages.Count()>0) { foreach ( var page in pages) { var image = Umbraco.Media(page.GetPropertyValue("carouselImage")); } }
Links – related content
- Umbraco v6 MVC Razor Cheatsheets (please note that it is for version 6 of Umbraco!)
- DynamicNode documentation. (DynamicNode is the dynamic access to all the data stored in your Umbraco website)