Working with Content
Content Loader Functions
Each view has a pre-defined variable called ContentLoader. This exposes several functions for querying the Optimizely repository.
(Clearly, most of these functions are thin wrappers around IContentLoader functionality.)
Get
Gets a specific content item by numeric ID.
Content #123 is named: {{ ContentLoader.Get(123).Name }}.
GetParent
Gets the parent of the target page.
<p>Return to {{ ContentLoader.GetParent(Model.CurrentPage) | link }}.</p>
GetStartPage
The start page is named: {{ ContentLoader.GetStartPage().Name }}.
GetTopLevelPages
<ul>
{% for page in ContentLoader.GetTopLevelPages() %}
<li>{{ page | link }}</li>
{% endfor %}
</ul>
GetAncestors
Returns the lineage of pages from the target page to start page (inclusive), ordered from “closest” (parent of the target page) to “furthest” (always start).
<ul>
{% for page in ContentLoader.GetAncestors(Model.CurrentPage, reverse:true) %}
<li>{{ page | link }}</li>
{% endfor %}
</ul>
GetDescendants
Returns a flat list of all descendant pages. (If you want a nested list, use GetBranch.)
<ul>
{% for page in ContentLoader.GetDescendants(Model.CurrentPage) %}
<li>{{ page | link }}</li>
{% endfor %}
</ul>
GetCrumbtrail
Essentially the same as GetAncestors, but always reversed and with some arguments to specify inclusion or exclusion of the start and target pages.
<ul class="breadcrumbs">
{% for page in ContentLoader.GetCrumbtrail(Model.CurrentPage, false, true) %}
{% unless forloop.last %}
<li>{{ page | link }}</li>
{% else %}
<li>{{ page.name }}</li>
{% endif %}
{% endfor %}
</ul>
GetDepth
Returns the numeric “depth” of the page. The start page is 0, the top-level pages are 1, etc.
<ul>
{% for page in ContentLoader.GetDescendants(Model.CurrentPage) %}
<li class="depth-{{ ContentLoader.GetDepth(page) }}">{{ page | link }}</li>
{% endfor %}
</ul>
GetSiblings
Returns children of the same parent as the target page (exclusive of the target page).
<ul>
{% for page in ContentLoader.GetSiblings(Model.CurrentPage)
<li>{{ page | link }}</li>
{% endfor %}
</ul>
GetBranch
This returns a flat list which is a depth-first traversal of a branch of the content tree. The list is of INode objects. Those with an IsOpening value of true represent indents; those with an IsClosing value of true represent outdents. Those with a HasValue value of true contain a value in the Value property.
{% for node in ContentLoader.GetPageTree(ContentLoader.GetStartPage(), 2) %}
{% if node.IsOpening %}<ul>{% endif %}
{% if node.HasValue %}
<li>{{ node.Value | link }}</li>
{% endif %}
{% if node.IsClosing %}</ul>{% endif %}
{% endfor %}
GetNearestOfType
Climbs the tree from the target page until it finds a page of the specified type.
The parent section is {{ ContentLoader.GetNearestOfType(Model.CurrentPage, "SectionContainer") }}.
GetNearestWithValue
Climbs the tree from the target page until it finds a page with a non-null value in the specified property. (This is handy for finding the nearest ancestor with a checkbox checked.)
The parent section is {{ ContentLoader.GetNearestWithValue(Model.CurrentPage, "NavAnchor") }}.
Content Filters
url
Resolves and returns the raw URL for the content item.
link
Creates an anchor tag to the content item. Optionally, you can provide a CSS class, a target attribute, or the text of the link (if not provided, the page name is used).
Visit the page at {{ page | link: class:"sibling", target:"_blank" }}.
Visit {{ page | link: text:"my page"}}.