LogoLogo
  • Introduction
  • Early Access
  • System Requirements
  • Elements FAQ
  • Purchasing & Licensing FAQ
    • License Types
  • Why Elements?
    • Static Website Benefits
    • Build a Digital Garden
  • Community Forum
  • Marketplace
  • Elements App
    • Getting Started
    • Keyboard Shortcuts
    • Migrations
      • Migrating from Blocs
      • Migrating from Classic
      • Migrating from Stacks
    • Design System
    • Editor
      • Apple Intelligence
      • Component Inspector
      • Dark Mode
      • Node Browser
      • Page Manager
      • Preview
      • Resources
      • Responsive Breakpoints
      • Workspaces
    • Components
      • Built-in Components
        • Accordion
        • Background
        • CMS
          • Helpers
          • Available Classes
            • ElementsCMS.php
            • ElementsCMSCollection
            • ElementsCMSItem.php
          • Frontmatter
          • Markdown
          • Online Editor
          • Pretty URLs
          • Syntax
        • Button
        • Container
        • Divider
        • Dropdown
        • Flex
        • Filter
        • Filter Tags
        • Form
        • Gallery
        • Grid
        • Image
        • Menu
        • Modal
        • Modal Close
        • Reveal
        • Image Slider
        • SVG
        • Text
        • Top Pages
        • Typography
        • Video
      • Common Controls
        • Link
        • Layout
        • Sizing
        • Spacing
        • Transitions
        • Effects
        • Filters
        • Transforms
        • Borders
        • Advanced
      • Custom Components
    • Globals
    • Templates
    • Project Settings
      • General
      • Web Icons
      • Advanced
      • Template
      • Publishing
    • Theme Studio
      • Theme
      • Colors
      • Screens
      • Font Family
      • Font Size
      • Spacing
      • Shadows
      • Border Width
      • Border Radius
      • Typography
    • How to
      • Add an Icon Inside a Button
      • Adjust Smooth Scroll Speed
      • Apply Anchor Scroll Padding
      • Add Snow to your Website
      • Build a Sticky Menu
      • Center Align Components
      • Create a Card
      • Site Banner with Text
      • Make a two column layout
    • Resources
    • Troubleshooting
    • SEO
      • Robots.txt
    • Accessibility
    • Advanced
      • URL Scheme
  • Elements Language
    • Introduction
      • Getting Started
      • Component Styling
    • Element Pack
      • info.json
      • Components
        • info.json
        • Icons
        • Properties.json
          • Grouping Controls
          • Default Values
          • General Structure
            • Title
            • ID
            • Format
            • Visible
            • Enabled
            • Responsive
          • UI Controls
            • Divider
            • Heading
            • Image
            • Information
            • Link
            • Number
            • Resource
            • Segmented
            • Select
            • Slider
            • Switch
            • Text
            • Text Area
            • Theme Border Width
            • Theme Border Radius
            • Theme Color
            • Theme Font
            • Theme Spacing
            • Theme Shadow
            • Theme Text Style
            • Theme Typography
        • Templates
          • Portal
          • Backend
          • Conditional Statements
          • Regular Expressions
          • Looping
          • Includes
          • Image Resources
          • HTML
            • Anchor
            • Raw
            • Editable Content
            • Dropzones
            • Inline templates
          • CSS
          • JavaScript
        • Assets
        • Hooks.js
          • Common use cases
          • Passing data to templates
          • Working with Collections
          • Working with UI Controls
          • Working with Resources
          • Available Functions
            • rw.addAnchor
            • rw.getBreakpoints
            • rw.getBreakpointNames
            • rw.getResponsiveValues
            • rw.resizeResource
            • rw.setProps
            • rw.setRootElement
          • Available Data
            • rw.collections
            • rw.component
            • rw.node
            • rw.pages
            • rw.project
            • rw.props
      • Collections
        • Data collections in Hooks.js
        • Accessing Data in Templates
        • Collections in properties.json
      • Shared Files
        • Assets
        • Templates
      • Themes
    • Troubleshooting
  • Elements Cloud
    • Getting Started
    • Troubleshooting
  • Elements Marketplace
    • Getting Started
    • Add-on Guidelines
    • Marketplace API
  • Branding
    • Logotype
    • Logotype Animated
    • Icon
  • Legal
    • Subscription Terms of Service
    • Cloud Terms of Service
    • Frameworks
Powered by GitBook

We are Realmac Software. We make nice things.

On this page
  • Constructor
  • Static Methods
  • Instance Methods
  • Usage Example

Was this helpful?

Edit on GitHub
Export as PDF
  1. Elements App
  2. Components
  3. Built-in Components
  4. CMS
  5. Available Classes

ElementsCMSItem.php

Represents a single CMS item loaded from a Markdown file with front matter. Provides structured access to metadata, raw and rendered content, file paths, and relationships to other items.

Constructor

__construct(ElementsCMS $cms, array $data = [])

Creates a new CMS item.

  • $cms (ElementsCMS): The CMS instance.

  • $data (array): The data for the CMS item.


Static Methods

static load(ElementsCMS $cms, string $filepath): ?self

Load a CMS item from a file.

  • $cms (ElementsCMS): The CMS instance.

  • $filepath (string): Path to the file.

  • Returns: ElementsCMSItem|null – Loaded item or null if not found.


Instance Methods

setOptions(array $options): self

Sets runtime options for the item.

  • $options (array): Option key/value pairs.

  • Returns: $this for method chaining.

getOption(string $key, $default = null)

Gets an option value by key.

  • $key (string): Option name.

  • $default (mixed): Default value if not found.

  • Returns: Mixed option value.

slug(): string

Returns the item’s slug (URL-friendly identifier).

title(): string

Returns the item’s title.

date(string $format = 'F j, Y'): string

Returns the item's date in the given format.

  • $format (string): Date format (default 'F j, Y').

datePublished(string $format = 'F j, Y'): string

Returns the item's published date.

dateModified(string $format = 'F j, Y'): string

Returns the item's last modified date.

featured(): bool

Checks if the item is marked as featured.

status(): string

Returns the item's status (e.g., published/draft).

body(): string

Returns the rendered HTML body content.

rawBody(): string

Returns the raw markdown body content.

image(): ?string

Returns the item's image URL, if available.

excerpt(int $words = 30): string

Returns an excerpt of the body, limited to the specified word count.

meta(string $key, $default = null): mixed

Fetches a value from the item's metadata/front-matter.

  • $key (string): Meta key.

  • $default (mixed): Default if key is missing.

attach(string $key, $value): self

Attach related data to the item (e.g., relations, computed properties).

get(string $key, $default = null): mixed

Returns any value from the item data or metadata.

__call($method, $arguments)

Allows dynamic method access to metadata fields.

file(): string

Returns the full path to the file.

fileName(): string

Returns the base filename (without extension).

url(): string

Returns the item’s URL.

lastModified(string $format = 'F j, Y'): string

Returns the file's last modified date.

toArray(): array

Returns all item data as an associative array.

with(string ...$relations): self

Declare relations (one-to-one or one-to-many) to eager-load.

withOne(string ...$relations): self

Declare one-to-one relations to eager-load.

withMany(string ...$relations): self

Declare one-to-many relations to eager-load.

loadRelations(): self

Loads all defined relations for the item.


Usage Example

$cms = new ElementsCMS(/* ... */);
$item = ElementsCMSItem::load($cms, 'posts/hello-world');

if ($item) {
    echo $item->title();           // Item title
    echo $item->body();            // Rendered body
    echo $item->datePublished();   // Published date
    $item->with('author', 'categories')->loadRelations();
}
PreviousElementsCMSCollectionNextFrontmatter

Last updated 9 hours ago

Was this helpful?