USD Nested Replacement Parameters


During a recent project customising Unified Service Desk, I had the need to nest one replacement parameter inside another.

Here's a simplified example of what I wanted to achieve:
[[ [[entity.PointerToAHostedControl]].name]]

  • entity.PointerToAHostedControl = MyHostedControl
  • = MyNameResult

In other words, I wanted to dynamically find the name of a hosted control and then get the name of the entity that was currently loaded on that hosted control.

The Problem

The problem is that it doesn't work! It just gets replaced to something like this:]]

This is because USD is 'swallowing up' all of the opening square brackets and treating them as a single 'start of replacement parameter' marker.

The Solution

After a lot of experimentation and some poking around the USD code with ILSpy, I discovered that you can use a javascript $Expression to solve the problem.
When USD processes replacement parameters, it works through the replacement parameter string from left to right, looking for 'start' markers [[. Luckily:

  1. Before it even starts looking for start markers, it processes all of the $Expression sections. If you place your inner replacement parameter within a $Expression, then it gets processed first.
  2. You can put normal replacement parameters inside a $Expression without actually writing any javascript. You just need to put them within quotes.

So starting with this (N.B. it is important to get your casing correct and quote marks in the right place):

Firstly, USD processes the $Expression, giving this:

Then it is processes the string like a normal replacement parameter to give the result we are looking for:


In replacement parameters, $Expression handlers are processed before everything else. You can take advantage of this to allow you to nest replacement parameters.
Maybe there might be other uses too that I haven't thought of yet. If only I'd got round to enabling comments on my blog, you could let me know if you think of anything...
Post image Untitled copyright Louise Leclerc (license)