Skip to content

Directives

Sugar provides familiar control structures and attribute helpers as HTML attributes.

TIP

Directives are just attributes prefixed with s:. They keep templates readable while compiling to fast PHP.

Directive Types

TypeWhat it doesExamples
Control FlowWraps elements in conditions or loops.s:if, s:foreach, s:switch, s:try
AttributeComputes or merges attributes.s:class, s:spread, s:tag
ContentReplaces element content with output.s:text, s:html
Pass-throughHandled by other passes.s:slot, s:bind, s:raw
html
<div s:if="$isReady">Ready</div>
html
<div s:class="['active' => $isActive]"></div>
html
<div s:text="$userName"></div>
html
<s-card s:bind="$cardProps">
	<div s:slot="header">Title</div>
</s-card>
html
<div s:raw>
	{{ this-is-left-untouched }}
	<s-template s:if="$nope">Not parsed</s-template>
</div>
Details

Jump to the full pages

Special Elements

  • <s-template> - Fragment element that renders only children and carries directives without adding HTML.

Use it as a wrapper when you need directives or scoping without introducing a real DOM node. It is also used for template inheritance and includes.

html
<s-template s:if="$show">
	<h2>Only this output renders</h2>
</s-template>

For related examples, see: