Download the PHP package silverbackis/common-js-bundle without Composer
On this page you can find all versions of the php package silverbackis/common-js-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download silverbackis/common-js-bundle
More information about silverbackis/common-js-bundle
Files in silverbackis/common-js-bundle
Package common-js-bundle
Short Description Silverback IS Common Javascript Bundle
License MIT
Informations about the package common-js-bundle
Common JS Bundle
Not affiliated with The CommonJS group
This bundle provides twig functions to easily add commonly used Javascript tags & SDKs to any page.
By default this bundle will support
- Google Analytics
- Google Tag Manager (GTM)
- Facebook Javascript SDK
- Twitter for Websites
Requirements
This bundle is only configured and tested to be used for Symfony >=3.4
All default configurations and examples assume you are using Symfony Flex.
Installation
This bundle will be submitted to the Symfony Flex contrib repository shortly with some example configs. If you're using flex, the bundle will automatically be added into yor bundles.php file though.
Enable the Symfony Flex Recipes Contrib Repository
Until merged set the SYMFONY_ENDPOINT env var:
Install the bundle:
Until merged, unset the environment variable:
Getting Started
Configuration
The default configuration values will work for 90% of use cases if you simply set the environment variables (when required). Other scripts will be enabled by default.
However, you may wish to create a config file to add in default blocks:
default_blocks
can be an array of blocks you want. You can also include parameters if you want. It will pre-populate blocks in the order provided so you can just write {{ cjs_js('name') }}
in your twig template
You cannot pass the
default_blocks
variable in via the twig template, but you can still remove and modify the default blocks from the template usingcjs_add_block()
andcjs_remove_block()
functions
Identifiers will be added as environment variables by Flex into your .env file.
Set the IDs for these services if you want to enable them. Otherwise they will be disabled.
Example usage
Javascript blocks can be configured directly from your twig templates using the following functions
at_block_name
can be"false"
(note this is a string) which will result in the function returning the javascript for you to insert. You may which to track a click event for example when a user clicks a link instead of in the main tracking code.
You can duplicate an SDK block including any blocks that have already been configured
You can also remove a block (e.g. if you've duplicated a block but want to remove a specific section )
To generate a new model to be inserted as a parameter you can use cjs_model
- this example shows how you can create your model with arguments to define variables and/or using setters and then how you would inject the model as a parameter into a block.
Finally to output the scripts after all the blocks have been configured for a given block use the cjs_js
function
If there is an noscript fallback specified, you will want to output this at a different section in your template. This is implemented when using GTM for example. Yuu can output the fallback HTML using cjs_html
Models
There are models available for some SDK blocks (e.g. Google Analytics Event). You can use these to easily construct and pass data to a block. All models allow you to define all the variables in the constructor (in the order they are documented here) and also have getters and setters. Examples are provided in the individual script docs.
Supported Javascripts Docs
Google Analytics
Google Tag Manager (GTM)
Facebook SDK
Contributing a new Javascript
Adding an SDK is pretty straight forwards.
- A new Provider is required in
CommonJsBundle\Provider\Sdk
extendingCommonJsBundle\Provider\BaseProvider
. - In the dependency injection new configuration parameters will be required
- Models can be added in the
CommonJsBundle\Model\PascalCaseName
namespace where PascalCaseName is the name of the new javascript in PascalCase. - Template blocks are added in
Resources/views/blocks/snake_case_name
with aninit.html.twig
file and then a sub directoryjs
for all blocks needed.
All versions of common-js-bundle with dependencies
symfony/framework-bundle Version ^3.4 || ^4
symfony/twig-bundle Version ^3.4 || ^4
symfony/serializer Version ^3.4 || ^4
symfony/property-access Version ^3.4 || ^4
symfony/validator Version ^3.4 || ^4
symfony/console Version ^3.4 || ^4