Skip to main content

Template Retrieval Functions

getTemplate Function (optional)

Introduction

The getTemplate function is an important method used in theme creation when building a customized site. It is utilized within the createTheme function to define template assignment logic for different page types.

Syntax

The getTemplate function has the following syntax:

getTemplate: (pageData, themeSettings) => {
// Custom behavior here
};

Parameters

The getTemplate function takes the following parameters:

  • pageData (optional): An object containing data specific to the current page. This parameter is mainly used when custom logic requires access to page-specific information.
  • themeSettings (optional): The themeSettings object represents the configuration options for the theme.

Usage

The getTemplate function is typically assigned to a specific page type within the pageTypes object of the theme configuration. By providing a custom implementation for getTemplate, developers can override the default template assignment logic for specific page types.

Example

Consider the following example of a createTheme function call:

import { getArticleTemplateByName } from '@orgnc/core/lib/themes/templates/article';
import DefaultTemplate from 'path/to/DefaultTemplate';
import CustomTemplate from 'path/to/CustomTemplate';

const theme = createTheme({
pageTypes: {
article: {
templates: [
{ name: 'default', component: DefaultTemplate },
{ name: 'customTemplate', component: CustomTemplate },
],
getTemplate: (article) => {
if (someCustomLogic) {
// Custom template assignment logic
return getArticleTemplateByName(theme, 'customTemplate');
}

// Use default logic
return getArticleTemplateByName(theme, 'default');
},
},
// Other page types...
},
});

In this example, the article page type has a custom getTemplate function defined. When a page of type article is rendered, the getTemplate function will be invoked to determine the appropriate template to use for that page.

The getTemplate function can utilize custom logic specific to the page type and access the provided article object and the theme object, if needed. Depending on the requirements, developers can implement different behaviors, such as selecting a template based on specific conditions or dynamically generating the template based on the provided data.

If the custom logic does not apply or no specific template is assigned, the getTemplate function should fall back to default logic or use a default template provided by the theme.

// Use default logic
return getArticleTemplateByName(theme, 'default');
info

You can find the default template getters for each page type at:

caution
import { getArticleTemplateByName } from '@orgnc/core/lib/themes/templates/article';
import { getPageTemplateByName } from '@orgnc/core/lib/themes/templates/page';
import { getCategoryTemplateByName } from '@orgnc/core/lib/themes/templates/category';
import { getTagTemplateByName } from '@orgnc/core/lib/themes/templates/tag';
import { getAuthorTemplateByName } from '@orgnc/core/lib/themes/templates/author';