Quantcast
Channel: www.netsi.dk » Umbraco
Viewing all articles
Browse latest Browse all 5

Umbraco tips

$
0
0

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.

  1. Open Settings > Languages
  2. Click the ... to the right of “Languages” and choose Create
  3. 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 =&gt; x.IsVisible())
			.Where(x =&gt; 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 =&gt; x.DescendantsOrSelf())
			. Where( x  = &gt;  x . IsVisible( ) ) 
			 . Where( x  = &gt;  x . GetPropertyValue( "carouselImage" ,   "" ) ! = "" ) ;
*/
if (pages.Count()&gt;0) {
	
	foreach ( var page in pages)
	{
		var image = Umbraco.Media(page.GetPropertyValue("carouselImage"));
		
	}
	
}

 

Links – related content


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images