3.7. Instructions
3.7.13. opt:insert
3.7.12. opt:include
« Previous
3.7.14. opt:literal
Next »

3.7.13. opt:insert

opt:insert allows to place the template snippets in the specified place. It is a part of the template inheritance feature.

Name Type Required? Description
snippet hard string Yes The name of the snippet
ignoredefault option No If set to yes, the default value of opt:insert is not treated as the snippet parent.

An example:

<opt:snippet name="foo">
    <p>I am your snippet.</p>
</opt:snippet>
 
<div>
    <opt:insert snippet="foo" />
</div>

Contrary to opt:capture, the inserted code remains fully functional. In the following example we change the value of the variable displayed in the snippet. We see that the value also changes in the output, between two inserts of the same code.

<opt:snippet name="foo">
    <p>Value: {@hoo}</p>
</opt:snippet>
 
<div>
    {@hoo is 1}
    <opt:insert snippet="foo" />
 
    {@hoo is 2}
    <opt:insert snippet="foo" />
</div>

The result:

<div>
    <p>Value: 1</p>
    <p>Value: 2</p>
</div>

The default content

opt:insert allows to define the default content in case the snippet does not exist:

<div>
    <opt:insert snippet="foo">
        <p>Oops, one snippet is missing.</p>
    </opt:insert>
</div>

By default, if the snippet contains the opt:parent tag and does not overload any other snippet, the default content of opt:insert will be treated as the parent:

<opt:snippet name="foo">
    <p>This is a snippet code.</p>
    <opt:parent />
</opt:snippet>
 
<div>
    <opt:insert snippet="foo">
        <p>This is a default code.</p>
    </opt:insert>
</div>

The result:

<div>
    <p>This is a snippet code.</p>
    <p>This is a default code.</p>
</div>

We can turn this off by setting the attribute ignoredefault to yes in opt:insert:

<opt:snippet name="foo">
    <p>This is a snippet code.</p>
    <opt:parent />
</opt:snippet>
 
<div>
    <opt:insert snippet="foo" ignoredefault="yes">
        <p>This is a default code.</p>
    </opt:insert>
</div>

The result:

<div>
    <p>This is a snippet code.</p>
</div>

Now, the default content will never appear, if there is any snippet named foo.

Inserting the content from opt:capture

opt:insert may also display the content captured by opt:capture. Contrary to the $system variable, the captured part can be chosen dynamically. The instruction takes only one attribute then:

Name Type Required? Description
captured Expression Yes The name of the captured part

If the tag contains some content, it is treated as a default content and displayed, if the specified captured part does not exist:

<opt:capture as="foo">
Some content here...
</opt:capture>
 
{@captured is 'foo'}
<opt:insert captured="@captured">
The default content.
</opt:insert>

See also:

3.7.13. opt:insert
3.7. Instructions
« Previous
3.7.12. opt:include
Next »
3.7.14. opt:literal