Page Property

Using page properties.

To call a property from a page, do this:

$page->fooBar;

The default value is always null. This is the thing that makes Mecha version 2.x.x different from version 1.x.x. In version 1.x.x, calling a property that does not exist from a page will trigger an error message, but not in version 2.x.x. In version 2.x.x, any page property is valid and has the value of null unless you set a value explicitly to it, either from page header, page data, or from a return value generated by hooks.

To set a value to the fooBar property, add a data in the page header with foo-bar as the property key:

---
title: Test Page
description: Testing page property.
type: Markdown
foo-bar: Test property value of `fooBar`.
...

Lorem ipsum dolor sit amet.

To set a value to the fooBar property from a separated file, add a file named as foo-bar.data to the related folder:

.\
└── lot\
    └── page\
        ├── test-page\
        │   └── foo-bar.data
        └── test-page.page

To set a value to the fooBar property with a hook, do this:

Hook::set('page.foo-bar', function ($value) {
    // `$value` is `null` by default
    return 'Test property value of `fooBar`.';
});

Now, $page->fooBar will return “Test property value of `fooBar`.”.

0 Comments

No comments yet.