Help > Indexing > Indexing via Scripting > Scripting Reference > Templates

Templates

Scripting templates allow for powerful and flexible presentation of your data when indexing with the 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, 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 script.  The Perceptive Search script is responsible for "discovering" or enumerating all the data that you want to index.  When the indexer needs to index the text of your document, the 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 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
<html>  <body>    Hello, this is the <%= Request.Filename %> record.
  </body></html>

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>

  <linkrel="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><imgsrc="Northwind.jpg"align="absmiddle"><spanstyle="font-size: larger">Northwind traders</span></td>

    <tdalign="right"><spanstyle="font-size: larger">Invoice</span></td>

  </tr>

</table>

<hr noshade>

<table width="100%">

  <tr>

    <td>

      <istyle="font-size: smaller">

      , Twin Points WA  98156<br>

      Phone: 1-206-555-1417   Fax: 1-206-555-5938   

      </i>

    </td>

    <tdalign="right">

      Date: <%= GetValue(objDBOrder, "OrderDate") %>

    </td>

  </tr>

</table>

<hr noshade>

<table width="100%"border="0">

  <tr>

    <tdvalign="top">Ship To</td>

    <tdvalign="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>

    <tdvalign="top">Bill To</td>

    <tdvalign="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">

  <trclass="TitleRow">

    <tdclass="TitleColumn">Order Id</td>

    <tdclass="TitleColumn">Customer Id</td>

    <tdclass="TitleColumn">Sales Person</td>

    <tdclass="TitleColumn">Order Date</td>

    <tdclass="TitleColumn">Required Date</td>

    <tdclass="TitleColumn">Shipped Date</td>

    <tdclass="TitleColumn">Shipped Via</td>

  </tr>

  <tr>

    <tdalt="Order ID"><%= GetValue(objDBOrder, "OrderID") %></td>

    <tdalt="Customer ID"><%= GetValue(objDBOrder, "Orders.CustomerID") %></td>

    <tdalt="Sales Person"><%= GetValue(objDBOrder, "FirstName") %><%= GetValue(objDBOrder, "LastName") %></td>

    <tdalt="Order Date"><%= GetValue(objDBOrder, "OrderDate") %></td>

    <tdalt="Required Date"><%= GetValue(objDBOrder, "RequiredDate") %></td>

    <tdalt="Shipped Date"><%= GetValue(objDBOrder, "ShippedDate") %></td>

    <tdalt="Shipper"><%= GetValue(objDBOrder, "Shippers.CompanyName") %></td>

  </tr>

</table>

<br>

<table border="0"width="100%"cellspacing="0">

  <trclass="TitleRow">

    <tdclass="TitleColumn"width="10%">Product ID</td>

    <tdclass="TitleColumn"width="50%">Product Name</td>

    <tdclass="TitleColumn"align="right"width="10%">Quantity</td>

    <tdclass="TitleColumn"align="right"width="10%">Unit Price</td>

    <tdclass="TitleColumn"align="right"width="10%">Discount</td>

    <tdclass="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 %>

  <trclass="<% if index mod 2 = 0 then%>ItemRow<% else %>ItemRowAlt<% end if %>">

    <tdalt="Product ID"><%= GetValue(objDBOrderItems, "Order Details.ProductID") %></td>

    <tdalt="Product Name"><%= GetValue(objDBOrderItems, "ProductName") %></td>

    <tdalign="right"alt="Quantity"><%= GetValue(objDBOrderItems, "Quantity") %></td>

    <tdalign="right"alt="Unit Price">$<%= FormatNumber(GetValue(objDBOrderItems, "Order Details.UnitPrice"), 2) %></td>

    <tdalign="right"alt="Discount"><%= FormatNumber(GetValue(objDBOrderItems, "Discount") * 100, 0) %>%</td>

    <tdalign="right"alt="Price">$<%= FormatNumber(price) %></td>

  </tr>

  <% objDBOrderItems.MoveNext %>

  <% index = index + 1 %>

  <% loop %>

  <tr>

    <tdcolspan="5"align="right">Sub Total: </td>

    <tdalign="right">$<%= FormatNumber(total, 2) %></td>

  </tr>

  <tr>

    <tdcolspan="5"align="right">Freight: </td>

    <tdalign="right">$<%= FormatNumber(GetValue(objDBOrder, "Freight"), 2) %></td>

  </tr> 

  <tr>

    <tdcolspan="5"align="right">Total: </td>

    <tdalign="right">$<%= FormatNumber(total + GetValue(objDBOrder, "Freight"), 2) %></td>

  </tr> 

 

</table>

</body>

</html>