Created
December 12, 2012 12:06
-
-
Save jhauge/4267282 to your computer and use it in GitHub Desktop.
Making current page properties available for javascript on all pages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" AutoEventWireup="true" | |
CodeBehind="Master.master.cs" Inherits="Web.masterpages.Master" %> | |
<!-- | |
This is the "root" master page that all other masterpages | |
in the website is based upon. | |
--> | |
<asp:Content runat="server" ContentPlaceHolderID="ContentPlaceHolderDefault"><!doctype html> | |
<html class="no-js" lang="en"> | |
<head> | |
</head> | |
<body> | |
<!-- | |
Script to load a variable telling about the current language | |
Insert before other scripts | |
--> | |
<script> | |
var UI = UI || {}; // Namespacing | |
UI.currentUmbNode = { | |
'pageId': '<%= CurrentNode.Id %>', | |
'name': '<%= CurrentNode.Name', | |
'nodeType': '<%= CurrentNode.NodeTypeAlias %>' | |
// Add more properties here if you need them | |
}; | |
// If this Script block is included before other scriptblocks | |
// in the site, you will be able to access the current page | |
// properties using code like this: | |
UI.currentUmbNode.pageId; | |
</script> | |
</body> | |
</html> | |
</asp:Content> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web.UI; | |
using umbraco.NodeFactory; | |
namespace Web.masterpages | |
{ | |
public partial class Master : MasterPage | |
{ | |
public Master() | |
{ | |
Init += PageInit; | |
} | |
public string CurrentNode { get; set; } | |
private void PageInit(object sender, EventArgs e) | |
{ | |
// Make the current umbraco node available in the frontend .net code | |
CurrentNode = Node.GetCurrent(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment