View snippets

View snippets allow embedding dynamic JSP content into the shop frontend.

View snippets are configured using the configureViewSnippetMapping method in the configurer of your plugin.

It is necessary to define on which page and in which location a jsp file is embedded. A location is a placeholder in the novomind iSHOP. The snippet is rendered into the placeholders only at the specified pages. To render a snippet on all pages you can use the method onAllPages(). Please check the interface for all available options.

// Show the twitter.jsp at the page end, but only on product pages
configurer.withViewSnippet("social/twitter.jsp").onProductPage().onPageEnd();
//Show the footer.jsp on every page at the end
configurer.withViewSnippet("footer.jsp").onAllPages().onPageEnd();

All view snippets are located in the folder META-INF\resources\plugin\plugin-helloworld, where plugin-helloworld is the name of your plugin.

There is the useful plugin ishop-plugin-devtool to display all standard locations in a novomind iSHOP.

Using models

A model can be passed to the JSPs to supply extra data.

In the configurer a modelBuilder can be registered with the withModel method.

configurer.withViewSnippet("content.jsp").onHomePage().onBelowBody().withModel(new SocialModelBuilder());

The model builder has to implement the ModelBuilder interface. The interface requires to implement the method createModel(ViewSnippetContext arg0) which is called every time the specified view snippet is rendered. Depending on where the snippet is rendered there may also be some information available in the ViewSnippetContext, like the product or the category.

public class SocialModelBuilder implements ModelBuilder {
  @Override
  public Object createModel(ViewSnippetContext context) {
    return new SocialModel("myApiKey", 123);
  }
}

The model is just a simple getter class.

public SocialModel() {
  public SocialModel(String apiKey, int productTrackingId);
  public String getApiKey();
  public int getProductTrackingId();
}

The modelTag makes the passed model accessible in a variable.

<%@ taglib uri="http://www.novomind.com/ishop/api" prefix="api" %>
<api:model var="socialModel" />

API-KEY: <c:out value="${socialModel.apiKey}" />
Tracking Id: <c:out value="${socialModel.productTrackingId}" />

Including resources

The ResourceTag of the ishop-api tag library helps you embed static resources like images or javascript and css files into a view snippet.

Conditional rendering

You can define that the view snippet will only be rendered if a condition is met. In this case you have to implement the ViewSnippetCondition and add it to the configuration call.

configurer.withViewSnippet("conditionApplies.jsp").onAllPages().onAboveBody().when(customCondition).withModel(socialModelBuilder);

Example: You want to display some content on german web sites only. Therefore you have to create a ViewSnippetCondition checking the current country. Every ViewSnippetCondition has to implement the apply method to check if your condition is met.

@Service
public class CountryDECondition implements ViewSnippetConfigurer.ViewSnippetCondition {

  private final ApiShopContext apiShopContext;
  private static final String country = "DE";

  @Inject
  public CountryDECondition(ApiShopContextFactory apiShopContextFactory) {
    this.apiShopContext = apiShopContextFactory.getDefaultShopContext();
  }

  @Override
  public boolean apply() {
    return country.equalsIgnoreCase(apiShopContext.getMcs().getCountry());
  }
}

This condition needs to be registered in the PluginConfigurer#configureViewSnippetMapping method. You can simply add the when method to the withViewSnippet call. We will do the rest for you.

@Configuration
@ComponentScan(basePackageClasses = DefaultPluginConfigurer.class)
public class DefaultPluginConfigurer extends PluginConfigurerAdapter {
  @Inject
  CountryDECondition countryDECondition;

  @Override
  public void configureViewSnippetMapping(final ViewSnippetConfigurer configurer) {
    configurer.withViewSnippet("locale_DE_specific.jsp").onAllPages().aboveBody().when(countryDECondition);
  }
}