Mecha CMS

Updates.

Page Property

Author: Taufik Nurrohman · 65 Views

Tweet

Using page properties.

To call a property from a page, do this:

$page->foo_bar;

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 that is generated by hooks.

To set a value to the foo_bar 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 `foo_bar`.
...

Lorem ipsum dolor sit amet.

To set a value to the foo_bar 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 foo_bar property with a hook, do this:

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

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

0000000000

0 Comments

No comments yet.