Snippets

Appzio snippets are a way to wrap commonly used code, centralize it's logic in one place and avoid repetition.

Creating Snippets


Deprecated: Function create_function() is deprecated in /var/www/docs.appzio.com/public/wp-content/plugins/wp-gfm/markdown.php on line 301

Deprecated: Function create_function() is deprecated in /var/www/docs.appzio.com/public/wp-content/plugins/wp-gfm/markdown.php on line 302

In your application you may have places where you’ll want to repeat a set of code. In order to avoid copying and code repetition Appzio provides you with the ability to create snippets and have a centralized place for storing commonly used code. Snippets are classes stored in aelogic/article/component/snippets and they must extend the basic ArticleComponent class. The only method that must be implemented is the template() method. It will be automatically called whenever you are using your snippet.

class ArticleCustomSnippet extends ArticleComponent
{
    public function template()
    {
        ...
    }
}

The snippet class does not extend the Article Controller, therefore you cannot access it’s methods directly. In order to attach elements to the scroll and use the methods that you’re using in your regular controllers and views you will need to do so through the factoryobj property which is an instance of the article controller.

$this->factoryobj->getHairline('#DADADA');
$this->factoryobj->getRow(array(
    ...
));

 

Using Snippets


Deprecated: Function create_function() is deprecated in /var/www/docs.appzio.com/public/wp-content/plugins/wp-gfm/markdown.php on line 301

Deprecated: Function create_function() is deprecated in /var/www/docs.appzio.com/public/wp-content/plugins/wp-gfm/markdown.php on line 302

Once your snippet class is created there are two ways to call it from your regular views or controllers.  First one is to use the getSnippet() method on the ArticleController. It takes the identifier of the snippet together with a list of parameters that should be sent to it. On the snippet itself, those parameters are available from the options array property.

$this->getSnippet('customsnippet');

The identifier of the snippet is the name of the class with all lowercase letters. Another way to call your snippet is to create a dedicated method for it. This is a common practice and you will often see it in the code. Here’s an example:

public function moduleGallery($params = array()) {
    return $this->returnComponent('gallery', 'module', false, $params);
}

This is just a wrapper function that you can use if you want your code to be more verbose and provide better clarity of what you are doing. If the snippet is going to be used in more than one application you should separate it into it’s own method.