-
Notifications
You must be signed in to change notification settings - Fork 9
Functions
Joomlatools Pages comes out-of-the-box with template functions that help you to easily customise your template. Functions are only supported for the PHP engine. Of course, you can also use any PHP function in your templates.
- page()
- collection()
- data()
- route()
- state()
- attributes()
- helper()
- import()
- date()
- slug()
- json()
- format
- replace
- [debug](#debug]
The page() template function allows you to retrieve a page object which contains the page properties such as title, content, layout, etc.
You can use the page() function to target the current page or any active page. If a page path is not provided, the method will return the current page.
Examples:
$title = page()->title; // Return the title of the current page.
$title = page('path/to/page')->title; // Return the title of another page, not the current page.See also: Page
The collection()template function allows you to retrieve a collection. It will return a traversable object.
The $state function parameter should be a php array that defines the specific state to be used to filter the collection.
See also: Page > Collection
Example:
$collection = collection('pages', ['limit' => 3, 'order' => 'shuffle');
The data($path) template function allows you to work with structured data in templates. It receives a relative path or URL to the data file that needs to be loaded.
Example:
$data = data('company/team');
See also: Data
If you want to generate an URL for a page you can use the route($page, $query = array(), $escape = false) template function. The function accepts three parameters:
-
$page: A page path, or page object -
$query: Additional query parameters used to generate the URL -
$escape: If true, the URL will be escaped, for example for use in javascript.
Fragments that are included in the path are also appended to the resulting URL.
Example:
$url = route('path/to/page#fragment');
or
$url = route(page()#fragment);
See also: URLs and Linking > Routing
The state() function gives you access to the active state. It returns a KModelStateInterface object.
Example:
$state = state();
See also: Collection > State
The attributes() template function allows you to generate HTML element attributes. This is especially useful if the attribute could be empty and you don't want to render its name. For example:
<a <?= attribute('class', $classes);?> <!-- if $classes is empty, it will not generate anything.-->The helper($identifier, $config = array()) template function allows you to include template helpers, it accepts a helper identifier and an optional array of config options for the helper.
See also: Templates > Helpers
The import($path, $data = array()) template function allows you to import a partial into a page or into another partial. For the sake of consistency, it is best to place them in the /partials directory.
However, you can import partials relative to the root pages folder /pages. The path to resolve the location of the partial can be both relative to the folder the partial is imported in, or absolute to the root pages folder.
The following partials are resolved against the root pages folder:
file.htmlfolder/file.html
The following partials are resolved relative to the folder the partials are imported into.
../file.html../folder/file.html
You can also pass variables to a partial. For example, suppose you have a partial called note.html in your /partials directory that contains this formatting:
<div>
<?= $note ?>
</div>The <?= $note ?> is a variable that gets populated when you import the partial and specify a value for that parameter, like this:
<?= import('/partials/note.html, ['content' => "This is my sample note."]) ?>The value of content (which is "This is my sample note") will be inserted into the $note variable.
Passing variables to includes is especially helpful when you want to hide away complex formatting from your Markdown content.
See also: Templates > Partials
[todo]
[todo]
[todo]
[todo]
[todo]
[todo]
Got a question or need help? We have a forum on Github Discussions where you can get in touch with us.