Help > Indexes > Indexing Rules > Scripting Rules > Templates

Templates

Perceptive Enterprise Search scripting templates allow for powerful and flexible presentation of your data when indexing with the Perceptive Enterprise Search engine.  It is both simple to use and flexible by leveraging the ActiveX framework on Windows.  Scripting templates give you a flexible way of generating HTML from dynamic content.

Designed to use ASP style scripting, Perceptive Enterprise Search scripting templates are compiler free, which means you can create them using your favorite text or HTML editor.

Your scripting template is coupled with a corresponding Perceptive Enterprise Search script.  The Perceptive Enterprise Search script is responsible for "discovering" or enumerating all the data that you want to index.  When Perceptive Enterprise Search needs to index the text of your document, the Perceptive Enterprise Search script "hands over" to your template which is executed and the resulting HTML is indexed.

Simple Example

This is a very simple example that demonstrates the Perceptive Enterprise Search script and template relationship.  This example will enumerate 100 documents and pass control to the template for rendering.

ISYSSCRIPT.XML
Const Total = 100

Dim Index

Function ScanFirst(Request)
	' Initialize the scanning variables
	Index = 1
	
	ScanFirst = ScanNext(Request)
End Function

Function ScanNext(Request)
	' Check if we are still scanning
	ScanNext = Index <= Total

	' Return the item and increment the count
	If ScanNext Then
		Request.Filename = Index
		Index = Index + 1
	End If
End Function

Sub RequestDocument(Request, Response)
	' Pass control to simple.template
	Response.Template "Simple.template"
End Function
Simple.template
  Hello, this is the <%= Request.Filename  %> record.

Database Example

The following example is a script that indexes the Northwind database that ships with Microsoft Office. The Northwind is a sample invoicing system that includes a master-detail relationship between orders and order items.  This script generates a dynamic HTML view of each order.

This script is available from the scripting rule wizard.

ISYSSCRIPT.XML
' Database connection string
const strConnectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Office\OFFICE11\SAMPLES\Northwind.mdb;Persist Security Info=False"

' Query to execute to produce enumeration
const strEnumQuery = "SELECT * FROM ORDERS "
  
' Variable to use during the database scan
dim objDBRecordset

function ScanFirst(Response)
  set objDBRecordset = Server.CreateObject("ADODB.RecordSet")
  ' Open the enumeration recordset
  objDBRecordset.Open strEnumQuery, strConnectionStr
  ' Pass to ScanNext to extract details
  ScanFirst = ScanNext(Response)
end function

function ScanNext(Response)
  ScanNext = NOT objDBRecordset.EOF

  if ScanNext then
    ' Use the primary key to identify the record
    Response.Filename = objDBRecordset.Fields("OrderId").Value

    ' Move to the next record
    objDBRecordset.MoveNext
  end if
end function

function RequestDocument(Request, Response)
  Response.Template "orders.template"
end function

Orders.template
<html>
  <link rel="stylesheet" type="text/css" href="styles.css">
<body>

<% 
  const strConnectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Office\OFFICE11\SAMPLES\Northwind.mdb;Persist Security Info=False"

  function ExecRecordset(Query, Params)
    dim objDBCommand: set objDBCommand = Server.CreateObject("ADODB.Command")
    objDBCommand.ActiveConnection = strConnectionStr
    objDBCommand.CommandText = Query
    set ExecRecordset = objDBCommand.Execute (, Params)
  end function
  
  function GetValue(RecordSet, Name)
    on error resume next
    GetValue = RecordSet(Name).Value
    if GetValue = Null then
      GetValue = ""
    end if
    if err.number <> 0 then
      GetValue = "<font color=""red"">" & Name & "</font>"
    end if
    on error goto 0
  end function
  
  function CalcPrice(Record)
    CalcPrice = Record("Quantity") * Record("Order Details.UnitPrice")
    CalcPrice = CalcPrice - (CalcPrice * Record("Discount"))
  end function

  dim objDBOrder, objDBOrderItems
  
  ' Open the order recordset
  set objDBOrder = ExecRecordset( _
    "SELECT Shippers.*, Customers.*, Orders.*, Employees.* " & _
    "FROM (((Employees RIGHT OUTER JOIN " & _
    "Orders ON Employees.EmployeeID = Orders.EmployeeID) LEFT OUTER JOIN " & _
    "Shippers ON Orders.ShipVia = Shippers.ShipperID) LEFT OUTER JOIN " & _
    "Customers ON Orders.CustomerID = Customers.CustomerID) " & _
    "WHERE Orders.OrderID = ? ", Array(Request.Filename))
  set objDBOrderItems = ExecRecordset(_
    "SELECT [Order Details].*, Suppliers.*, Categories.*, Products.* " & _
    "FROM ((([Order Details] LEFT OUTER JOIN " & _
    "Products ON [Order Details].ProductID = Products.ProductID) LEFT OUTER JOIN " & _
    "Suppliers ON Products.SupplierID = Suppliers.SupplierID) LEFT OUTER JOIN " & _
    "Categories ON Products.CategoryID = Categories.CategoryID) " & _
    "WHERE [Order Details].OrderID = ? ", Array(Request.Filename))  
 %>
<table width="100%">
  <tr>
    <td><img src="Northwind.jpg" align="absmiddle"> <span style="font-size: larger">Northwind traders</span></td>
    <td align="right"><span style="font-size: larger">Invoice</span></td>
  </tr>
</table>
<hr noshade>
<table width="100%">
  <tr>
    <td>
      <i style="font-size: smaller">
      One Portals Way, Twin Points WA  98156<br>
      Phone: 1-206-555-1417   Fax: 1-206-555-5938    
      </i>
    </td>
    <td align="right">
      Date: <%= GetValue(objDBOrder, "OrderDate")  %>
    </td>
  </tr>
</table>
<hr noshade>

<table width="100%" border="0">
  <tr>
    <td valign="top">Ship To</td>
    <td valign="top" alt="Ship To">
      <%= GetValue(objDBOrder, "ShipName")  %><br>
      <%= GetValue(objDBOrder, "ShipAddress")  %><br>
      <%= GetValue(objDBOrder, "ShipCity")  %>
      <%= GetValue(objDBOrder, "ShipRegion")  %>
      <%= GetValue(objDBOrder, "ShipPostalCode")  %><br>
      <%= GetValue(objDBOrder, "ShipCountry")  %><br>      
    </td>
    <td valign="top">Bill To</td>
    <td valign="top" alt="Bill To">
      <%= GetValue(objDBOrder, "Customers.CompanyName")  %><br>
      <%= GetValue(objDBOrder, "ContactName")  %><br>
      <%= GetValue(objDBOrder, "Customers.Address")  %><br>
      <%= GetValue(objDBOrder, "Customers.City")  %>
      <%= GetValue(objDBOrder, "Customers.Region")  %>
      <%= GetValue(objDBOrder, "Customers.PostalCode")  %><br>
      <%= GetValue(objDBOrder, "Customers.Country")  %><br>
    </td>
  </tr>
</table>
<br>

<table border="0" width="100%" cellspacing="0">
  <tr class="TitleRow">
    <td class="TitleColumn">Order Id</td>
    <td class="TitleColumn">Customer Id</td>
    <td class="TitleColumn">Sales Person</td>
    <td class="TitleColumn">Order Date</td>
    <td class="TitleColumn">Required Date</td>
    <td class="TitleColumn">Shipped Date</td>
    <td class="TitleColumn">Shipped Via</td>
  </tr>
  <tr>
    <td alt="Order ID"><%= GetValue(objDBOrder, "OrderID")  %></td>
    <td alt="Customer ID"><%= GetValue(objDBOrder, "Orders.CustomerID")  %></td>
    <td alt="Sales Person"><%= GetValue(objDBOrder, "FirstName")  %> <%= GetValue(objDBOrder, "LastName")  %></td>
    <td alt="Order Date"><%= GetValue(objDBOrder, "OrderDate")  %></td>
    <td alt="Required Date"><%= GetValue(objDBOrder, "RequiredDate")  %></td>
    <td alt="Shipped Date"><%= GetValue(objDBOrder, "ShippedDate")  %></td>
    <td alt="Shipper"><%= GetValue(objDBOrder, "Shippers.CompanyName")  %></td>
  </tr>
</table>
<br>
<table border="0" width="100%" cellspacing="0">
  <tr class="TitleRow">
    <td class="TitleColumn" width="10%">Product ID</td>
    <td class="TitleColumn" width="50%">Product Name</td>
    <td class="TitleColumn" align="right" width="10%">Quantity</td>
    <td class="TitleColumn" align="right" width="10%">Unit Price</td>
    <td class="TitleColumn" align="right" width="10%">Discount</td>
    <td class="TitleColumn" align="right" width="10%">Price</td>
  </tr>
  
  <% dim total, index: total = 0: index = 0  %>
  <% do while Not objDBOrderItems.Eof  %>
  <% dim price: price = CalcPrice(objDBOrderItems)  %>
  <% total = total + price  %>
  <tr class="<% if index mod 2 = 0 then%>ItemRow<% else %>ItemRowAlt<% end if  %>" >
    <td alt="Product ID"><%= GetValue(objDBOrderItems, "Order Details.ProductID")  %></td>
    <td alt="Product Name"><%= GetValue(objDBOrderItems, "ProductName")  %></td>
    <td align="right" alt="Quantity"><%= GetValue(objDBOrderItems, "Quantity")  %></td>
    <td align="right" alt="Unit Price">$<%= FormatNumber(GetValue(objDBOrderItems, "Order Details.UnitPrice"), 2)  %></td>
    <td align="right" alt="Discount"><%= FormatNumber(GetValue(objDBOrderItems, "Discount") * 100, 0)  %>%</td>
    <td align="right" alt="Price">$<%= FormatNumber(price)  %></td>
  </tr>
  <% objDBOrderItems.MoveNext  %>
  <% index = index + 1  %>
  <% loop  %>
  <tr>
    <td colspan="5" align="right">Sub Total: </td>
    <td align="right">$<%= FormatNumber(total, 2)  %></td>
  </tr>
  <tr>
    <td colspan="5" align="right">Freight: </td>
    <td align="right">$<%= FormatNumber(GetValue(objDBOrder, "Freight"), 2)  %></td>
  </tr>  
  <tr>
    <td colspan="5" align="right">Total: </td>
    <td align="right">$<%= FormatNumber(total + GetValue(objDBOrder, "Freight"), 2)  %></td>
  </tr>  
  
</table>

</body>
</html>