title: Template Syntax summary: A look at the operations, variables and language controls you can use within templates. # Template Syntax A template can contain any markup language (e.g HTML, CSV, JSON..) and before being rendered to the user, they're processed through [SSViewer](api:SilverStripe\View\SSViewer). This process replaces placeholders such as `$Var` with real content from your [model](../model) and allows you to define logic controls like `<% if $Var %>`. An example of a SilverStripe template is below: **app/templates/Page.ss** ```ss
<% base_tag %>Welcome $FirstName $Surname.
<% end_with %> <% if $Dishes %>You are coming from $UsersIpAddress.
```You are logged in as $CurrentMember.FirstName $CurrentMember.Surname.
<% end_if %> ``` A conditional can also check for a value other than falsy. ```ss <% if $MyDinner == "kipper" %> Yummy, kipper for tea. <% end_if %> ```Page '$Title' is a child of '$Up.Title'
<% loop $Children %>Page '$Title' is a grandchild of '$Up.Up.Title'
<% end_loop %> <% end_loop %> ``` Given the following structure, it will output the text. My Page | +-+ Child 1 | | | +- Grandchild 1 | +-+ Child 2 Children of 'My Page' Page 'Child 1' is a child of 'My Page' Page 'Grandchild 1' is a grandchild of 'My Page' Page 'Child 2' is a child of 'MyPage'Page '$Title' is a child of '$Up.Up.Up.Title'
<% end_loop %> ``` #### Top While `$Up` provides us a way to go up one level of scope, `$Top` is a shortcut to jump to the top most scope of the page. The previous example could be rewritten to use the following syntax. ```ssPage '$Title' is a child of '$Top.Title'
<% loop $Children %>Page '$Title' is a grandchild of '$Top.Title'
<% end_loop %> <% end_loop %> ``` ### With The `<% with %>` tag lets you change into a new scope. Consider the following example: ```ss <% with $CurrentMember %> Hello, $FirstName, welcome back. Your current balance is $Balance. <% end_with %> ``` This is functionalty the same as the following: ```ss Hello, $CurrentMember.FirstName, welcome back. Your current balance is $CurrentMember.Balance ``` Notice that the first example is much tidier, as it removes the repeated use of the `$CurrentMember` accessor. Outside the `<% with %>.`, we are in the page scope. Inside it, we are in the scope of `$CurrentMember` object. We can refer directly to properties and methods of the [Member](api:SilverStripe\Security\Member) object. `$FirstName` inside the scope is equivalent to `$CurrentMember.FirstName`. ### Me `$Me` outputs the current object in scope. This will call the `forTemplate` of the object. ```ss $Me ``` ## Comments Using standard HTML comments is supported. These comments will be included in the published site. ```ss $EditForm ``` However you can also use special SilverStripe comments which will be stripped out of the published site. This is useful for adding notes for other developers but for things you don't want published in the public html. ```ss $EditForm <%-- Some hidden comment about the form --%> ``` ## Related Lessons * [Creating your first theme](https://www.silverstripe.org/learn/lessons/v4/creating-your-first-theme-1) ## Related Documentation [CHILDREN Exclude="How_Tos"] ## How to's [CHILDREN Folder="How_Tos"] ## API Documentation * [SSViewer](api:SilverStripe\View\SSViewer) * [ThemeManifest](api:SilverStripe\View\ThemeManifest)