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.
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).
<sqltemplate type="display" tables="employees" limit="5"
<sqltemplate type="edit" tables="managers" limit="1"
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
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,
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.
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:
The changes are:
<sqltemplate type="display" tables="employees" limit="5">
<sqltemplate type="edit" tables="managers" limit="1">
Note that, both for regular ids
and default ids,
the new tag can
override the previous values. So if I do this:
- I've changed the id
of our "definition" template to default.
This is a special id
which indicates that the attributes in this tag should be applied to every tag of the
same name in the document - in this case, to all sqltemplate
- I've removed the hidden
attribute, since any tag whose id
is default is
automatically hidden. You can un-hide it by putting the hidden attribute back and
setting it to false.
- I've removed the ref
attributes from the last two sqltemplates, since we no longer need them
- the database will be set by default!
<sqltemplate database="myDB" tables="employees" limit="5"
The second two tags will actually, internally, look like this:
<sqltemplate database="myDB" tables="managers"
is overridden, database
and limit are inherited)
<sqltemplate database="myDB" tables="employees"
is overridden, database
and tables are
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
<sqltemplate tables="employees" limit="5">
These included tags will do the following:
<sqlfield id="moneyField" displayValue="'$' .
<enum key="f" value="Female">
<enum key="m" value="Male">
- Automatically fill in the database
and type attributes of
any sqltemplate tag in the
- For any sqlfield
whose ref is moneyField,
it will change the
display to make the money value look nicer.
- The enumset
with id eGender
will be entirely imported
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:
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).
<sqltemplate database="myDB" tables="employees" type="edit"
<sqltemplate ref="tempEdit" type="add" rowsToAdd="1">
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.
Enumsets | Referencing Tags | Next: