SUPPORT FOR THE .NET DLR

One of the most versatile aspects of Umbraco is its support for additional languages through the .NET Dynamic Language Runtime (DLR) text. Out of the box, you can create macros that are based on IronPython and IronRuby, simply by using the backoffice interface. This is a great alternative to XSLT and .NET driven macros for those who prefer working with a different language.

This section simply provides you with syntactical examples of how to render the content using these alternate languages. The author suggests the following online resources for further reading about the DLR languages mentioned in the following sections.

  • http://ironruby.codeplex.com/: This is an IronRuby open source project.
  • http://ironpython.codeplex.com/: This is an IronPython open source project.

For those of you who prefer working with IronPython or IronRuby, the examples in Listings 5-7, 5-8, and 5-9 should provide you with a good introduction on how to tap into the Umbraco content using one of these languages.

IronPython

Here are some examples of what you can do using IronPython as your language of choice in your Umbraco macros. The options are virtually endless, just like when you work with .NET.

Listing Pages from Current Page

In Listing 5-7, you can see how few lines of code it takes to generate an unordered list of pages using IronPython.

LISTING 5-7: PagesFromCurentNode.py

from umbraco.presentation.nodeFactory import Node
from umbraco import library

#list all of the subpages from the ‘currentPage’

result = “<ul>”

for childNode in currentPage.Children:
  result += “<li><a href=‘” + library.NiceUrl(childNode.Id) +  “’>” +
childNode.Name + “</a></li>”

result += “</ul>”

print result

Subpages from Changeable Source

As discussed earlier, DLR driven macros can also take parameters. Listing 5-8 shows an example of this.

LISTING 5-8: SubpagesFromSource.py

from umbraco.presentation.nodeFactory import Node
from umbraco import library

#set the node id you would like to fetch pages from here
#you can also set it as a macro property with the alias ‘nodeId’ instead

rootNodeId = int(nodeId)

result = “<ul>”

for childNode in Node(rootNodeId).Children:
  result += “<li><a href=‘” + library.NiceUrl(childNode.Id) +  “’>” +
childNode.Name + “</a></li>”

result += “</ul>”

print result

Notice the extreme similarities between the two code listings. The simple difference is that in Listing 5-8 you are taking the nodeId as an argument through a macro parameter, which makes the macro much more flexible and reusable for other potential sections of the site.

IronRuby

To illustrate its versatility, here is an example of what the Ruby solution looks like as well.

LISTING 5-9: ChangeSourceRuby.rb

result = “<ul>”;

currentPage.Children.each do |this_item|
  result += “<li><a href=‘” + this_item.NiceUrl + “’>”
            + this_item.Name + “</a></li>”
end

result += “</ul>”

puts result

As with previous examples, these are just the tip of the iceberg so to speak. The point was to show you that if you prefer any of these languages, Umbraco isn't about to stand in your way.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset