NodeBox

Create visual output with Python programming code
Home Download Reference Tutorial Library Gallery About

Templating

Templating is a design strategy in which you re-use a certain graphic style, or a piece of your script. For example, instead of taking each separate heading in a text and assigning it the color red, you define a header style once (one that looks red). This style is then re-used over and over on each heading. This way, when you change your mind about the color of headings (they should be grey), you only need to modify the header style, and not each separate header.

Templating is not exactly a technical issue, but an organisatorial solution. Keep your project organised by separating the data from the composition, and by separating the composition from the style.

Take a look at the example below: here I've made a definition for my own custom flower() command, an element consisting of ten red-pinkish ovals connected by lines. At the bottom of the script I can then place multiple flowers on the screen. When I change the flower definition, each of these flowers would reflect the changes.

 

 

Defining your own commands

Aside from the commands in the NodeBox reference, you can define your own custom commands. One of these could be header() for example. Defining your own commands is like composing your own toolbox, with a set of tools that you can apply to your creations over and over.

def header(txt, x, y):
fill(1, 0, 0)
font("Dolly-Bold", 18)
text(txt, x, y)

The definition of a command starts with def, followed by the name of the command (pick any relevant name you like), and a list of parameters. Don't forget the colon at the end of the line - this tells NodeBox that the intented statements below are what the command does.

Parameters

Command definitions are not executed by themselves: you are instead teaching NodeBox something, for example, what a header is. Command definitions need to be called from somewhere else in the script. This is where parameters come in: notice how the definition of header() uses a txt, a x, and a y parameter. Theses are used like variables inside the definition. Once the command is called, you supply real values for txt, x and y.

header("Templating", 20, 40)

NodeBox would now execute all of the code in the header() definition, with txt being "Templating", x being 20, and y being 40.

Return value

You may have noticed that some commands in NodeBox have a return value that you can store in a variable. For example, textheight(txt, w) returns the height of a paragraph displaying txt with a width of w.

def paragraph(txt, x, y, w):
fill(0.2)
font("Dolly-Roman", 14)
text(txt, x, y, w)
return textheight(txt, w)

The return statement inside a definition returns a value. This can be any number of things, a number, a string, a list. The return statement always comes last inside the definition.

Example

In the example below, we put two identical scripts next to each other, one using plain top-to-bottom scripting and one using templates. The first script is in fact less code, but imagine it would draw not two but twenty paragraphs... then you would have to define fonts and colors over and over again, and probably end up feeling like a robot trying to explain something to another robot.

Moreover, changing the header color or the horizontal margin is much easier in the second script because we have to do it only once: by changing the code in the header() definition or by changing the value of the x variable.

 



The script with plain top-to-bottom scripting. Notice its repetitiveness.


The same script with templates.
fill(1, 0, 0.25)
font("Dolly-Bold", 14)
txt = "Without templating"
text(txt, 20, 30, 200)

fill(0.2)
font("Dolly-Roman", 14)
txt = "Without templating, ..."
text(txt, 20, 50, 200)

fill(1, 0, 0.25)
font("Dolly-Bold", 14)
txt = "Messy"
text(txt, 20, 120, 200)

fill(0.2)
font("Dolly-Roman", 14)
txt = "This makes your script...."
text(txt, 20, 140, 200)
def header(txt, x, y):
fill(1, 0, 0.25)
font("Dolly-Bold", 14)
text(txt, x, y, outline=True)
return textheight(txt)

def paragraph(txt, x, y, w):
fill(0.2)
font("Dolly-Roman", 14)
text(txt, x, y, w, outline=True)
return textheight(txt, w)

x = 20
y = 30
w = 200

y += header("With templating", x, y)
txt = "With templating, ..."
y += paragraph(txt, x, y, w) + 10

y += header("Clean", x, y)
txt = "This keeps your script..."
paragraph(txt, x, y, w)