with
The with
statement allows for processing code associated with a given page or file. When using a page's URL or one of the keywords prev
and next
as an argument, the context within the block of code changes to that page. When using a file path, it is possible to access related runtime variables and, in case the file is an image, you can directly apply options for resizing and cropping. If there is no matching page of file, an optional else
block is processed instead.
Changing Context
Using URLs
When using a page URL, either by string or variable value, the context within the statement will change to the given page, in case the page exists. All variables used in the statement's code are then associated with that page automatically. The else
block is optional.
<@ with "/page/url" @>
@{ title }
<@ else @>
Page doesn't exist!
<@ end @>
Using a variable as argument:
<@ with @{ page } @>
@{ title }
<@ end @>
Previous and Next Pages
Instead of a page URL, it is also possible to use the keywords prev
and next
to change the context to the previous or next item in the pagelist.
<@ newPagelist { type: 'siblings' } @>
<@ with prev @>
<a href="@{ url }">
Previous: @{ title }
</a>
<@ end @>
<@ with next @>
<a href="@{ url }">
Next: @{ title }
</a>
<@ end @>
Using Files
Passing a file path as argument allows for using the file's metadata and runtime variables in the statement's code block. In case the file is an image, resizing options can be passed as well. The file path can be either a full path or a glob pattern. When using glob patterns, the first match will be used.
<@ with @{ logo | def('/shared/*logo*') } {
width: 100,
height: 100,
crop: true
} @>
<img
src="@{ :fileResized }"
alt="@{ :basename }"
width="@{ :widthResized }"
height="@{ :heightResized }"
title="@{ :caption }"
/>
<@ else @>
<# Use sitename in case ther is no logo. #>
@{ sitename }
<@ end @>
Related runtime variables:
- :basename
- The basename (filename) of the file.
- :caption
- A caption associated with the current file in a
with
statement. - :file
- The resolved file path of the current file.
- :fileResized
- The file path of the resized version of the file.
- :height
- The height of the file.
- :heightResized
- The height of the resized version of the file.
- :width
- The width of the file.
- :widthResized
- The width of the resized version of the file.