A snippet is any piece of HTML code to add to the site. You have likely often added to site pages the tracking code of a search engine, a social network button, a validation meta tag, or code to activate a chat.
With snippets you can perform these operations directly from the app with JavaScript without modifying the HTML template pages, except for adding a simple tag in the position where you want the snippet to appear.
Before using snippets you must enable them in the back office:
Then add Admin SDK to the app HTML pages:
<script src="https://open2b.dev/admin-sdk/open2b-admin-sdk-v7.8.min.js"></script>
Creates a snippet or replaces an existing one with the same code:
| Parameter | Description | ||||||
|---|---|---|---|---|---|---|---|
code |
Snippet identifier code. It can be 1 to 32 characters long, letters and numbers, and must match: /^[a-zA-Z][a-zA-Z0-9]{0,31}$/. |
||||||
position |
Snippet position in pages. It can be:
|
||||||
content |
Snippet content. This is the static content, generally in HTML format, that will be inserted as-is into the page. | ||||||
templateFiles |
Template files to add the snippet to. Template files are all files with the
.html extension found in the template root folder. See also the Template Manual for the list of files commonly present in a template.Use |
In the following example a snippet is added to the "department.html" and "product.html" pages before the </head> tag.
var html = '<style type="text/css"> h1 { color: blue; } </style>';
Admin.Snippets.create('BlueHeader', 'Head', html, [ 'department.html', 'product.html' ], 'Yes', function(error) {
if ( error != null ) { alert(error); return; }
// done
});
Removes all app snippets.
Admin.Snippets.clear(function(error) {
if ( error != null ) { alert(error); return; }
// done
});
Removes snippets with the specified codes.
Admin.Snippets.remove([ 'BlueHeader', 'Chat' ], function(error) {
if ( error != null ) { alert(error); return; }
// done
});
Returns snippets with the specified codes. Use null instead of the codes list to return all snippets.
Admin.Snippets.get([ 'BlueHeader', 'Chat' ], function(snippets, error) {
if ( error != null ) { alert(error); return; }
alert('BlueHeader: '+snippets.BlueHeader);
alert('Chat: '+snippets.Chat);
});
You can add up to 1,000 snippets per app. The HTML source of a snippet can be up to 50,000 characters long and, in any case, the sum of the HTML source lengths of all snippets in an app cannot exceed one million characters.