Hook: on_template_load
avatar
Señor FAQ

¡Hola, amigos! I’m Señor FAQ, the mustached maestro of questions and answers! With my trusty glasses and a book of endless wisdom, I turn dudas into solutions. Soy el héroe de los curiosos and the champion of clarity.


Hook: on_template_load

The on_template_load hook is executed during the template loading process in JScms. It is particularly useful for plugins or custom logic that need to interact with page data, perform specific actions, or manipulate the rendering process based on the current page or user context.

Usage


$hookResults = DynamicHooks::executeHook(
    'on_template_load',
    false,
    $jakdb, $jkv, $lang, $userGroupId, $page, $page1, $pageId, $jscmsLanguage
);

foreach ($hookResults as $namespace => $result) {
    if (is_callable($result)) {
        $result($jakdb, $jkv, $lang, $userGroupId, $page, $page1, $pageId, $jscmsLanguage);
    }
}
    

This hook is executed using DynamicHooks::executeHook() with the following parameters passed to the registered callbacks:

  • $jakdb - The database instance.
  • $jkv - The configuration array.
  • $lang - The active language for JScms.
  • $userGroupId - The ID of the current user's group.
  • $page - The current page slug.
  • $page1 - Additional page parameter (e.g., subpage or article ID).
  • $pageId - The ID of the current page.
  • $jscmsLanguage - The language context for JScms.

Purpose

The on_template_load hook is ideal for scenarios where plugin-specific logic or dynamic actions need to be applied during the template loading phase. It supports tasks like dynamic page routing, content injection, or serving custom endpoints like RSS feeds.

Example Implementation


DynamicHooks::addHook('on_template_load', function ($jakdb, $jkv, $lang, $userGroupId, $page, $page1, $pageId, $jscmsLanguage) {
    // Check if the page corresponds to the FAQ slug
    if (!empty($jkv["slugforfaqarticle"]) && $page === $jkv['slugforfaqarticle'] && isset($pageId) && isset($page1)) {

        // Initialize DynamicFAQ class
        $faqClass = new DynamicFAQ($jakdb, $jkv, $lang, $userGroupId);

        // Fetch the article data
        $article = $faqClass->getFAQById($page1, $jscmsLanguage);

        // Redirect if no article is found
        if (!$article) {
            JScms_redirect(DynamicRewrite::parseUrl($jkv["slugforfaq"]));
        } else {
            $articleData['title'] = $article['question'];
            $articleData['seo_keywords'] = $article['seo_keywords'];
            $articleData['seo_description'] = $article['seo_description'];
        }

        $pageToLoad = $pageId;
    }

    // Serve an RSS feed for a specific endpoint
    if ($_SERVER['REQUEST_URI'] === '/faqrss.xml') {
        $dynamicFAQ = new DynamicFAQ($jakdb, $jkv, $lang, $userGroupId);
        header('Content-Type: application/rss+xml; charset=utf-8');
        echo $dynamicFAQ->generateRssFeed();
        exit;
    }
}, 'faq');
    

Best Practices

  • Use descriptive and unique namespaces for your hooks to avoid conflicts.
  • Perform checks for page-specific logic to prevent unintended actions on unrelated pages.
  • Keep the callback logic efficient to maintain fast page load times.

Troubleshooting

  • If the hook does not execute, verify that it is registered correctly with the appropriate namespace.
  • Ensure that the callback parameters match the expected data types.
  • Debug using logs to identify any issues in the callback execution.
Was this article helpful?
0 out of 0 found this helpful