This ZF2 Module aims to provide a fast way to configure and append code snippeds for JS libraries.
Often happens to have to set up a Javascript library and many times it would be nice to have a place where you can configure them and perhaps with the ability to override the configuration rather than throw them in the view.
Add ripaclub/zf2-hanger-snippet
to your composer.json
{
"require": {
"ripaclub/zf2-hanger-snippet": "~1.0.6"
}
}
In your layout before the body closing tag
<?php echo $this->hangerSnippet(); ?>
Optionally, if you need to add more placements:
<?php echo $this->hangerSnippet()->render('placementName'); ?>
return [
'hanger_snippet' => [
'enable_all' => true, //if not specified true by default
'snippets' => [
'snippet-name' => [
'config_key' => '', // Config node in the global config, if any, retrivied data will be merged with values then passed to the template
'template' => '', // Template script path, if not specified 'hanger-snippet/snippet-name' will be used
'placement' => '', // Placement identifier, if not specified the default placement will be used
'enabled' => true, // When not specified 'enable_all' value will be used
'values' => [
// Other values for the template
],
],
],
],
];
Do not forget to add HangerSnippet module to you application.config.php
file.
'modules' => [
// ...
'HangerSnippet',
'Application',
],
Configuration:
return [
'ga' => [
'monitoring_id' => 'UA-XXXXXXXX-X',
'domain' => 'yourdomain.com',
'anonymize_ip' => false, // refer to https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#anonymizeip for more information
'options' => [
'siteSpeedSampleRate' => 1,
'sampleRate' => 100
// refer to https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference for more options
],
],
'hanger_snippet' => [
'snippets' => [
'google-analytics' => [
'config_key' => 'ga', // the config node in the global config, if any
'values' => [
// other values for the template
],
],
],
],
];
Configuration:
return [
'facebook' => [
'appId' => '...',
],
'hanger_snippet' => [
'snippets' => [
'facebook-sdk' => [
'config_key' => 'facebook', // the config node in the global config, if any
'values' => [
'async' => false,
'status' => true,
'xfbml' => true,
'version' => 'v2.2',
],
],
],
],
];
Configuration:
return [
'grecaptcha2.0' => [
'uri' => 'https://www.google.com/recaptcha/api.js'
// Optional API parameters - see https://developers.google.com/recaptcha/docs/display
'parameters' => [
'render' => 'onload',
// 'hl' => '...',
// 'onload' => '...',
],
],
'hanger_snippet' => [
'snippets' => [
'google-nocaptcha-recaptcha' => [
'config_key' => 'grecaptcha2.0', // the config node in the global config, if any
'values' => [
'sitekey' => '',
// Optional configurations - see https://developers.google.com/recaptcha/docs/display
'theme' => 'light',
'type' => 'image',
'callback' => '...',
'expiredCallback' => '...'
],
],
],
],
];
The placement of Google ReCaptcha snippet, unlike the others, needs to be specified (it can not be simply appended to the page).
To place this snippet where you need it ...
<?php echo $this->hangerSnippet()->render('google-nocaptcha-recaptcha'); ?>
The string google-nocaptcha-recaptcha
is the default name of the placement for this snippet (see module.config.php
).