Partials and functions

Partials

Partials are optional PHP fragments in theme/{active}/partials/. They let you customise card HTML without touching core PHP functions.

render_article_card($article)
  ↳ Looks for theme/{active}/partials/article-card.php
      → Found    : uses the partial with injected variables
      → Not found: uses the default built-in rendering

No error is thrown if a partial is missing — the built-in fallback is used automatically.


partials/article-card.php

VariableTypeDescription
$articlearrayArticle data from the index: title, slug, date, tags, image, summary
$article_linkstringFull article URL (pre-calculated)
<article class="article-card">
    <?php if (!empty($article['image'])): ?>
    <div class="card-image">
        <a href="<?php echo $article_link; ?>">
            <img src="<?php echo getBaseUrl() . htmlspecialchars($article['image']); ?>"
                 alt="<?php echo htmlspecialchars($article['title']); ?>">
        </a>
    </div>
    <?php endif; ?>
    <div class="card-body">
        <h3><a href="<?php echo $article_link; ?>"><?php echo htmlspecialchars($article['title']); ?></a></h3>
        <p><?php echo get_article_summary($article); ?></p>
        <a href="<?php echo $article_link; ?>" class="read-more"><?php echo __t('read_more'); ?></a>
    </div>
</article>

partials/project-card.php

VariableTypeDescription
$projectarrayProject data
$project_linkstringFull project URL
$card_classstringPre-calculated CSS classes

Helpers available in partials

getBaseUrl()                         // Base URL with trailing slash
cleanUrl($type, $slug, $page, $cat) // Generate any CMS URL
url_slug($type)                      // Localised URL prefix
sanitizeSlug($string)                // URL-safe slug
format_date($date)                   // Date formatted per site settings
__t($key)                            // Translation via active locale
get_article_summary($article)        // Summary or auto-generated clean excerpt
_clean_excerpt($html, $length)       // Raw excerpt without shortcodes/HTML
loadSettings()                       // Site settings (use sparingly)

functions.php

Load order

functions.php (root)
  ├── core-functions.php
  ├── data-functions.php
  ├── theme-api.php → template-functions.php
  ├── data-layer.php
  └── lang-cache.php
theme/{active}/functions.php   ← your file, loaded here

What you can do

// Define helper functions
function mytheme_body_class(): string {
    $classes = ['theme-mytheme'];
    $type = $_GET['type'] ?? '';
    $slug = $_GET['slug'] ?? '';
    if (empty($type) && empty($slug)) { $classes[] = 'is-home'; }
    elseif (!empty($slug))             { $classes[] = 'is-single'; }
    else                               { $classes[] = 'is-list'; }
    return implode(' ', $classes);
}

// Pre-fetch sorted data
$GLOBALS['sorted_articles'] = [];
$__articles = sl_load_index('article');
if (!empty($__articles)) {
    usort($__articles, fn($a, $b) => strcmp($b['date'] ?? '', $a['date'] ?? ''));
    $GLOBALS['sorted_articles'] = $__articles;
}
unset($__articles);

ℹ️

Use slloadindex('article') rather than $GLOBALS['data']['article'] in functions.php — at that point $GLOBALS['data'] may not yet be defined.


What you cannot do

  • Redefine a CMS function (render_article_card, cleanUrl, etc.) — PHP will throw a fatal error. Use partials instead.
  • Use template variables ($item, $pageTitle, etc.) — they do not exist yet when functions.php is loaded.