NIHVIVO-1563 Add template method models to help directive

This commit is contained in:
rjy7 2011-02-22 20:05:45 +00:00
parent d6ff3b05a6
commit b323f50c04
4 changed files with 49 additions and 29 deletions

View file

@ -50,8 +50,13 @@ public class DumpHelper {
TemplateModel tm = null; TemplateModel tm = null;
try { try {
tm = dataModel.get(varName); tm = dataModel.get(varName);
} catch (TemplateModelException tme) { } catch (TemplateModelException e) {
log.error("Error getting value of template model " + varName + " from data model."); 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; return null;
} }

View file

@ -12,8 +12,8 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.web.directives.BaseTemplateDirectiveModel; import edu.cornell.mannlib.vitro.webapp.web.directives.BaseTemplateDirectiveModel;
import edu.cornell.mannlib.vitro.webapp.web.methods.BaseTemplateMethodModel;
import freemarker.core.Environment; import freemarker.core.Environment;
import freemarker.template.Configuration;
import freemarker.template.SimpleScalar; import freemarker.template.SimpleScalar;
import freemarker.template.TemplateDirectiveBody; import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException; import freemarker.template.TemplateException;
@ -40,27 +40,44 @@ public class HelpDirective extends BaseTemplateDirectiveModel {
"The help directive doesn't allow nested content."); "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)) { if ( !(o instanceof SimpleScalar)) {
throw new TemplateModelException( 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(); TemplateHashModel dataModel = env.getDataModel();
Map<String, Object> dm = (Map<String, Object>) DeepUnwrap.permissiveUnwrap(dataModel); 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( 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(); String help;
Map<String, Object> map = new HashMap<String, Object>(); 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");
}
String help = ((BaseTemplateDirectiveModel) value).help(directiveName, env); Map<String, Object> map = new HashMap<String, Object>();
map.put("help", help); map.put("help", help);
map.put("type", type);
try { try {
map.put("stylesheets", dataModel.get("stylesheets")); map.put("stylesheets", dataModel.get("stylesheets"));
@ -69,25 +86,25 @@ public class HelpDirective extends BaseTemplateDirectiveModel {
} }
DumpHelper helper = new DumpHelper(env); 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) { public String help(String name, Environment env) {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
map.put("name", name); 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>(); 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); map.put("params", params);
List<String> examples = new ArrayList<String>(); List<String> examples = new ArrayList<String>();
examples.add("<@" + name + " directive=\"dump\" />"); examples.add("<@" + name + " for=\"dump\" />");
examples.add("<@" + name + " method=\"profileUrl\" />"); examples.add("<@" + name + " for=\"profileUrl\" />");
map.put("examples", examples); map.put("examples", examples);
return mergeToHelpTemplate(map, env); return mergeToHelpTemplate(map, env);

View file

@ -1,9 +1,9 @@
<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> <#-- $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"> <div class="dump help">
<h3>Template directive help</h3> <h3>Template ${type} help</h3>
${help} ${help}
</div> </div>

View file

@ -133,14 +133,12 @@ ${stylesheets.addFromTheme("/css/sstest.css", "/css/sstest2.css")}
${scripts.addFromTheme("/js/jstest.js")} ${scripts.addFromTheme("/js/jstest.js")}
${scripts.add("/js/script1.js", "/js/script2.js", "/js/script3.js")} ${scripts.add("/js/script1.js", "/js/script2.js", "/js/script3.js")}
<@dumpAll /> <@dumpAll />
<@help directive="dump" /> <@help for="dump" />
<@help for="profileUrl" />
<@describe var="stylesheets" /> <@describe var="stylesheets" />
<@describe var="scripts" />
<@describe var="headScripts" />