Neil's Place

July 21, 2005

6:34 PM How Templates Work XXVIII - Parent Conditions

Sometimes you want to simply generate one block of content at the top level and different content at the recurisive level. For example, the bookmarks toolbar in Firefox displays buttons at the first level, but menus and submenus for content below that. The entire bookmarks toolbar is generated by a XUL template.

Templates have a means of allowing a rule to match only if the generated content would be inserted inside an element with a particular tag name. For instance, if the container was a <vbox>, a rule could be created that would only match a <vbox> element. This is useful for recursive templates, since the inner iterations may use different content. It's most useful to distinguish between the outer and inner levels during template generation. For the bookmarks toolbar, the outer content is inserted into an <hbox>, but at lower levels, the content will be inserted into a <menu>

In case you aren't clear, the tag that must match for the outer iteration is the root element, the one with the datasources attribute on it. For inner iterations, it will be the element with the uri attribute from the previous iteration.

To do this kind of matching for the simple template syntax, you place a parent attribute on the rule, set to the tag to match. For instance, we might use the following:

<vbox datasources="template-guide-streets.rdf"
            ref="http://www.xulplanet.com/rdf/myneighbourhood">
  <template>
    <rule parent="vbox">
      <groupbox uri="rdf:*">
        <caption label="rdf:http://purl.org/dc/elements/1.1/title"/>
      </groupbox>
    </rule>
    <rule>
      <label uri="rdf:*" value="rdf:http://www.xulplanet.com/rdf/address"/>
    </rule>
  </template>
</vbox>

On the first pass, the container where generated content would be inserted is a <vbox>, so the first rule will match and a captioned <groupbox> will be created. On the next pass, the parent container will be the element with the uri attribute from the previous pass, in this case, the <groupbox> The first rule will not match in this case, but the second rule will match and a label will be created. The result can be seen in you try the example.

A tag test can also be used with the extended syntax, although the syntax for using it is different. Instead of placing a parent attribute on the <rule>, you place a tag attribute on the <content> tag in the conditions. For instance, the equivalent tag test using the extended syntax for the previous example is the following:

<content uri="?start" tag="vbox">

This example generates the same output content as when using the simple template syntax.

As we've seen in the past few examples, there are many different ways of structuring the two rules to match differently at different levels. General triple tests, tests on an RDF type, container tests and parent tag tests all provide a wide variety of ways to match in very specific ways. Of course, in the simple examples we've been using, the advantages of one kind of condition test over another are not obvious. In more complex examples however, you will see the benefit of one test over others depending on the structure of the data and the UI that you wish to create. By combining the different types of conditions together, more complex interfaces can be created just with templates.

Comments ( 0 )

July 18, 2005

8:18 PM XUL Tutorial Now Part of the Mozilla Developer Documentation

In case you haven't noticed, the XUL tutorial is now available as part of the Mozilla Developer Documentation site. An advantage of this is that errors can be corrected quickly and by anyone who spots them. The user notes created by users haven't been transfered though. I'm not sure if that's a good thing or not.

The user notes include a number of useful tips that people have written relating to a particular page. Currently the user notes aren't posted until I actually read them. This is a good thing since half of the notes are either questions (which I usually just ignore), or are misleading or incorrect. On one hand, I don't want to discourage someone by not accepting a comment they've made, but on the other hand, I feel that if a comment is misleading or uses, for example, some code that is a bad way of doing something, I don't want to post it, since the point of the user notes is to improve the documentation.

Anyway, I think there are plans on posting some other XULPlanet stuff, and I also plan on posting the template guide I've been writing on here. The Aaron countdown timer seems to have disappeared recently perhaps indicating his return a few days early so maybe the App tutorial or Prefbar will get updated also.

In other news, I've been hacking a bit at templates using XML data. Here's the first ever image of a XUL template fed with only XML input. It doesn't look very exciting but that's because the excitment is in disguise. Vlad's just reviewed the main part of the code for this, so we could very well see this stuff early in the 1.9 timeframe.

Comments ( 3 )


7:52 PM How Templates Work XXVII - Container Tests

The simple rule syntax supports two special conditional tests that are commonly used with multiple rules. The first of these tests can be used to test if an element is a container or not. To use this test, place an iscontainer attribute on a <rule>. The iscontainer attribute should be set to true if you only want to match containers, and false if you only want to match non-containers. A container is an RDF container such as a Seq.

The iscontainer attribute makes it easier to handle recursive content since you can have one rule for all containers and another rule for all non-containers. You don't need to match by type or some other predicate. This allows you to recurse down to larger levels without needing additional rules. It is commonly used with menus, and we can rewrite the previous example using the iscontainer attribute instead.

<button label="Houses in my Neighbourhood" type="menu"
        datasources="template-guide-streets.rdf"
        ref="http://www.xulplanet.com/rdf/myneighbourhood">
  <template>
    <rule iscontainer="true">
      <menupopup>
        <menu uri="rdf:*" label="rdf:http://purl.org/dc/elements/1.1/title"/>
      </menupopup>
    </rule>
    <rule>
      <menupopup>
        <menuitem uri="rdf:*" label="rdf:http://www.xulplanet.com/rdf/address"/>
      </menupopup>
    </rule>
  </template>
</button>

The only difference in the code in this example is that the order of the rules has been switched around, the condition check for house has been removed and the iscontainer attribute has been added. Since the iscontainer attribute is set to true, the rule will match as long as the member value or child of the starting node is an RDF container. We could also have left the rules in the original order and set the iscontainer on the first rule to false. The only thing we need to make sure is that the rules are in the proper order, so that the right data will be matched by the right rule. Remember, the more specific rules should go before less specific rules.

Note that leaving out the iscontainer attribute is not the same as setting it to either true or false. If you don't use the iscontainer attribute, the rule will match regardless of whether the node is a container or not.

The iscontainer attribute will also match containers appropriately if you have used the containment attribute in the template to change the predicates that indicate containership. If the node has one of the predicates listed in the containment attribute pointing out of it, it will also be considered to be a container. For instance, we might add the following to the previous example:

<button label="Houses in my Neighbourhood" type="menu"
        datasources="template-guide-streets.rdf"
        containment="http://www.xulplanet.com/rdf/address"
        ref="http://www.xulplanet.com/rdf/myneighbourhood">

The houses do have a value for the "http://www.xulplanet.com/rdf/address" predicate, so they will also be considered to be containers as well, resulting in another level of menus. Of course, we will need to update the predicates and labels to retrieve the right data. But this example demonstrates that something different is indeed happening.

The second special condition attribute tests for empty containers. This invloves setting the isempty attribute on a rule to either true or false. Setting it to true will match all empty containers, that is, containers with no children. Setting it to false will match all containers that have at least one child. Leaving out the isempty attribute will match anything. This condition test is commonly used to display the generated content differently for empty and non-empty containers.

You will commonly use the two attributes iscontainer and isempty together in different combinations to create the effect you need. Typically, this will mean one rule for a container with children, a second rule for empty containers, and a third rule for non-containers. Considering the case of bookmarks, the first two rules would match folders, while the third rule would match bookmarks. Naturally, the emptiness test does not apply to nodes that are not containers.

Note that both the iscontainer and isempty attributes are only available for rules that use the simple syntax.

Comments ( 0 )

July 16, 2005

7:29 PM How Templates Work XXVI - Generating a Recursive Menu

When creating recursive menus, you will need to use multiple rules, since leaf items will need to be created differently than non-leaf items. Leaf items will need to use a <menuitem> element whereas non-leaf items will need to use a <menu> element. This will involve at least two rules, although you might use other rules if you had other differences to handle.

<button label="Houses in my Neighbourhood" type="menu"
        datasources="template-guide-streets.rdf"
        ref="http://www.xulplanet.com/rdf/myneighbourhood"
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <template>
    <rule rdf:type="http://www.xulplanet.com/rdf/House">
      <menupopup>
        <menuitem uri="rdf:*" label="rdf:http://www.xulplanet.com/rdf/address"/>
      </menupopup>
    </rule>
    <rule>
      <menupopup>
        <menu uri="rdf:*" label="rdf:http://purl.org/dc/elements/1.1/title"/>
      </menupopup>
    </rule>
  </template>
</button>

The first rule matches all houses, while the second rule is used for streets. The content generated for each rule differs in only two ways. First, the menu tag is different (menuitem versus menu), and the label is taken from a different RDF predicate. In the first pass, the second rule matches the streets, so a <menupopup> and <menu> element are created. The uri attribute is on the <menu> element since we don't want to repeat the popup for every result. After the first pass, the content will be equivalent to the following (ignoring the template related content):

<button label="Houses in my Neighbourhood" type="menu">
  <menupopup>
    <menu uri="http://www.xulplanet.com/rdf/marion" label="Marion Street"/>
    <menu uri="http://www.xulplanet.com/rdf/garden" label="Garden Avenue"/>
  </menupopup>
</button>

The inner pass through the data handles the houses. The houses match the first rule so a <menupopup> and <menuitem> element are generated and inserted inside the street content (the <menu> element). Again, the popup is only created once since the uri attribute is on the <menuitem> element. The effect is a menu with a submenu. There's nothing special about the way menus are handled -- the builder follows the same method for any type of content. However, the nature of menus can make this tricky to follow. Here is the result of the above example after both levels have been handled.

<button label="Houses in my Neighbourhood" type="menu">
  <menupopup>
    <menu uri="http://www.xulplanet.com/rdf/marion" label="Marion Street">
      <menupopup>
        <menuitem uri="http://www.xulplanet.com/rdf/garden/16" label="16"/>
        <menuitem uri="http://www.xulplanet.com/rdf/garden/18" label="18"/>
      </menupopup>
    </menu>
    <menu uri="http://www.xulplanet.com/rdf/garden" label="Garden Avenue">
      <menupopup>
        <menuitem uri="http://www.xulplanet.com/rdf/garden/25" label="25"/>
        <menuitem uri="http://www.xulplanet.com/rdf/garden/37" label="37"/>
      </menupopup>
    </menu>
  </menupopup>
</button>

Comments ( 0 )

July 15, 2005

11:51 PM How Templates Work XXV - Multiple Rules with Recursion

You may recall that templates generate content recursively. After the data is generated, each result is used as the new reference point for a nested iteration of the template. This is usually used to generate content in a tree or menu. The inner iteration uses the same rules as the outer iteration. However, it is quite possible that you would like child or leaf nodes to appear differently than the parent nodes. Multiple rules are useful in this situation. In this case, one rule would be used to match the outer data and another rule would be used to match the inner data. The builder will apply all rules in both cases, however, if the rules are created correctly, there will only be matches for the rules that you want.

For instance, we might have a datasource which represents the houses in a neighbourhood. The top node contains several children, one for each street. Each street also contains children, one for each house. Naturally, you would want the streets to be displayed in a different manner to the houses. The recursive nature of templates can be used for this example. The outer pass will start at the top node and generate the content for each street. The next pass will use a street as the starting point and generate the content for each house. We could go further and generate data for each room in each house by adding more rules.

Here is an example which shows some sample neighbourhood data.

<hbox datasources="template-guide-streets.rdf"
      ref="http://www.xulplanet.com/rdf/myneighbourhood"
      xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <template>
    <rule rdf:type="http://www.xulplanet.com/rdf/House">
      <vbox uri="rdf:*" class="box-padded">
        <label value="Address: rdf:http://www.xulplanet.com/rdf/address"/>
        <label value="Floors: rdf:http://www.xulplanet.com/rdf/floors"/>
      </vbox>
    </rule>
    <rule>
      <groupbox uri="rdf:*" class="box-padded">
        <caption label="rdf:http://purl.org/dc/elements/1.1/title"/>
      </groupbox>
    </rule>
  </template>
</hbox>

The first rule matches only those items that have an RDF type of "http://www.xulplanet.com/rdf/House". The second rule doesn't have any condition filter so will match any result. The starting point indicated by the ref attribute is "http://www.xulplanet.com/rdf/myneighbourhood". In the RDF data, this is an RDF Bag with two children. Since the simple rule syntax is used in both rules, the builder will iterate over the children to generate results. At this pass, both of the children of "http://www.xulplanet.com/rdf/myneighbourhood" are streets and not houses so neither child will match the first rule. However, both children will match the second rule. Thus, two matches will be created using the second rule. The second rule creates a <groupbox> with a <caption>. If you look at image of the example, you will note that two groupboxes have been created.

The builder then recurses, using the previous result as the new starting point. For the first street, this new starting point will be "http://www.xulplanet.com/rdf/marion". The builder reapplies the rules starting from this new location in the RDF graph. The new node is an RDF Seq with children so the simple rules can generate some results. However, these results are houses, so the first rule will match. The second rule, since it has no conditions, will also match, but since the first rule takes priority, these rules would never apply. The effect is that the content for the first rule would be used for each house. This content is inserted inside the outer content generated for the street. This means that the <vbox> and the two labels will be placed inside the <groupbox> generated from the previous pass.

We could be more specific and specify a type in the datasource for the streets as well. This wouldn't affect the output in this example, but it may be more optimal in more complex templates to be as specific as possible when creating conditions. If there were other types of buildings on a particular street, we might add an additional rule for this. For instance, we might add another rule after the first:

<rule rdf:type="http://www.xulplanet.com/rdf/Store">
  <vbox uri="rdf:*" class="box-padded">
    <label value="Address: rdf:http://www.xulplanet.com/rdf/address"/>
    <label value="Sells: rdf:http://www.xulplanet.com/rdf/sells"/>
  </vbox>
</rule>

This rule is similar to the first rule, however is matches only those items that have an RDF type of "http://www.xulplanet.com/rdf/Store".

Comments ( 0 )

July 11, 2005

10:50 PM How Templates Work XXIV - Another Multiple Rule Example

The most common use of multiple rules is to apply different action bodies to different results. Often this will be because a particular result has a property that others do not. For instance, in an earlier example, one of the photos had a description and the other photos did not. In this case, you might wish to display the photo with a description in a different manner. This is useful if you wish to hide any content that would be needed to display the description.

The next example shows how we can do this.

<vbox id="photosList" align="start" datasources="template-guide-photos5.rdf"
      ref="http://www.xulplanet.com/rdf/myphotos">
  <template>
    <rule>
      <conditions>
        <content uri="?start"/>
        <member container="?start" child="?photo"/>
        <triple subject="?photo"
                predicate="http://purl.org/dc/elements/1.1/title"
                object="?title"/>
        <triple subject="?photo"
                predicate="http://purl.org/dc/elements/1.1/description"
                object="?description"/>
        <triple subject="?photo"
                predicate="http://purl.org/dc/elements/1.1/date"
                object="?date"/>
      </conditions>
      <action>
        <hbox uri="?photo" class="box-padded">
          <vbox>
            <label value="?title"/>
            <image src="?photo"/>
          </vbox>
          <groupbox>
            <caption label="Photo Details"/>
            <label value="?description"/>
            <label value="Date: ?date"/>
          </groupbox>
        </hbox>
      </action>
    </rule>
    <rule>
      <conditions>
        <content uri="?start"/>
        <member container="?start" child="?photo"/>
        <triple subject="?photo"
                predicate="http://purl.org/dc/elements/1.1/title"
                object="?phototitle"/>
      </conditions>
      <action>
        <vbox uri="?photo" class="box-padded">
          <label value="?phototitle"/>
          <image src="?photo"/>
        </vbox>
      </action>
    </rule>
  </template>
</vbox>

In this example, the first rule matches only those photos with title, description, and date properties. The second rule matches those photos with a title. If a photo did not have a title, it would not match any rule. In the example data, only the first photo has been given all the properties to match the first rule. The second photo has a title and date but no description, while the third photo has only a title. The first photo will match both rules whereas the other two photos will match only the second rule. The resulting data will be:

(?start = http://www.xulplanet.com/rdf/myphotos,
 ?photo = http://www.xulplanet.com/ndeakin/images/t/palace.jpg,
 ?title = Palace from Above
 ?description = View from the top of the tower looking east of the Doges Palace,
 ?date = 2005-04-30T14:55:00+01.00)
(?start = http://www.xulplanet.com/rdf/myphotos,
 ?photo = http://www.xulplanet.com/ndeakin/images/t/palace.jpg,
 ?phototitle = Palace from Above)
(?start = http://www.xulplanet.com/rdf/myphotos,
 ?photo = http://www.xulplanet.com/ndeakin/images/t/canal.jpg,
 ?phototitle = Canal)
(?start = http://www.xulplanet.com/rdf/myphotos,
 ?photo = http://www.xulplanet.com/ndeakin/images/t/obelisk.jpg,
 ?phototitle = Obelisk)

The first result matches the first rule and contains variables for the two additional predicates that were examined in the conditions. Since the second rule doesn't refer to these variables, they will not be filled in. Although the canal photo has a date, the second rule doesn't use it, so you cannot refer to the ?date variable in this rule. But, you could use a <binding> to get the date without requiring it to match the conditions. Of course, the variable doesn't have to be ?date in the second rule.

You might notice that the ?title variable is used in the first rule whereas the ?phototitle variable is used for the second rule, despite that they both store the value of the title predicate. There is no reason for this -- it is only used in this example to show that there is no connection between the variables used in the two rules. You can use different variables if it makes sense in the context of the template or the data, although in this example it would seem more reasonable to use the same variable name.

The only exception to this is that the container and member variables (those that are referred to in the uri attributes), must be the same in all rules. In this example, the container variable is ?start and the member variable is ?photo. If different variables were used for these, the rules would not work properly. The reason for this is due to the manner in which the builder processes the data.

Looking back at the results listed above, the palace photo appears twice so the second one will be removed, leaving only three matches. Although the builder actually generates mutliple matches for one resource, in many cases it isn't necessary to be aware of this detail when creating templates. It is usually sufficient to assume that earlier rules are used to filter results much like an if-else construct in a programming language. If the last rule has no specific conditions (for example a simple rule with no attributes on the <rule> element), it could be considered to be the final else block that matches all data.

Since results generated by earlier rules override those of later results, you will want to ensure that the rules are placed in the right order. The rules should be ordered from most specific to least specific. If you reversed the order of the two rules in the example above, three photos would still be displayed, but the larger rule that displays the image details would be dropped as the other rule has a higher priority. The result would be that the palace photo would not show this information.

Comments ( 5 )

July 10, 2005

4:20 PM How Templates Work XXIII - Multiple Simple Rules

You can also use multiple rules with the simple rule syntax. Here is the previous example rewritten using the simple syntax:

<hbox id="photosList" datasources="template-guide-photos3.rdf"
      ref="http://www.xulplanet.com/rdf/myphotos"
      xmlns:dc="http://purl.org/dc/elements/1.1/">
  <template>
    <rule dc:title="Canal">
      <button uri="rdf:*" image="rdf:*" label="View" orient="vertical"/>
    </rule>
    <rule>
      <image uri="rdf:*" src="rdf:*"/>
    </rule>
  </template>
</hbox>

The result to the user in this example is the same as the previous example. You can also mix simple and extended rules in one template, although you may prefer to use the same style in all rules for consistency.

However, if you are going to be using a number of rules, the template builder is more efficient when using multiple rules using the simple rule syntax. This is because all simple rules will iterate over the same data, usually the children of an RDF container. So the builder only performs this step once and filters the data for each rule.

When using the extended rule syntax, the manner in which the graph is navigated may be different for every rule, so no optimization can be done. The builder needs to process every condition of every rule. If, for example, you have six rules, each with a <member> condition, the builder will need to construct the children six times. You probably won't notice any difference for small amounts of data such as the photos example we've been using, but you might for large datasets. Thus, you will want to use the simple rule syntax when possible.

If you are only going to be using one rule, it doesn't matter, of course. Speaking of using a single rule, the simple rule syntax allows a slight shorthand. You can remove the <rule> element and place the rule's conditions directly on the <template>. There's no performance benefit, but it does save some typing.

<hbox id="photosList" datasources="template-guide-photos3.rdf"
      ref="http://www.xulplanet.com/rdf/myphotos"
      xmlns:dc="http://purl.org/dc/elements/1.1/">
  <template dc:title="Canal">
      <button uri="rdf:*" image="rdf:*" label="View" orient="vertical"/>
  </template>
</hbox>

This example shows only a single photo since a condition is used to filter out the other two photos. Note that in this shorthand, the conditions are placed directly on the <template> element.

Comments ( 2 )

July 9, 2005

7:03 PM How Templates Work XXII - Using Multiple Rules

All of the examples shown so far have used only a single rule. The builder supports the use of multiple rules as well. This involves using additional <rule> elements after the first. There are three main reasons for using multiple rules. First, to generate different content when different criteria are met, second, to apply different content for child elements created during template recursion, and third, just to generate additional results that will be merged with the first rule. We'll see examples of each of these techniques. A multiple rule template looks like the following:

<hbox id="photosList" datasources="template-guide-photos3.rdf"
      ref="http://www.xulplanet.com/rdf/myphotos">
  <template>
    <rule>
      <conditions>
        <content uri="?start"/>
        <member container="?start" child="?photo"/>
        <triple subject="?photo"
                predicate="http://purl.org/dc/elements/1.1/title"
                object="Canal"/>
      </conditions>
      <action>
        <button uri="?photo" image="?photo" label="View" orient="vertical"/>
      </action>
    </rule>
    <rule>
      <conditions>
        <content uri="?start"/>
        <member container="?start" child="?photo"/>
      </conditions>
      <action>
        <image uri="?photo" src="?photo"/>
      </action>
    </rule>
  </template>
</hbox>

This template contains two rules, the first contains a <triple> which matches only the photo with a title of "Canal". The second rule doesn't contain such a triple and will match all three of the photos. It the first rule was used by itself, only one result would match. If the second rule was used by itself, three results would match. When used together in this example, the results are combined and only three results are shown. However, you will probably notice that the one photo that matches the first rule has appeared differently that the others. In fact, the content for this photo is that of the first rule with the button, whereas the content for the other photos is that of the second rule with the normal images.

This demonstrates the first style of using multiple rules, to generate different content under different circumstances. This is a very useful and commonly used technique since it allows for more complex content to be displayed. For instance, the Firefox bookmarks toolbar displays folders in a different manner than regular bookmarks. This is done by using multiple rules, one for folders and another for bookmarks. Actually, there are more than two rules used in bookmarks, since there are also separators, submenus, and livemarks to deal with.

We already know that a rule generates a set of results, possibly filtered based on the rule's conditions. There's no magic to the way in which the template builder processes multiple rules. It just takes the results generated from the first rule, adds the results for the second rule, adds the results for the third rule, and so on. Here are the results that would be generated by the first rule above, before any bindings are applied:

(?start = http://www.xulplanet.com/rdf/myphotos,
 ?photo = http://www.xulplanet.com/ndeakin/images/t/canal.jpg,

Then, the builder adds the three results generated from the second rule:

(?start = http://www.xulplanet.com/rdf/myphotos,
 ?photo = http://www.xulplanet.com/ndeakin/images/t/canal.jpg,
(?start = http://www.xulplanet.com/rdf/myphotos,
 ?photo = http://www.xulplanet.com/ndeakin/images/t/palace.jpg)
(?start = http://www.xulplanet.com/rdf/myphotos,
 ?photo = http://www.xulplanet.com/ndeakin/images/t/canal.jpg)
(?start = http://www.xulplanet.com/rdf/myphotos,
 ?photo = http://www.xulplanet.com/ndeakin/images/t/obelisk.jpg)

So four possible results are available, one from the first rule and three from the second. However, the example shows that only content for three results are generated. What happened to the fourth result?

This is where the useful aspect of multiple rules comes in. Note that two of the results above are actually for the same photo (canal.jpg). The template builder removes any duplicate items before generating content. It does this by only allowing the match for the earliest rule. That is, the canal.jpg generated by the second rule is removed, since an earlier rule (the first rule) already generated a match for that result.

One important distinction is that the determination of duplicates is only based on the member variable, in this case the ?photo variable. It doesn't matter whether other variables are the same or not.

If you look at the example again, you might notice that the canal photo that matches the first rule has appeared in-between the other two photos, even though those photos are generated from the second rule. The builder hasn't put all the matches for the first rule before the matches for the second rule. In fact, the order is the same as the examples that only use one rule. Compare the multiple rule example with an earlier example that used only a single rule. The photos have appeared in the same order in both cases.

This is because the builder notices that the photos are in an RDF Seq in the datasource and arranges them in the order they appear in the Seq. This and other automated sorting done by the template builder is a fairly complicated process that will be discussed in more detail later.

Comments ( 5 )