Table of Contents
This extension provides a bare minimum of “mark” functionality without the frills. It consists of a HTML form with just a submit button.
Usage
Add this snippet at any point in the .\lot\y\*\page.php
file (for example, in the page footer) in your active layout folder to display the mark button:
<?= self::form('mark'); ?>
Options
Key | Description |
---|---|
active | If the value is set to false , then the mark button will be disabled. |
as | Label for the current marking context. If this option is not set, the value will be the key value. |
description | The description text or HTML that will be used as the value of the title attribute on the submit button. If this option is not set, and the as option is set, the value will be 'Mark as %s' if it has not been marked, or 'Unmark as %s' if it has been marked. |
key | See Marking Context. |
kick | Custom redirection link. If this option is not set, the user will be redirected to the associated page route after the mark. |
page | The page object to associate with the mark data. If this option is not set, it will take the current page object as the value. |
title | The submit button text or HTML. If this option is not set, the value will be 'Mark' if it has not been marked, or 'Unmark' if it has been marked. |
Marking Target
The page
option can be used to specify a page object as the marking target. By default, it takes the current page object as its value:
<?php
$page = new Page(LOT . D . 'page' . D . 'about.page');
echo self::form('mark', [
'page' => $page,
'title' => i(empty($page->marked[""]) ? 'Mark as read' : 'Cancel')
]);
?>
It is especially useful if you want to display mark buttons in .\lot\y\*\pages.php
file:
<?php foreach ($pages as $page): ?>
<article>
<h3>
<?= $page->title; ?>
</h3>
<p>
<?= $page->description; ?>
</p>
<?= self::form('mark', [
'kick' => $url->path,
'page' => $page
]); ?>
</article>
<?php endforeach; ?>
Marking Context
By default, the context is an empty string, so your mark status will be available as a ""
item in the $page->marked
array:
test($page->marked); // Returns `["" => true]`
if (!empty($page->marked[""])) {
// …
}
However, you can use the same form to create your own marking context to categorize your mark data, that is, by determining your marking context on the key
option:
<div class="marks">
<?= self::form('mark', [
'as' => i('Favorite'),
'key' => 'favorite',
'title' => empty($page->marked['favorite']) ? '❤️' : '🖤'
]); ?>
<?= self::form('mark', [
'as' => i('Read'),
'key' => 'read',
'title' => empty($page->marked['read']) ? '🟢' : '⚫'
]); ?>
</div>
.marks {
display: flex;
flex-wrap: wrap;
gap: 0.5em;
margin: 2em 0;
}
.marks > * {
margin: 0;
padding: 0;
}
Marked Pages
You will be able to see all the pages that have been marked at http://127.0.0.1/mark/1
or http://127.0.0.1/mark/:mark/1
where :mark
is your marking context.
Marking Data
Marking data are stored in cookies. You can use the marks()
function to obtain those data so that you can further work with them as you wish:
marks(string $key = null): array;
Here is an example of how to use the function to create a list of marked pages:
<?php
function recent_marks(int $chunk = 5, string $key = "") {
extract(lot(), EXTR_SKIP);
$content = "";
$title = i('Marks');
if (isset($state->x->mark)) {
$marks = marks($key);
if ($marks && !empty($marks[0])) {
if (isset($marks[1])) {
$title = i($marks[1]);
}
$content .= '<ul>';
foreach (array_slice($marks[0], 0, $chunk) as $v) {
$v = LOT . D . $v;
$content .= '<li>';
if ($file = exist([
$v . '.archive',
$v . '.page'
], 1)) {
$mark = new Page($file);
if ($page->url === $mark->url) {
$content .= '<a aria-current="page">';
} else {
$content .= '<a href="' . $mark->url . '">';
}
$content .= $mark->title;
$content .= '</a>';
} else {
$content .= '<a aria-disabled="true">';
$content .= '/' . $v; // Page does not exist
$content .= '</a>';
}
}
if (count($marks[0]) > $chunk) {
$content .= '<li>';
if ($site->is('marks') && $key === $page->name) {
$content .= '<a aria-current="page">';
$content .= '…';
$content .= '</a>';
} else {
$content .= '<a href="' . $url . '/' . trim($state->x->mark->route ?? 'mark', '/') . ("" !== $key ? '/' . $key : "") . '/1" title="' . eat(i('View All')) . '">';
$content .= '…';
$content .= '</a>';
}
$content .= '</li>';
}
$content .= '</ul>';
} else {
$content .= '<p role="status">';
$content .= i('No %s yet.', 'marks');
$content .= '</p>';
}
} else {
$content .= '<p role="status">';
$content .= i('Missing %s extension.', ['<a href="https://mecha-cms.com/store/extension/mark">mark</a>']);
$content .= '</p>';
}
return '<h3>' . $title . '</h3>' . $content;
}
Use it like this:
<div>
<?= recent_marks(5); ?>
</div>
<div>
<?= recent_marks(5, 'read'); ?>
</div>
0 Comments
No comments yet.