Introducing rndr – a package for macro testing and ajax calls

Rndr is a (Umbraco 4.5+) package that basically makes whatever macro callable from a url. The response will be the exact output from the macro.

The requesting url is constructed this way (in debug-mode):

http://www.mysite.com/rndr?macroalias=mymacro&pid=1100

And the result will be from mymacro (using a basic test macro), for example:

Hello world from page "My page"

So you send your macro alias and your page id with the query string, and you will get back the result of your macro. Adding the page id is necessary since macros can use the currentPage object. But you can define a default page id as we will see later.

Calling a macro that way makes it easy to visualize for example IronPython macros. You can test run your macro immediately after you saved it for the first time, no need of including it in a template or a page.

There’s no magic under the hood. The package only consists of a template that calls a macro which in turn renders the requested macro. The rendering macro takes care of the request string with the options that comes with it.

The package is avaliable from here http://our.umbraco.org/projects/website-utilities/rndr-url-macro-renderer (vote for it if you like it, thanks!)

Can we use this on a live site?

For a live site you would not like to open up for all macros to be callable. Also the url can be pretty long and unnecessary explicit. Therefore there’s a shortcut-option. For example:

http://www.mysite.com/rndr?sc=mm

or

http://www.mysite.com/rndr?sc=mm&pid=1100

Where sc is a shortcut for a specific macro – you define your own shortcuts to whatever macro you choose. That also makes it safer, since you only define shortcuts for the macros you like to be callable.

What’s the use of this again?

The practical uses are mainly macro testing and ajax calling. As for ajax calling, you can easily do a macro that will put out some information taken a parameter or two from the url (or from the macro parameters) and return a xml or whatever string for your javascript to parse:

function getDataFromMyMacro(id)
 {
    url = "http://www.mysite.com/rndr?sc=gg&id=" + id;
    jQuery.ajax({
    url: url,
    type: 'POST',
    success: function (data) {
     alert(data); },
    error: function (msg, url, line) {
     alert(msg); }
    } );

 }

Many people use built in Base functionality for this, but I find it very convenient to have the ajax calls to get data from Xslt or IronPython code as well.

Macro parameters?

Yes you can also add macro parameters if you like:

http://www.mysite.com/rndr?macroalias=mymacro&pageid=1100&x=magic&y=132187

or

http://www.mysite.com/rndr?sc=mm&x=magic&y=132187

They will be sent to your macro as they should.

How do I define what macros I like to use in this way?

If you run in debug mode you can call all macros with the standard request string (macroalias=sss&pageid=nnn). Otherwise you add shortcuts this way:

debugmode = False # set to True to allow all macros to be called

# define shortcuts:
shortcuts={}
shortcuts["mm"] = ["mymacro",1100] # including a default page id
shortcuts["dd"] = ["helloworld"]

The above setting would make those two requests valid:

http://www.mysite.com/rndr?sc=mm
http://www.mysite.com/rndr?sc=dd&pid=1100

Where do I set that options, and what’s that strange syntax?

The macro is a IronPython script, called RenderMacro, so after you’ve installed the package you find the code file in Scripting Files folder (Developer section in Umbraco UI).

Testing with included rndrtest macro

The package comes with a sample macro called rndrtest with an added shortcut “test”. It simply prints “hello from {page name}”. You can try it out with this call (where pid is a published page id):

http://www.mysite.com/rndr?sc=test&pid=1100
hello from mypage

Try it with a macro parameter + get a quick dive into python

To try the macro parameter option do this :

Open up the macro rndrtest from Macros, and add a parameter called myparam, type = text.

Open upp the script rndrtest.py from Scripting Files, change it so it looks like this:

print "hello from " + currentPage.Name
# check if parameter exists, and if so - print the value
if "myparam" in globals(): print myparam

Save, then test run it:

http://www.mysite.com/rndr?sc=test&pid=1100&myparam=testing-testing
hello from mypage
testing-testing

I like to use my own url, not rndr

Sure – you can call it whatever you like. Just change the name of the rndr-template.

I want to test my Ascx macros

Sure, enclose the macro call with <form id=”aspnet” runat=”server”> … </form> in the rndr-template.

About these ads

4 thoughts on “Introducing rndr – a package for macro testing and ajax calls

  1. Hi Jonas,

    Good work with Rndr!

    Curious, do you know about the “/umbraco/macroResultWrapper.aspx” page?

    TinyMCE uses it to get/render macros. The main issue is that it requires back-office authentication to work.

    – Lee

    • Hi Lee,
      thanks :)

      No, didnt know. Will have a look at it.

      Also note that this particular post is kinda outdated, it was written for the previous version of Rndr with Python as the rendering macro – the current Razor version has only got the basic functionality (yet). However it’s working fine (I just used it to test some uQuery + Razor :-).

      Cheers
      Jonas

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s