Creating Widgets
From Snapp CMS Developer Documentation
Contents |
Note: Widgets in the global library have priority over widgets in the
custom folder if the same name is used
There are two parts to a widget
1) Configuration File (only required if widget is to be used in the user generated content)
2) Widget Class
Rules for Creating Widgets
- A Widget shall never output code directly
- Widget class names shall be prefixed with "widget"
- The name of the file must match the class name
- Always write a with a few required configuration options as possible
- Always pre-define and many internal variables as possible
- Widget should only output basic html structure allowing for css styling
Configuration File
The configuration file is a simple ini file that contains all the possible configuration options that a user and access while adding the widget to a piece of content
A widget configuration ini will contain the following information
| Config Options | Description |
|---|---|
| widget : string (required) | PHP class name of widget |
| title : string (required) | A short description or Name of Widget |
| description : string (required) | A description of what the widget does |
| [Basic] : INI Section (required) | A simplified set of configuration options for most users |
| [Advanced] : INI Section (required) | Advanced or Complete set of configuration options or an advanced user |
For a list of pre-defined or special configuration options Click Here
Example Configuration INI File
e.g. widgetContent.ini
widget="Content" title="Insert Content" description="Insert existing piece on content @ this location [Basic] content= [Advanced] content= class=
Widget Class
The widget class are free form class that the developer can customize to what ever they need.
With the exception of that there are two functions that must be included in every widget
| Config Options | Description |
|---|---|
| __construct($cfg=' ') | The function is passed all the configuration option for the widget updating it |
| output() | This function generated the html output an returns the output as a string |
Example Class
e.g. widgetContent.php
<?
class widgetContent {
private $id = 0;
private $class = ;
private $showTitle = true;
public function __construct($cfg) {
if (is_string($cfg)) {
$str = '{'.stripslashes(str_replace("'",'"',$cfg)).'}';
$cfg = json_decode($str);
}else if (is_numeric($cfg)) {
$cfg = array('id'=>$cfg);
}
if (is_array($cfg) || is_object($cfg)) {
foreach ($cfg AS $name=>$value){
switch ($name) {
case 'id': $this->id = (int)$value; break;
case 'class': $this->class = $value; break;
case 'showTitle': $this->showTitle=(bool)$value; break;
}
}
}
}
public function output() {
$output = array();
if ($this->id > 0) {
$content = new moduleContent($this->id);
$output[] = '<div '.(isset($this->class)?'class="'.$this->class.'"':).'>';
if (!empty($content->title) && $this->showTitle == true) {
$output[] = '<h2>'.$content->title.'</h2>'.chr(10);
}
$output[] = $content->content.chr(10);
$output[] = '</div>'.chr(10);
}
return implode(chr(10),$output);
}
}
?>
