Object References
An object reference is a special object in Go that can provide either id
and/or class
attributes to an element.
%div[article]
%h1.article-title= article.Title
%p= article.Content
In this example we used the article
value as an object reference to the div
element. Here is how this might render out:
<div id="article-123" class="article">
<h1 class="article-title">The Title</h1>
<p>The content of the article</p>
</div>
The article
value we used in the example has implemented the following two methods:
func (a Article) ObjectID() string {
return fmt.Sprintf("article-%d", a.ID)
}
func (a Article) ObjectClass() string {
return "article"
}
Anything that implements func ObjectID() string
can be used to provide an id
attribute to an element. Likewise, anything that implements func ObjectClass() string
can be used to provide a class
attribute to an element.
We can also provide a prefix that will be used for both the class and id attributes.
%div[article, "stunning"]
%h1.article-title= article.Title
%p= article.Content
renders as:
<div id="stunning_article-123" class="stunning_article">
<h1 class="article-title">The Title</h1>
<p>The content of the article</p>
</div>