Xplan Quick Xplain
More function examples:
additional functions cont.
‘Great things are done by a series of small things brought together’ Vincent Van Gogh.
We’ve had a few emails and conversations around the functions tutorial we explored and wrote about here. One of the main things that keeps coming up, is people want more examples.
That’s understandable, often working with others who aren’t programmers (as is the case for most of us working in the Xplan space) it can sometimes take just the right example or a couple of examples before it starts to stick and the logic behind a concept is understood and readily applied.
So here is another small example of simple function that can speed up coding your templates whilst making them more robust.
Dollars, Dollars, Dollars
Given our industry, it’s no surprise that dollar figures are heavily utilised, not only throughout many different documents of the client process, but often significantly in single documents such as: Fact Find, SOA, Review document, FDS etc.
We’ve already covered some expanded methods of the currency function here. But what if we took that currency knowledge and used it in our own function to:
- reduce the coding each time
- ensure consistency
In this example we’ve just named our function ‘Currency’, you will note the capitalisation, as Xmerge (python) is case sensitive. If it wasn’t we wouldn’t be able to use this as there’s already the native ‘currency’ function in Xplan.
You may wish to differentiate your function names more to avoid any confusion with Xmerges native functions, but for the purpose of this example we’re going to stick with ‘Currency’.
<:let Currency=lambda x: currency(floatify(x), 0, has_symbol=True, blank_zero=False):>
Let’s break it down (colour coded)
- Our function name is Currency
- x is just our standard parameter (argument). X is just waiting to be subbed in for an actual value. This could be another variable, it would be a field like $client.total_income.value.
- We’re utilising the standard Xmerge currency function here. You’ll note we’ve ‘floatified’ ‘x’ inside the currency function to ensure it handles anything thrown at it. If we just floated it we wouldn’t be able to deal with string based currency values.
- We’re also using the extended currency arguments (see here) to place a dollar sign for us automatically and ensure we don’t get blank values
To call it in the template:
or if no income amount
Remember: You need to declare functions before you call them, so make sure the let Currency declaration is in the Template before you call the function.
Like the other functions covered, this one example will contribute to saving you a bit of time with your templates (cumulatively the more you use them and add to them the bigger the impact). It also aids in making your templates more readable, particularly with formatted areas like within tables.
Hopefully these examples have given you a solid understanding of how simple, and repeated things, can be wrapped up in a function to improve templates and Xmerge coding.
Getting back to our Vincent Van Gogh quote at the start of the article – many small things build to great things. Small changes to the way you go about coding like this, all add up, build on and leads to everyone doing more and achieving more with their templates and code.