addToViewContextDefinition

addToViewContextDefinition( view_id, property_name, type, callback )

Adds a property to the context data of a view. The property can then be referenced in templates. If the property already exists in the context data of the view, it is updated. For example, if a view has a property called ‘thumbnail’ and you pass ‘thumbnail’ in the property_name argument, then the return value of the callback function overwrites the current value of ‘thumbnail’.

var layout = container.getComponent('Layout');

layout.addToViewContextDefinition('Header.View', '48HourShippingMessage', 'string', function(context) {
  return 'Hello' + context.profileModel.firstname + '. Free 2 day shipping on orders over 40 USD.';
});

In the above example, the first and second arguments indicate the view, ‘Header.View’, to which the property, ’48HourShippingMessage’, will be added. The function returns a message as a string, with the first name of the logged in user and the message text. To display the message in the view, add the property to the template with the expression {{48HourShippingMessage}}.

<nav class="header-main-nav">
	<div>{{48HourShippingMessage}}</div>
	<div id="banner-header-top" data-cms-area="header-top"></div>
</nav>

Leave a comment

Your email address will not be published. Required fields are marked *