Docs: Search, comments and previews
Search, comments and previews
Site search, comments and drafts: searchResults, askBox, comments, commentsBlock, isPreview and previewed.
The examples use the $esc shortcut from the overview.
Search #
Print a search box or link only while search is on: searchEnabled() (in Actions and sections) is true. The /search address is routed only while search is on, and a form posting there on an install with search off lands on a 404.
searchResults() #
The search page's state: {state, query, page, rows, prev, next, ask, query_max, start} (the snippet is M, the rest P). query_max and start are new in 1.18.0.
<?php $r = $this->searchResults(); ?>
<h1>Results for <?= $esc($r['query']) ?></h1>
<?php $this->askBox(); ?>
<ol start="<?= (int) $r['start'] ?>">
<?php foreach ($r['rows'] as $row): ?>
<li><a href="<?= $esc($row['url']) ?>"><?= $esc($row['title']) ?></a> <p><?= $row['snippet'] ?></p></li>
<?php endforeach; ?>
</ol>
- Your own search page is optional. Core ships
views/search.phpfor/searchand floors it for every theme. Ship your own and it wins. stateisempty(nothing asked yet),preparing(the index has not finished its first build),results,throttledorpaused(maintenance).queryis what was searched andpagethe page shown.prevandnextare the neighbouring pages' URLs ornull. There is no total.- Each row is
{title, url, snippet, kind, date}.titleis plain text.urlis a root-relative path.snippetis already escaped, with the matched words in<mark>.kindis the source (page,post,forum_topic, or a module's own).dateisY-m-d H:i:sornull. query_maxis the longest query the index reads, for the box'smaxlength. It is there on every page, so a search box in a 404 page or a header can read$this->searchResults()['query_max'].startis the number of the page's first result, for an<ol start>.
askBox() #
Prints the AI answer block. A visitor can ask for an AI answer to the query they searched, when the site owner turns answers on in System ▸ AI settings. Your page shows that block only once it calls askBox(), best right after your results heading. It prints nothing unless an answer can be had, pushes its own script and stylesheet through the asset channel, and stays hidden without JavaScript. Leave it out and your page offers no answers. Do not build the block yourself from searchResults()['ask']: the token, the disclosure naming the AI provider and the script that renders the answer as plain text belong together.
Comments #
commentsBlock($kind, $id) #
Prints the comments component for a page or post.
<?php $this->commentsBlock('post', (int) $post['post_id']); ?>
<?php $this->commentsBlock('page', (int) ($page['page_id'] ?? 0)); ?>
Comments are kept by the Comments module. commentsBlock() prints nothing while that module is off. The Blog's own post view calls commentsBlock() after the article, and the starter theme's page view calls it after the page's content. If your theme ships its own post view or page view, its posts or pages show comments only once it calls it too.
It prints nothing in a preview, while no module keeps comments, while the item's comments are switched off (a page takes them only on a Layout chosen on the Comments page's Settings tab), or when the visitor may not read them. It pushes core/assets/comments.css through the asset channel. The markup is the comments component. Ship components/comments.php and yours wins over core's floor. It receives $comments, $kind and $id.
comments($kind, $id) #
A thread for a page or post as {state, total, items, more_url, form, notice, view_as, images_failed} (names P, body M), or null. It is what $comments holds in the component.
stateisopen(the form is there),closed,signin(a guest: link to the sign-in address),verify(confirm the email address first) ordenied.totalcounts every visible comment.itemsholds the first top comments, oldest first, each followed by its first replies. Later pages count top comments only, so a thread is never split. Each item is{id, anchor, author: {name, url, user_id, picture_url, profile_url, badge}, body, date, parent, depth, reply_to, reactions, more_replies, more_replies_link, images}.- Escape each author's
nameandurl(urlis a root-relative path ornull).user_id,picture_urlandprofile_urlare new in 1.19.2 and are raw: escapepicture_urlandprofile_urltoo. Both are''when the person has no profile this visitor may open.urlfalls back toprofile_url, so the name links where the picture does. Draw the face with theperson-avatarcomponent. Echo eachbodyas it is.dateisY-m-d H:i:sUTC. - Indent a reply by its
depth(0 or 1, never more) and namereply_towhen it is set. author.badge(new in 1.20.0) is the staff badge the author's role shows, as plain text. Escape it, and draw nothing when it is empty.reactions(new in 1.20.0) is the comment's reaction row, ornull. Hand it as it is to thereactionscomponent, withinertfromview_as, which is true under View as (the chips show and do nothing):$this->component('reactions', ['reactions' => $item['reactions'], 'inert' => !empty($comments['view_as'])]).images(new in 1.21.0) is the comment's pictures, oldest first:[{id, url, small_url, width, height}]. Print them after the body and escape bothurlandsmall_url(small_urlcarries&s=1). Core'spicturescomponent draws them.images_failed(new in 1.21.0) is true after?images=failed. Say it witht('images.failed').- Give the section
id="comments"and each commentid="<anchor>"(comment-<id>): a member lands there after posting. - Show
more_urlas a link to the rest when it is notnull, andnotice(heldorposted) after a post. - A top comment may carry
more_repliesandmore_replies_link(new in 1.20.0). Each is a root-relative path ornull. Draw "Show more replies" only when both are set. - If you draw the thread yourself, keep this shape, or the loaded replies have nowhere to go. Each comment is an
<li data-comment-id="<id>">. A top comment's replies sit inside it in an<ol class="cms-comments__replies">. "Show more replies" is that list's last item: an<li data-comment-more-replies="<more_replies>">holding an<a href="<more_replies_link>">. Core draws this incore/components/comment-items.php. - The Comments module's own component loads the script that fetches the next replies in place. If your
components/comments.phpreplaces it, that script is not loaded and the link opens the page that shows them, which works without JavaScript. - A theme that overrides the module's
views/comments-replies.phpanswers with the<li>s alone: no header, footer orpage-container.
Draw the form only from form, which is there only while state is open: {action, csrf, guest, fields, stamp, trap, captcha, captcha_provider, notify, replies, reply, max_length, images}.
- Send
csrfascsrf_tokenand the text asbody. form.images(new in 1.21.0) is how many pictures one comment may hold, or 0 (always 0 for a guest). Only above 0, draw thepictures-fieldcomponent with it asmax, and send the form asmultipart/form-data.- A comment form of your own must also draw
$restriction(new in 1.21.0) through therestriction-noticecomponent. Without it, a member who is restricted from commenting is told nothing. - For a guest (
form.guesttrue,csrfan empty string), draw the guest fields only fromform.fields({name: 'required', email: 'required'|'optional'}). Putform.stampin a hidden input namedstamp. Add an empty input namedform.trapthat is visually hidden, witharia-hidden="true",tabindex="-1"andautocomplete="off". - Call
captchaBox('comment')whenform.captcha_provideris set. It isrecaptcha_v2,recaptcha_v3orturnstile, elsenull.form.captchais the v2 site key only while v2 runs, so a component that draws the v2 box from it alone fails under the other two providers. See Sign-in, forms and consent. - Offer "Email me replies" only when
form.notifyis true. Offer Reply (and sendform.replyasparent) only whenform.repliesis true. Limit the body toform.max_lengthwhen it is notnull. - A guest's thread stays cacheable. A member's form carries the session token and takes the render out of the cache, as does a render that read
?reply=or?comment=. - Print the per-comment controls with
actions('comments.comment', $item)(see Actions and sections). - Report is not one of those controls. Print it with the
reportcomponent, as the last item of the comment's row of actions:$this->component('report', ['report' => $this->report('comments.comment', (int) $item['id']), 'noun' => 'comment']). Seereport()in Actions and sections. Since 1.20.0 core draws that row as a<div>, not a<p>, and Report is no longer in a menu at the comment's top right. A theme whose owncomponents/report.phpdrew only the menu version shows its Report at the end of that row.
Previews #
isPreview() and previewed($kind) #
isPreview() is whether an authorised draft preview is being rendered. It is for draft previews only: when staff browse the site in an unpublished theme (Site ▸ Themes), it stays false and comments still print. previewed($kind) is the draft row a preview shows in place of the stored one (page, post, menu or theme_options), or null on every other render. It is for a module's front route; one that does not ask renders as before.
<?php if ($this->isPreview()): ?><p>Draft preview</p><?php endif; ?>
A reviewer can preview a draft page or post on your theme. Core marks the changed top-level blocks of the body with data-draft-change="changed" or "added" and styles them in its own preview.css. Style [data-draft-change] yourself if you want your look. The preview blocks inline scripts and external images, so keep your page working without them.