Instructor: Sometimes you may need
to make your templates more flexible.
For example, suppose that some of your Apache servers
are configured to use HTTPS and some of them use only HTTP,
but the rest of their settings are basically the same.
You could have two different Apache templates,
but it would be cleaner and more maintainable
to have one template that you pass an HTTPS parameter to.
One of the great things about EPP templates
is that you can pass parameters into them,
which is not something you can do with ERB templates.
The ERB approach to using external variables
is much messier and less reliable.
To try out using a parameter, let's modify our motd template
so that you can pass a parameter saying whether or not
you want to include Apache's location information.
That is its server route and document route.
Let's go back into the template.
If you have any parameters in the template
then you need to add them to the beginning of the template.
You can't put any content before your parameters.
First we start with the non-printing tag.
Then we put one or more parameters inside pipe characters.
After the opening pipe, you need to put
the data type of the first parameter.
Here are the core data types in Puppet.
There are other data types,
but these are the ones you will use most often.
As you can see, Puppet includes the typical data types
that you would find in other languages.
The data type we need for our parameter is Boolean
because it will be either true or false
that we want to print the Apache directory locations.
So we type Boolean and then the name of the parameter.
Let's call it print_paths.
Then you can put in a default value if you want.
That's usually a good idea, so you don't force people
to set a parameter when using the template.
To set a default you just put the equal sign
and the default value, which we'll set to true.
Finally you need to put in the closing pipe
and the closing tag, which must have a dash
so that it doesn't print a new line.
Now we need to use the parameter.
We'll just put in an if statement between the two lines
about Apache directory locations.
Of course you could leave out the equals equals true part
and it would still work.
Then we just need to end the if statement
with a closing brace and non-printing tags
after the two lines about Apache locations.
We're all done so let's try it.
It didn't do anything because
when the print paths parameter is true,
the template does exactly what it did before.
So we need to set the parameter to false to see if it works.
To pass a parameter we add a comma
after the template name argument, then a curvy brace,
then the name of the parameter in quotes
and without the dollar sign.
Then a hash rocket, then the value
we want the parameter to be, then a closing brace.
That's it, so let's try it again.
It worked.
It didn't print the two lines about Apache directories,
but it still printed the other lines.
Before we leave this, let's be responsible programmers
and document the parameter.
Comment tags in EPP are probably what you would expect.
They start with less than, percent,
and then have a pound sign.
I'll skip ahead after typing the comment.
Now we end with a closing tag that starts with a dash,
so it doesn't print a new line.
You might be wondering if this is a violation of the rule
that parameters need to come first in the template.
Well comment tags are the one exception to the rule
and if you don't end them with a new line stripping tag,
then you'll get a syntax error.
So some EPP purists even put comments after the parameters,
but I think that makes it less readable,
so I put them first.
And that's it for this lesson.