ripaclub/zf2-hanger-snippet

一个将HTML/JS代码片段附加到ZF2应用程序的模块

v1.0.6 2015-06-05 13:48 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:00:24 UTC


README

Travis Packagist License Scrutinizer Scrutinizer Coverage

本ZF2模块旨在提供一个快速配置和添加JS库代码片段的方法。

理由

常常需要设置一个JavaScript库,很多时候很希望有一个地方可以配置它们,也许还有能力覆盖配置而不是直接在视图中抛出。

安装

通过Composer

ripaclub/zf2-hanger-snippet 添加到您的composer.json文件中

{
   "require": {
       "ripaclub/zf2-hanger-snippet": "~1.0.6"
   }
}

设置

在布局中的body关闭标签之前

<?php echo $this->hangerSnippet(); ?>

可选的,如果您需要添加更多位置

<?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
                ],
            ],
        ],
    ],
];

别忘了将 HangerSnippet 模块添加到您的 application.config.php 文件中。

'modules' => [
        // ...
        'HangerSnippet',
        'Application',
],

内置代码片段

Google Analytics

配置:

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
                ],
            ],
        ],
    ],
];

Facebook JavaScript SDK

配置:

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',
                ],
            ],
        ],
    ],
];

Google no CAPTCHA reCAPTCHA

配置:

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' => '...'
                ],
            ],
        ],
    ],
];

与其它不同,Google ReCaptcha代码片段的位置需要指定(不能简单地附加到页面上)。

将此代码片段放置到您需要的位置 ...

<?php echo $this->hangerSnippet()->render('google-nocaptcha-recaptcha'); ?>
注意

字符串 google-nocaptcha-recaptcha 是此代码片段位置的默认名称(见 module.config.php)。

Analytics