Better Templates for Everyone Tutorial

Hello World

The hello world template creates a simple html file from a single bte file. To compile helloworld.bte, use this command line:
java com.Ostermiller.bte.Compiler helloworld.bte
The command produces a file named helloworld.html in the same directory.

Hello World uses the very basics of the template system. The html document is contained in template tags which are contained in document tags.


Using Templates

This example shows how to use a bte document as a template for other bte documents. basicone.bte and basictwo.bte both need to be compiled.

HTML documents all start with the same basic structure. In addition you probably want all the html documents on your site to have the same look and feel. They should all have the same background color and have similar structure. You only want to have to enter this information once. You can accomplish this using templates.

basicone.bte and basictwo.bte contain only the text specific to their html file. basictpl.bte contains the elements that are common to both html files.

The "content" of basicone and basictwo is inserted into the appropriate place in basictpl to create the basicone and basictwo html files.


Suppressing Output

You will probably find it convenient to compile everything (*.bte). If you tried that in the last example you would find that basictpl.bte doesn't compile on its own and produces error messages. You can tell the compiler not to try compiling such a template on its own and not to try creating output for it by adding output=false.


Multiple Templates

Multiple templates can be included in one document. In this example the products name, product cost, and product description each have their own template. This example shows how easy it is to separate presentation from data using the BTE system.



One very nice feature of BTE is its ability to provide a default implementation of a template. Suppose we want to add shipping information to our products from the previous example and we want to sell a third product. Now suppose most of our products can be shipped but our third product is to heavy. We want to be able to have our template put in the default blurb about shipping unless there are special circumstances.


Error Handling

If you compiled the above example, you probably noticed that the compiler complained about using the default. Thats didn't hurt, but it would be nice to be able to turn off expected warnings. The way to do that is to add a group tag around the shipping and use the ondefault=ok option to disable the warning. Using group tags you can adjust all aspects of error handling for any part of your template. This example is almost the same as the previous one but the compile time warnings have been disabled.


Optional Sections

Normally if the compiler can't find what should be included, it will stop. Another feature of groups is that we can set the error handling for this case as well. When the compiler can't resolve something it may ignore the entire group and continue. This allows us to create optional sections.

Perhaps we want to add pictures of the products to our web site, but we don't have pictures for all the products. We can tell the bte compiler to skip the picture section if there is no picture available. In this example, we have added pictures to products one and three. Notice that we can use separate templates for the width and height attributes for the photo and set warnings if they are missing but still display the photo. productthree has a photo, width, and height. It compiles with no warnings. producttwo has no photo. It compiles with no warnings. productone has a photo with no width or height and it compiles with warnings. Notice that the template reuses the product name as the alt tag on the image.


Multiple Levels

Perhaps we are now ready to create a home page for our products. We want it to look similar to our product pages, but it shouldn't have product information on it. To do this we might want to point our product template at another higher level template and point our homepage template at the same higher level template.


Navigation Bars

Now we no enough about templates to implement an navigation bar that will be on each page.

Other File Types

So far we have seen that Better Templates for Everyone can be used to create html files. However, BTE can create any sort of file. The file extension can be specified in the doc tag. Here is an example that creates a hello world text file.

Branching: selecting and choosing using ones

There is a flow control mechanism in bte. The bte "one" tag will traverse each group it contains and use the first (and only the first) group that resolves.


Copyright (c) 2000-2004 by Stephen Ostermiller

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. (site index)