SourceForge.net Logo
Home | SourceForge | Documentation | FAQ | What's New | To-Do List | Forums | Download | Contact

Sparse - Referencing Tags

Tags and attributes are a powerful way to define your applications... but there is a downside, which is the fact that you sometimes will have to keep re-defining things all the time. This is addressed in HTML and XML by the use of CSS, but Sparse doesn't quite provide for that. Instead, you can do something almost as good: You can reuse attributes!

It's probably easiest just to show you an example of this.

<sqltemplate database="myDB" id="defaultTemplate" hidden="true"/>
<sqltemplate type="display" tables="employees" limit="5" ref="defaultTemplate">
  <sqlrow>
     [........]
  </sqlrow>
</sqltemplate>
<P>
<sqltemplate type="edit" tables="managers" limit="1" ref="defaultTemplate">
  <sqlrow>
     [........]
  </sqlrow>
<sqltemplate>

There's several things happening here. There are three sqltemplates listed here. The first one is our "definition" template - where we tell Sparse how to treat all the other templates. In the first line, we give our template an id of defaultTemplate, and set hidden=true so that it won't actually try to be displayed (we can't display it, because there's no other information in it! We just want to define it for future use).

Now, we can reference our template by using the ref attribute. In the next two templates, we reference the defaultTemplate which we just defined. What Sparse will do now is to take all the attributes in the defaultTemplate and apply them to the new templates. In other words, we can leave out the database attribute entirely from the last two templates, since we already defined it in the first one!

This has many uses. For a start, you can keep attributes which you would normally repeat over and over in a single place. If you want to change them, a single change is all that's needed. You can also keep these "definition" tags in a separate file (see below) so you can change only a single file to alter your entire application - just like CSS!

One more thing - you can include multiple ids in your ref attribute. This allows you to define a single tag in terms of several other tags. For example, you can say that all sqlfield tags whose id is money should display its value a certain way (see Working With Data). And you can say that all tags whose id is blue should display with a blue background (OK, you can just do this with a CSS class, but bear with me). If you set your ref attribute to blue money, both sets of attributes will be included. Note that if there are multiple ids that have the same attribute, the first id will override the later ones.

Default Attributes

That looks nice, but you still have to keep referencing defaultTemplate every time you want to change something. What if you want every template you use to have the same database automatically - similar to a CSS class that affects all TD tags? Easy-peasy: say hi to default attributes. A few minor changes gives us this:

<sqltemplate database="myDB" id="default"/>
<sqltemplate type="display" tables="employees" limit="5">
  <sqlrow>
     [........]
  </sqlrow>
</sqltemplate>
<P>
<sqltemplate type="edit" tables="managers" limit="1">
  <sqlrow>
     [........]
  </sqlrow>
<sqltemplate>

The changes are:
Note that, both for regular ids and default ids, the new tag can override the previous values. So if I do this:

<sqltemplate database="myDB" tables="employees" limit="5" id="default">
<sqltemplate tables="managers">
 [.......]
</sqltemplate>
<sqltemplate limit="10">
  [......]
</sqltemplate>

The second two tags will actually, internally, look like this:
<sqltemplate database="myDB" tables="managers" limit="5"> (tables is overridden, database and limit are inherited)
<sqltemplate database="myDB" tables="employees" limit="10"> (limit is overridden, database and tables are inherited)

Including External Tags

While we're pretending to be CSS styles, one of the great things about CSS is the ability to use a single global CSS file to affect every page that includes it. Luckily, Sparse does that too! Just utilize the sqlinclude tag. Not only can you include enumsets this way, you can also include any tags you want whose attributes should be supplied to other tags. An added bonus is that any tags in an external file are automatically hidden by default, so you don't have to supply that attribute either. A quick example:

<sqlinclude src="defaults.xml"/>
<sqltemplate tables="employees" limit="5">
  <sqlrow>
     <tr>
      <td><sqlfield name="name"></td>
      <td><sqlfield name="salary" ref="moneyField"></td>
      <td><sqlfield name="gender" enumset="eGender"></td>
    </tr>
  </sqlrow>
</sqltemplate>

defaults.xml:

<sqltemplate database="myDB" type="edit" id="default"/>
<sqlfield id="moneyField" displayValue="'$' . number_format($value, 2)"/>
<enumset id="eGender">
  <enum key="f" value="Female">
  <enum key="m" value="Male">
</enumset>

These included tags will do the following:

Reusing Entire Bodies

One last neat thing you can do is to reuse the contents of an entire sqlrow. This is primarily useful for appending an add template just below an edit template which looks identical. For example:

<table>
<sqltemplate database="myDB" tables="employees" type="edit" limit="5" id="tempEdit">
  <sqlrow>
    <tr>
      <td><sqlfield name="name"> <sqlfield name="salary"></td>
      <td><sqlaction type="action"></td></tr>
  </sqlrow>
</sqltemplate>
<sqltemplate ref="tempEdit" type="add" rowsToAdd="1">
</table>

This will simply print one extra row below the edit template allowing you to add a new employee. Note that an sqlaction action button is used; this will be turned into the proper button for each template (edit for the edit template, add for the add template).

This appending of the sqlrow contents is unique to sqltemplate; nothing similar will happen to any other tags. If you don't want the sqlrow content to be appended to the referencing tag, you'll have to make sure you define your templates using a separate sqltemplate which has no sqlrow in it.

Previous: Enumsets | Referencing Tags | Next: Searching