Snippets

Snippets are pieces of code that are executed prior to the page being rendered. Typically you use snippets to do a task that outputs a HTML string. They are dynamic because you are generating the string on the fly.

Some ideas for snippets could be:

Snippet Storage

Snippets are stored in the /library/snippets folder. Every snippet has the following file format:
  filename.php.snippet

Creating A Snippet

Creating a snippet is pretty easy. You code up PHP function that returns a string. This function should be prefaced with QCMS_ so that we don't run into name collisions with other PHP software. So for example, a date and time snippet can look like this:

<?php
function QCMS_DateTime($type=0) {
switch($type) {
	case 0:
		$output = date("h:i");
		break;
	case 1:
		$output = date("h:i T");
		break;
	case 2:
		$output = date("H:i");
		break;
	case 3:
		$output = date("H:i T");
		break;
	case 4:
		$output = date("F j, Y");
		break;
	case 5:
		$output = date("F j, Y T");
		break;
	case 6:
		$output = date("l, F j, Y");
		break;
	case 7:
		$output = date("l, F j, Y T");
		break;
	case 8:
		$output = date("F j, Y h:i");
		break;
	case 9:
		$output = date("F j, Y h:i T");
		break;
	case 10:
		$output = date("F j, Y H:i");
		break;
	case 11:
		$output = date("F j, Y H:i T");
		break;
	case 12:
		$output = date("l, F j, Y h:i");
		break;
	case 13:
		$output = date("l, F j, Y h:i T");
		break;
	case 14:
		$output = date("l, F j, Y H:i");
		break;
	case 15:
		$output = date("l, F j, Y H:i T");
		break;
	default:
		$output = date("h:i");
		break;
}
return($output);
}
?>

Notice that it returns a string. It should not echo.

Using Snippets

You use snippets from your main.theme.php file or in your .php.content files.

Before you invoke it, you must include the snippet with the IncludeSnippet helper. Supply the snippet filename without the extension. In this example, we see there is a snippet called datetime.php.snippet. The name in this case is "datetime".

<?php
AddQlickFontItem("https://fonts.googleapis.com/css?family=Prata");
AddQlickFontItem("https://fonts.googleapis.com/css?family=Lato");

AddQlickCssItem("responsive.css");
AddQlickCssItem("styles.css");

IncludeSnippet("datetime");

// ============================================================================================================================
 
$qlick["snippet_datetime"] = QCMS_DateTime(15);

$qlick["pageHeaderContent"] = <<< PAGE_HEADER_CONTENT
<style>
  #logo {
    float: left;
  }
  #datetime {
    float: right;
  }
}
</style>
<img id="logo" src="/theme/images/logo-240x80.png" alt="Qlicksoft logo"/>
<div id="datetime">
  {$qlick['snippet_datetime']}
</div>
PAGE_HEADER_CONTENT;
?>

The code above is being used in qlicksoft.com for its page header. If you look at our website, you will see the date and time being shown in the upper right hand corner.

This also is a good example of using the <style> tag within a heredoc content zone.

To avoid walking over existing qlick variable keys, always preface the snippet variable slot with "snippet". In the example above this is "snippet_datetime".