Learn how Scrivito CMS can help you deliver amazing digital experiences
See Scrivito CMS in action

Fetching and Saving CMS Field Content

Fetching and Saving CMS Field Content

Fetching CMS field content: scrivito("content")

The scrivito("content") function can be called on any jQuery instance, but it expects that the jQuery instance contains a single DOM element that was created by scrivito_tag. An error is thrown if the function is applied to elements not generated with scrivito_tag. If the jQuery instance contains no elements or more than one element, an error will be thrown, too.

The function returns the current value of the CMS field to which the DOM element relates. scrivito("content") always returns what has been passed to scrivito("save") most recently.

Currently, scrivito("content") supports fields of the string, text, html, date, enum, and multienum type. The linklist type is not supported.

Saving CMS field content: scrivito("save")

$(“...”).scrivito(“save”, content) --> Promise

The scrivito("save") function can be called on any jQuery instance, but it expects that the jQuery instance contains a single DOM element that was created using scrivito_tag. An error will be thrown if elements not generated by means of scrivito_tag are detected. If the jQuery instance is empty, the call will be silently ignored. If the jQuery instance contains more than one DOM element, an error will be thrown.

The function must be called with a content parameter whose format fits the type of the attribute being set. The format of the attribute values is identical to the format used in the Ruby API with a couple of additional possibilities:

For binary attributes, you may pass in a JavaScript Blob or File object. The latter inherits from the former. The object is automatically uploaded to the CMS.

For date fields, you may also use the Date class provided by JavaScript.

The function returns a jQuery Promise. Once the content has been successfully saved, the promise will be fulfilled (without passing any additional data). If the save operation fails, the promise will be rejected with a single parameter, an object containing a message key whose value is an error message string.

Use the function as follows:

$(".my_dom_element").scrivito("save", "New Content").then( function() { // content was successfully saved. }, function(error) { // content could not be saved due to an error, see `error.message` } );

Note that you do not need to build your own error messages for every invocation of scrivito("save"). In-place editing will report the error to the user automatically. The handler for a rejected promise should only be used for any additional code you would like to run in case of an error.