iRule Preprocessor
Problem this snippet solves:
The engine works by converting a template (which is a string, or a file) into a TCL script, and then running it. Nearly each line of text encountered will be returned as is. The exception is text between =<% ... %>= which is treated as Tcl code (the eval happens in a safe interp, see Safe Interps). This makes it very flexible and it leaves you freedom of doing what you want, dealing with any data and putting how much code you want in the template.
Also for convenience there is a
<%= ... %>
for spooling output (the proc _defaultSpool takes care of sending the text to the right place; you can change that: see Preprocessor Commands).How to use this snippet:
Template Syntax
Delimiter/Command | Name | Description |
---|---|---|
| Code | the beginning of a template code block |
| Output | the beginning of a template code block that after it is evaluated by the tcl interpreter will hand the results to a function defined by printCommand option |
| Preprocessor Code | the beginning of an options block used to set template options such as printCommand, logCommand |
| Default Logging Code | the beginning of a logging code block with the log threshold set to 0 |
| Level Select Logging Code | the beginning of a logging code block with the log threshold set to [0-9] |
| Default Logging Output | the beginning of a logging code block with the log threshold set to 0 which after it is evaluated by the tcl interpreter will hand the results to a function defined by logCommand option |
| Level Select Logging Output | the beginning of a logging code block with the log threshold set to [0-9] which after it is evaluated by the tcl interpreter will hand the results to a function defined by logCommand option |
| Close Code/Output Block | ends the current block of template code for all types of template code blocks |
Template Commands
Command | Arguments | Options | Returns | Explanation |
---|---|---|---|---|
|
| none | templateHandle | Reads the file referenced by the file object and returns a template handle |
|
| none | none | set the to which will be used when is run |
|
| none | none | sets the global log level to a value for the template specified by which will be used when is run |
|
| none | none | globally disables template logging statements |
|
| none | templateResult | renders out the template specified by with all the information provided by the template and the other various methods mentioned above |
Simple example
iRule Code
when HTTP_REQUEST { set uri [HTTP::uri] if {0} { log local0. "You should never see this" } elseif {[regexp {*.jpg$} $uri]} { pool /Common/test1 } elseif {[regexp {^test*} $uri]} { pool /Common/test2 log local0. "Sending connection from: [IP::remote_addr] to pool: /Common/test1" } else { log local0. "No match Found" } }
Template Code
1. when HTTP_REQUEST { 2. set uri [HTTP::uri] 3. 4. 5. 6. 7. if {0} { 8. log local0. "You should never see this" 9. <% foreach row $rows { %> 10. <% foreach {regex pool logEnabled} $row break %> 11. } elseif {[regexp {<%=$regex%>} $uri]} { 12. pool <%=$pool%> 13. <% if { $logEnabled {{}} "Yes" } { %> 14. log local0. "Sending connection from: [IP::remote_addr] to pool: <%=$pool%>" 15. <% } } %> 16. } else { 17. log local0. "No match Found" 18. } 19. }
Line by Line
Line | Explaination | Result |
---|---|---|
1 | a literal line | put =when HTTP_REQUEST {= into |
2 | a literal line | put =set uri [HTTP::uri]= into |
3 | a log line with a log level threshold equal to 1 | if logLevel >= 1; then put =log local0. "[HTTP::uri]"= into |
4 | a log line with a log level threshold equal to 2 | if logLevel >= 2; then put =log local0. "Client IP: [IP::client_addr]"= into |
5 | a log line with a log level threshold equal to 4 | if logLevel >= 4; then put =log local0. "Client Port: [TCP::remote_port]"= into |
6 | a log line with a log level threshold equal to 3 | if logLevel >= 3; then put =log local0. "Local IP: [IP::local_addr]"= into |
7 | a literal line | put =if {0} {= into |
8 | a literal line | put =log local0. "You should never see this"= into |
9 | template Code | evaluate =foreach row $rows {= |
10 | template Code | evaluate =foreach {regex pool logEnabled} $row break= |
11 | literal with inline template Output code | put =} elseif {[regexp {[puts $regex]} elseif {[regexp {= where $regex is evaluated within the template scope into |
12 | literal with inline template Output code | put =pool [put $pool]= where $pool is evaluated within the template scope into |
13 | template Code | evaluate =if { $logEnabled
|
14 | literal with inline template Output code | put =log local0. "Sending connection from: [IP::remote_addr] to pool: [puts $pool]"= where $pool is evaluated within the template scope into |
15 | template Code | evaluate =} }= |
16 | literal line | put =} else {= into |
17 | literal line | put =log local0. "No match Found"= into |
18 | literal line | put } into |
19 | literal line | put } into |
A few Crucial Notes
the
content
variable is the variable where the rendered result is built up and what is eventually returned to the user as the output of {{ipp::render
Preprocessor Configuration
proc _newLog {args} { global content set txt "#[join $args]" append content $txt }
Versions
ipp.1.2.tcl.zip it works MORE perfectly (as of this writing)
Updated Jun 06, 2023
Version 2.0Michael_Earnhar
Historic F5 Account
Joined October 30, 2008
No CommentsBe the first to comment