NIHVIVO-1563 Add template method models to help directive
This commit is contained in:
parent
d6ff3b05a6
commit
b323f50c04
4 changed files with 49 additions and 29 deletions
|
@ -50,8 +50,13 @@ public class DumpHelper {
|
|||
TemplateModel tm = null;
|
||||
try {
|
||||
tm = dataModel.get(varName);
|
||||
} catch (TemplateModelException tme) {
|
||||
log.error("Error getting value of template model " + varName + " from data model.");
|
||||
} catch (TemplateModelException e) {
|
||||
log.error("Error getting value of template model '" + varName + "' from data model.");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (tm == null) {
|
||||
log.error("No variable '" + varName + "' defined in data model." );
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -12,8 +12,8 @@ import org.apache.commons.logging.Log;
|
|||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.web.directives.BaseTemplateDirectiveModel;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.methods.BaseTemplateMethodModel;
|
||||
import freemarker.core.Environment;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.SimpleScalar;
|
||||
import freemarker.template.TemplateDirectiveBody;
|
||||
import freemarker.template.TemplateException;
|
||||
|
@ -40,27 +40,44 @@ public class HelpDirective extends BaseTemplateDirectiveModel {
|
|||
"The help directive doesn't allow nested content.");
|
||||
}
|
||||
|
||||
Object o = params.get("directive");
|
||||
Object o = params.get("for");
|
||||
|
||||
if ( o == null) {
|
||||
throw new TemplateModelException(
|
||||
"Must specify 'for' argument.");
|
||||
}
|
||||
|
||||
if ( !(o instanceof SimpleScalar)) {
|
||||
throw new TemplateModelException(
|
||||
"Value of parameter 'directive' must be a string.");
|
||||
}
|
||||
"Value of parameter 'for' must be a string.");
|
||||
}
|
||||
|
||||
String directiveName = ((SimpleScalar)o).getAsString();
|
||||
String name = ((SimpleScalar)o).getAsString();
|
||||
TemplateHashModel dataModel = env.getDataModel();
|
||||
Map<String, Object> dm = (Map<String, Object>) DeepUnwrap.permissiveUnwrap(dataModel);
|
||||
Object value = dm.get(directiveName);
|
||||
Object value = dm.get(name);
|
||||
|
||||
if (! (value instanceof BaseTemplateDirectiveModel) ) {
|
||||
if (value == null) {
|
||||
throw new TemplateModelException(
|
||||
directiveName + " must be the name of a directive.");
|
||||
"Value of parameter '" + name + "' must be the name of a directive or method");
|
||||
}
|
||||
|
||||
Configuration config = env.getConfiguration();
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
String help = ((BaseTemplateDirectiveModel) value).help(directiveName, env);
|
||||
|
||||
String help;
|
||||
String type;
|
||||
if (value instanceof BaseTemplateDirectiveModel) {
|
||||
help = ((BaseTemplateDirectiveModel) value).help(name, env);
|
||||
type = "directive";
|
||||
} else if (value instanceof BaseTemplateMethodModel) {
|
||||
help = ((BaseTemplateMethodModel) value).help(name, env);
|
||||
type = "method";
|
||||
} else {
|
||||
throw new TemplateModelException(
|
||||
"Value of parameter '" + name + "' must be the name of a directive or method");
|
||||
}
|
||||
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("help", help);
|
||||
map.put("type", type);
|
||||
|
||||
try {
|
||||
map.put("stylesheets", dataModel.get("stylesheets"));
|
||||
|
@ -69,25 +86,25 @@ public class HelpDirective extends BaseTemplateDirectiveModel {
|
|||
}
|
||||
|
||||
DumpHelper helper = new DumpHelper(env);
|
||||
helper.writeDump("help.ftl", map, directiveName);
|
||||
helper.writeDump("help.ftl", map, name);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String help(String name, Environment env) {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
|
||||
map.put("name", name);
|
||||
|
||||
map.put("effect", "Output help for directive or method.");
|
||||
map.put("effect", "Output help for a directive or method.");
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("var", "name of directive/method");
|
||||
params.put("for", "name of directive or method");
|
||||
map.put("params", params);
|
||||
|
||||
List<String> examples = new ArrayList<String>();
|
||||
examples.add("<@" + name + " directive=\"dump\" />");
|
||||
examples.add("<@" + name + " method=\"profileUrl\" />");
|
||||
examples.add("<@" + name + " for=\"dump\" />");
|
||||
examples.add("<@" + name + " for=\"profileUrl\" />");
|
||||
map.put("examples", examples);
|
||||
|
||||
return mergeToHelpTemplate(map, env);
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<#-- $This file is distributed under the terms of the license in /doc/license.txt$ -->
|
||||
|
||||
<#-- Template for displaying directive help -->
|
||||
<#-- Template for displaying help for a template directive or method -->
|
||||
|
||||
<div class="dump help">
|
||||
<h3>Template directive help</h3>
|
||||
<h3>Template ${type} help</h3>
|
||||
|
||||
${help}
|
||||
</div>
|
||||
|
|
|
@ -133,14 +133,12 @@ ${stylesheets.addFromTheme("/css/sstest.css", "/css/sstest2.css")}
|
|||
${scripts.addFromTheme("/js/jstest.js")}
|
||||
${scripts.add("/js/script1.js", "/js/script2.js", "/js/script3.js")}
|
||||
|
||||
|
||||
<@dumpAll />
|
||||
|
||||
<@help directive="dump" />
|
||||
<@help for="dump" />
|
||||
|
||||
<@help for="profileUrl" />
|
||||
|
||||
<@describe var="stylesheets" />
|
||||
|
||||
<@describe var="scripts" />
|
||||
|
||||
<@describe var="headScripts" />
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue