Skip to content

Creating a view

Alexander Söderberg edited this page Oct 1, 2017 · 5 revisions

Creating a view

Everything you need to get a view working, is to be found in the abstract class com.github.intellectualsites.iserver.api.views.View. Just extend that, and move from there.

Constructor

The constructor takes a view pattern, these are further explained here

public ExampleView() {
  super ( "(view)/[pattern])", "viewName" );
}

#passes

There is one method that allows you to reject a view, and thus control what you accept or not. This code is called if the requested URL matches your specified regex pattern.

@Override
public boolean passes(final Request request) {
  // This will only return true if the request was paired with a post
  // request, this is good for form specific views and alike.
  return request.getPostRequest() != null;
}

#generate

This is the method that actually generates the view. You are free to generate whatever content you want, and you can freely choose between sending text or raw bytes.

@Override
public Response generate(final Request r) {
  Response response = new Response(this /* Requires a parent view */);
  // This will tell the client that we are sending a CSS document  
  response.getHeader().set("Content-Type", "text/css; charset=utf-8");
  // This sets a text content body
  response.setContent("body { background-color: green; }");
  // Then we return our created response
  return response;
}

Register

In order for anyone to use your view (via config/views.yml), then you've got to register it. It's quite simple:

// The first parameter is the key that will be used to bind
// a view from the views.yml class
Server.getInstance().addViewBinding("example", ExampleView.class);

If you don't want to make your view configurable, or are building a private application, then you can register your view directly into the ViewManager

Server.getInstance().getViewManager().add(new ExampleView());

Default Views

The default views can be found in com.github.intellectualsites.iserver.api.views

Optional Interfaces

IgnoreSyntax

Implement this interface if you want to ignore the Crush syntax: com.github.intellectualsites.iserver.api.util.IgnoreSyntax

CacheApplicable

If you want your generated responses to be cached, then implement com.github.intellectualsites.iserver.api.cache.CacheApplicable. You have to implement one method:

@Override
public boolean isApplicable(final Request r) {
  // To allow all request to use the cached
  // response, just return true. If you want to
  // do additional checks, then return the value
  // based on those.
  return true;
}

Navigation

Templates

Configuration

Files can be found in .kvantum/config

Views

/commands

Development

Clone this wiki locally