Sometimes you may have processing that you would like to perform at the beginning of each page load. This can be done by entering a startup class within site_settings. Normally this class will be stored within the site's classes folder. 

Here we see an example of a startup class definition.

Startup class definition

Notice that you will normally define a site object as a class variable and this must have a simple set method generated. This method will be called automatically to pass the main site object to the class.

The class must define a method called setup. This will be called during the page load process.

Let's review what is happening in the sample code.

This same site is hosted at two different domains, one with a .com extension and one with a .ca extension. First we call the $site function programmatically to request the top level domain.

$tld = $this->site->dollar_function('site','tld');

Next we use the tld to set the English locale to either CA or US. This will be used by the $locale references used on the site. For example, we can render the word color on the .com site and colour on the .ca site. We also add the locale to the html lang property.

$locale = ($tld === 'ca') ? 'CA' : 'US';

Next we make sure that the codeframe that is loaded includes a linktags object. For example, this same request could be used to generate email content which does not generally define linktags.

$linktags = $this->site->layout->get_object('linktags');
if ($linktags !== false) {

The remainder of the code is used to set the correct Google Analytics key, depending on the URL as well as cause the following link alternate tags to be generated:

$current = $this->site->get_current_url();
if ($locale === 'CA') {
	$ga = 'UA-12345678-1'; // .ca
	$ca_link = $current;
	$us_link = str_replace('','',$current);
else {
	$ga = 'UA-12345678-2'; // .com
	$us_link = $current;
	$ca_link = str_replace('','',$current);
if ($us_link !== $ca_link) {  // Don't set in the sandbox
	$linktags->add_link_tag('alternate',$ca_link,array('hreflang' => 'en-ca'),'en_ca');
	$linktags->add_link_tag('alternate',$us_link,array('hreflang' => 'en-us'),'en_us');
	$linktags->add_link_tag('alternate',$us_link,array('hreflang' => 'en'),'en');

This is a good example of how you can:

  1. Call dollar functions programmatically
  2. Interact with layout objects defined in the codeframe
  3. Add linktag elements programmatically
site_setting Specification
site_setting Defaults