Using templates
You can use templates in your Asterisk configuration files (e.g. in sip.conf) to avoid repetitive sections. To appreciate their usefulness, consider the following code:
[bob]
type=friend
context=local
allow=ulaw,alaw
secret=verysecret1
host=dynamic
dtmfmode=rfc2833
[alice]
type=friend
context=local
allow=ulaw,alaw
secret=verysecret1
host=dynamic
dtmfmode=rfc2833
[jack]
type=friend
context=local
allow=ulaw,alaw
secret=verysecret1
host=dynamic
dtmfmode=rfc2833
[hellen]
type=friend
context=local
allow=ulaw,alaw
secret=verysecret1
host=dynamic
dtmfmode=rfc2833
In the code above, we’ve defined parameters for each peer individually, even though they all hold the same values. This is where templates come into play – we can define a template containing these parameters and then apply it to each peer.
To define a template, we need to place an exclamation mark in parentheses after the section name, like this:
[internal-phones](!)
Now we can define the parameters that the template will contain:
[internal-phones](!)
type=friend
context=local
allow=ulaw,alaw
secret=verysecret1
host=dynamic
dtmfmode=rfc2833
To apply a template, we simply type the template name in parentheses after the device name, e.g.:
[hellen](internal-phones)
Now, our code will be much shorter, but will not lose any of the functionality:
[internal-phones](!)
type=friend
context=local
allow=ulaw,alaw
secret=verysecret1
host=dynamic
dtmfmode=rfc2833
[bob](internal-phones)
[alice](internal-phones)
[jack](internal-phones)
[hellen](internal-phones)