Skip to content

Using Astroid Framework Library

Hitesh Aggarwal edited this page Sep 2, 2020 · 2 revisions

Introduction

Astroid\Framework is Astroid library namespace, provides you to access the Astroid Template and it's params at any level and in any extension. It also have objects like Document, Debugger and more. This class also have some useful functions which are:

  • To get Framework Version

    Astroid\Framework::getVersion();
  • To check client is Administrator

    Astroid\Framework::isAdmin();
  • To check client is Site

    Astroid\Framework::isSite();

Astroid\Framework::getDocument()

Returns (Astroid\Document) the current template document. Actually, this is not Joomla! Document (Factory::getDocument) but something is similar to that. You can add Meta data, Javascript, Stylesheet to the template at any point of code from any extension. You can create your custom template options and load from frontend folder with include function. See Overriding in Astroid.

Using Astroid Document

First get the document:

use Astroid\Framework as AstroidFramework;
$document = AstroidFramework::getDocument();

// or
 
$document = Astroid\Framework::getDocument();
  • To load a Stylesheet:

    $document->addStyleSheet($url)
  • To load CSS code:

    $document->addStyleDeclaration($css) // By default for all devices
    $document->addStyleDeclaration($css, 'tablet') // To add css for device width <= 991.98px
    $document->addStyleDeclaration($css, 'mobile') // To add css for device width <= 767.98px
  • To load a JavaScript file in head/body.

    $document->addScript($url) // By default load in <head>
    $document->addScript($url, 'body') // Add additional parameter `body` passed to load before </body>
  • To load JS code in head/body.

    $document->addScriptDeclaration($url) // By default load in <head>
    $document->addScriptDeclaration($url, 'body') // Add additional parameter `body` passed to load before </body>
  • To add Meta Tag in head.

    $document->addMeta($name, $content, $attribs = [])

    For example:

    $document->addMeta('twitter:card', 'summary_large_image')
    // <meta name="twitter:card" content="summary_large_image" />
    
    // or
    
    $document->addMeta('', 'article', ['property' => 'og:type']);
    // <meta property="og:type" content="article" />
  • To add Custom Tag into head/body.

    $document->addCustomTag($content, $position); // position can be head or body
  • To add Link Tag into head.

    $document->addLink($href, $rel, $attrbs);

    For Example:

    To add <link rel="icon" href="icon.png" type="image/png" sizes="16x16"> into head.

    $document->addLink('icon.png', 'icon', ['type' => 'image/png', 'sizes' => '16x16']);
  • To load a Layout (Frontend Partial).

    To load a partial layout, See Overriding in Astroid for more detail. This function look up following folder locations to load desired layout.

    1. templates/ACTIVE_TEMPLATE/html/frontend
    2. libraries/astroid/framework/frontend
    $document->include($name, $data = [], $return = false);
    // $name is name of file
    // $data is data pass to the file
    // $return is flag for data should be return or print as well

    For Example:

    To add <link rel="icon" href="icon.png" type="image/png" sizes="16x16"> into head.

    $document->addLink('icon.png', 'icon', ['type' => 'image/png', 'sizes' => '16x16']);
  • To load Font Awesome.

    $document->loadFontAwesome();
  • To check Development Mode.

    $document->isDev(); // return true or false
  • To render a Module Position anywhere.

    $document->position($position, $style = "none");
  • To render a loadposition and loadmoduleid anywhere.

    $document->loadModule($content); // will find and render all {loadposition} and {loadmoduleid}

Astroid\Framework::getTemplate()

Returns (Astroid\Template) the current template object. This is not Joomla! Template (Factory::getApplication()->getTemplate()) but something is similar to that. The object has all info and params of active site template. You can get Astroid template options at any point of website in any extension.

Getting Astroid Template Options

First get the template:

use Astroid\Framework as AstroidFramework;
$template = AstroidFramework::getTemplate();

// or
 
$template = Astroid\Framework::getTemplate();

Then get params:

$params = $template->getParams();
// $template->params will be protected property in future, Please do not use this directly

$params will be a Joomla! Registry class object. So, now you can get any param's value as like below:

$value = $params->get('key', 'default value');