What's the difference between a template and a script?


What is the template and a script since the syntax for both looks very similar? A template is like a full HTML page that includes VFP expressions and blocks of self-contained code. A script can contain much more flexible structures with pure HTML inserted between structural components. A template is evaluated from top to bottom by looking at each expression in the document and evaluating each and embedding hte content into the HTTP stream. A script on the other hand is converted into a full program that executes top to bottom. Scripts have the ability to easily switch between HTML and code in the same document including between structured statements while templates must accomplish their task within the individual expressions and tasks. In a template every expression or code block is an island of code, where a script acts as a comprehensive program. A really clear example of the difference involves outputting a VFP cursor to an HTML table. In a template, any block of structured code must be self-contained. Thus:
<TABLE>
<%  SCAN
     Response.Write( '<TR><TD>' + Field_1 + '</TD><TD>' + Field_2 + '</TD></TR>' )
    ENDSCAN
%>
</TABLE>
Notice how, once in the code, you have to manually produce the HTML via Response.Write. Now look at the script version:
<TABLE>
<%  SCAN %>
     <TR>
       <TD><%=Field_1%></TD>
       <TD><%=Field_2%></TD>
     </TR>
<%  ENDSCAN %>
</TABLE>
This is much more like true HTML. In fact you could easily imagine laying out the table with a visual editor, and then just inserting the SCAN loop. You can do anything with a script that you can do with a template, and much more. However, non-compiled scripts tend to run a lot slower than templates because they are created and then executed on the fly through Fox interpreter. For better performance of scripts you can compile scripts into native VFP code, but at that point you loose the ability to edit files without recompiling. The syntax for templates and scripts is similar, so it's easy to switch between the two both from the calling end as well as from the script end. For most applications I write I use templates with occasional scripts if the logic requires complex conditional or looping structures. Your preferences may vary.


Last Updated: 02/25/00