Using the STS Template Engine to generate Asterisk configuration files
The STS Template Engine, developed by Sunrise Telephone Systems is a universal template engine for Cocoa featuring its own macro language for conditional template expansion. Although it can be used for any kind of template expansion requirement, it was developed for the purpose of generating Asterisk configuration files from templates.
The STS Template Engine is released under the GPL version 2 as Objective-C source code for embedding into Cocoa or GNUstep applications.
Overview
Methods provided
- stringByExpandingTemplate:usingDictionary:errorsReturned:
- stringByExpandingTemplate:withStartTag:andEndTag:usingDictionary:errorsReturned:
- stringByExpandingTemplateAtPath:usingDictionary:encoding:errorsReturned:
- stringByExpandingTemplateAtPath:withStartTag:andEndTag:usingDictionary:encoding:errorsReturned:
Macros supported
%IF, %IFNOT, %IFEQ, %IFNEQ, %IFDEF, %IFNDEF, %ELSIF, %ELSIFNOT, %ELSIFEQ, %ELSIFNEQ, %ELSIFDEF, %ELSIFNDEF, %ELSE, %ENDIF, %DEFINE, %UNDEF, %LOG, %ECHO and %DEBUG.
Predefined Placeholder Variables
_timestamp, _uniqueID, _hostname, _userCountryCode, _userLanguage, _systemCountryCode, _systemLanguage
Example template
%% created 06-JUL-2005 by Sunrise Telephone Systems Ltd.
%% version 1.00
%%
%% The generated configlet must be included in /etc/asterisk/sip.conf
%%
%%
; THIS FILE HAS BEEN AUTOGENERATED % «_timestamp »
; DO NOT EDIT THIS FILE – MODIFICATIONS MAY BE OVERRIDDEN
;
;
[xlite] ; SIP profile for local X-Lite on % «_hostname »
port=5061
host=127.0.0.1
type=friend
qualify=no
reinvite=no
canreinvite=no
disallow=all
%IF allowUlaw
allow=ulaw
%ENDIF
%IF allowAlaw
allow=alaw
%ENDIF
%IF allowGSM
allow=gsm
%ENDIF
%IF allowILBC
allow=ilbc
%ENDIF
context=autocontext
callerid=”% «username »“<% «callerid »>
;
%% END OF TEMPLATE
Example code
– (IBAction)createConfiglet:(id)sender {
NSString *configlet;
NSDictionary *userInput;
NSArray *errorLog = [[NSArray alloc] init];
// initialise the dicitionary with the user input
userInput = [NSDictionary dictionaryWithObjectsAndKeys:
[usernameField stringValue], @”username”,
[cidnumberField stringValue], @”callerid”,
[ringtimerField stringValue], @”ringtimer”,
[ulawCheckbox stringValue], @”allowUlaw”,
[alawCheckbox stringValue], @”allowAlaw”,
[gsmCheckbox stringValue], @”allowGSM”,
[ilbcCheckbox stringValue], @”allowILBC”,
nil];
// expand the template to create the configlet
configlet = [NSString stringByExpandingTemplateAtPath:
@”/Library/Application Support/Asterisk/templates/xlite.template”
usingDictionary:userInput
encoding:NSUTF8StringEncoding
errorsReturned:&errorLog];
// etc