Built-in Page Variables

During the build phase, Coralite automatically injects a page object into the state of your components. This object provides essential information about the current page's URL and file path, making it easy to create dynamic logic like highlighting active navigation links or generating breadcrumbs.

These core variables are organized into url and file objects within the page context.

Available Page Variables #

Metadata Variables #

In addition to the path-related variables, if you are using the built-in metadata plugin, it injects metadata from the page's <head> into the page.meta object:

Example Usage #

Because templates do not support dot notation for tokens (e.g., {{ page.url.pathname }}), you must map these variables to local properties in your component's state (via the data function) or getters.

Here is an example of a navigation link component that automatically adds an "active" class if the current URL matches its target URL:

HTML
Code copied!
<!-- src/components/nav-link.html -->
<template id="nav-link">
  <a href="{{ href }}" class="link {{ activeClass }}">
    <slot></slot>
  </a>
</template>

<script type="module">  
  import { defineComponent } from 'coralite'
  
  export default defineComponent({
    attributes: {
      href: { type: String }
    },
    getters: {
      // Mapping nested page variable to a flat state property
      activeClass: (state) => (state.href === state.page.url.pathname ? 'active' : '')
    }
  })
</script>

For static values that don't require reactivity on the client, mapping them in data is the preferred approach:

HTML
Code copied!
<!-- src/components/page-info.html -->
<template id="page-info">
  <div class="page-info-box">
    <p>Current URL: <strong>{{ currentUrl }}</strong></p>
    <p>Source File: <strong>{{ fileName }}</strong></p>
    <p>Language: <strong>{{ language }}</strong></p>
  </div>
</template>

<script type="module">  
  import { defineComponent } from 'coralite'
  
  export default defineComponent({
    data ({ page }) {
      return {
        currentUrl: page.url.pathname,
        fileName: page.file.filename,
        language: page.meta.lang
      }
    }
  })
</script>

Start Building with Coralite!

Use the scaffolding script to get jump started into your next project with Coralite

Copied commandline!