XML technologies / SVG / Creating graphic elements from templates / Defining templates for elements
(Excerpt from “Das Einsteigerseminar SVG — Webgrafiken mit XML” by Helma Spona)
SVG element templates are defined with the <symbol> tag and referenced with the <use> tag. Unlike symbols defined with <symbol>, other named objects and groups are rendered even if hidden behind another object or formatted invisible, costing time according to their number and complexity. Elements defined with <symbol> are not rendered, only objects created from them. If you define 100 elements with <symbol> and use only two, only those two instances are calculated.
Inside the <symbol> tag, use filters, gradients, groups and all basic SVG objects. Inside the <symbol> tag, you can also use a symbol-specific <defs> section. Otherwise, definitions originally hidden in the <defs> section may become visible when the template is used. Prevent this by placing, inside the <symbol> tag, these elements within <defs>. Define the template preferably in the page’s <defs> section.
For an existing complex element, simply wrap its definition in a <symbol> tag and move the closing tag </defs> if present. The next listing uses the previous chapter’s graphic button.svg as its basis. Highlighted lines must be added or moved (</defs>). If a filter is assigned, define two symbols: one without a filter, used in the second through a <use> tag. Assign filters to that <use> tag.
<?xml version="1.0" standalone="no"?>
...
<svg width="300" height="800">
<defs>
<symbol id="bttnohne">
<desc>Der Button</desc>
<circle cx="25" cy="25" r="25" style="stroke-width:1;stroke-opacity:0.8;stroke:rgb(222,0,0);fill-opacity:1;opacity:1;fill:url(#runderVerlauf);"/>
<ellipse cx="25" cy="15" rx="20" ry="12" style="stroke-width:1;stroke:none;fill-opacity:1;opacity:1;fill:url(#linearerVerlauf)"/>
</symbol>
...
<symbol id="bttn">
<use x="15" y="10" xlink:href="#bttnohne" style="filter:url(#SVGObjekt)"/>
</symbol>
</defs>
</svg>
Running the file displays nothing, because the symbol has been defined but not used.
Copyright © verlag moderne industrie GmbH 2001
You may print the online version for your private use.
Otherwise, this chapter from “Das Einsteigerseminar SVG — Webgrafiken mit XML” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
verlag moderne industrie GmbH, Justus-von-Liebig-Straße 1, D-86899 Landsberg/Lech, fon ++49 (0) 81 91 / 125 – 0, leserservice(at)mi-verlag.de
back to top