Alberto Ruiz

Custom template variables

Blog Post created by Alberto Ruiz on Oct 15, 2015

There are a few template variables you can use to render platform, user or course information dynamically in contents, LTI custom params and other places. This is quite powerful, but it can be better...

 

With a custom template variable Building Block you can define new template variables to render virtually anything in your contents, from your Blackboard Learn instance using the API or taking data from the data base, or from an external system using web services, external data bases, etc... You can even render not only strings, but even html snippets to add dynamically, for instance, search forms from your Library service.

 

This is made by creating an extension Building Block, for what you have to include :

 

 <webapp-type value="javaext" />

 

in your bb-manifest.xml file.

 

You also have to declare in the same file your custom context handler, defining your extension with:

 

 <extension-defs>
   <definition namespace="blackboard.platform">
    <extension id="customContextHandler"
      point="blackboard.platform.contextHandler"
      class="es.elearningmedia.services.CustomContextHandler"
      singleton="false" />
  </definition>
 </extension-defs>

 

Then, in your source code, you create your custom context handler:

 

package es.elearningmedia.services;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import blackboard.persist.BbPersistenceManager;
import blackboard.platform.context.Context;
import blackboard.platform.context.ContextEntry;
import blackboard.platform.context.ContextHandler;
import blackboard.platform.security.EntitlementList;
import blackboard.platform.security.Entitlements;
import blackboard.platform.security.SecurityContext;
import blackboard.util.resolver.Resolver;

//Extends ContextHandler class to include our variable resolver
public class CustomContextHandler implements ContextHandler {

  public CustomContextHandler() {
  }

  public Entitlements getEffectiveEntitlements(Context ctx) {
       return new EntitlementList();
  }

  public Entitlements getRestrictedEntitlements(Context ctx) {
       return null;
  }

  public List<SecurityContext> getSecurityContexts(Context unused) {
       return new ArrayList<SecurityContext>();
  }

  // Here is were we inyect our custom resolver
  public List<ContextEntry> resolveKeys(HttpServletRequest request,
  BbPersistenceManager unused) {
       CustomResolver resolver = new CustomResolver(request);
       Resolver.attachResolverToContext(resolver);
       return new LinkedList<ContextEntry>();
  }

}

 

And now implement the custom resolver who contains the logic to show what you want where you insert your template variable:

 

package es.elearningmedia.integracionbiblioteca.services;

import javax.servlet.http.HttpServletRequest;

import blackboard.platform.context.ContextManagerFactory;
import blackboard.util.resolver.ResolverComponent;

public class CustomResolver implements ResolverComponent {

  private static HttpServletRequest _request = null;

  public CustomResolver(HttpServletRequest request) {
       _request = request;
  }

  public String[] getKeys() {

       return (new String[] { "variable_key" }); // the first part of the
                                                    // template variable (@X@
                                                    // tag) - variable_key in
                                                    // @X@variable_key.show@X@
  }

  public String resolve(String method, String attributes[]) {
       String varout = null;

       if ("show".equalsIgnoreCase(method)) { // the second part of the @X@
                                                 // tag - show in
                                                 // @X@variable_key.show@X@
  
       try {

          ContextManagerFactory.getInstance().setContext(_request);
          String uriPrefix = Utils.getUriPrefix();
          // the code for inserting
          // the Unika search box
          varout = "Whatever you whant to show in your template variable <div></div><script></script>...";
  
          return varout;

       } catch (Exception e) {

       } finally {

          ContextManagerFactory.getInstance().releaseContext();

       }
  }

       return null;

}


}

 

Now typing @X@variable_key.show@X@ in your contents you will get the rendered html code you put in varout.

 

This technique has been very useful to me in different projects, so I hope this helps to somebody else.

Outcomes