Ejemplo n.º 1
0
  public void internalRenderTemplate(Map<String, Object> args, boolean startingNewRendering)
      throws GTTemplateNotFoundWithSourceInfo, GTRuntimeException {

    if (startingNewRendering) {
      // start with fresh tag-stack
      GTTagContext.singleton.init();
    }

    try {

      // must store a copy of args, so we can pass the same (unchnaged) args to an extending
      // template.
      this.orgArgs = new HashMap<String, Object>(args);
      this.binding =
          new Binding(
              new HashMap<String, Object>(
                  args)); // Must create a new map to prevent script-generated variables to leak out
      this.binding.setProperty("java_class", this);
      // must init our groovy script

      // groovyScript = InvokerHelper.createScript(groovyClass, binding);
      groovyScript = groovyClass.newInstance();
      groovyScript.setBinding(binding);

      // create a property in groovy so that groovy can find us (this)

      // call _renderTemplate directly
      _renderTemplate();

      // check if "we" have extended another template..
      if (extendsTemplateLocation != null) {
        // yes, we've extended another template
        // Get the template we are extending
        extendedTemplate = templateRepo.getTemplateInstance(extendsTemplateLocation);

        // tell it that "we" extended it..
        extendedTemplate.extendingTemplate = this;

        // ok, render it with original args..
        extendedTemplate.internalRenderTemplate(orgArgs, false);
      }

    } catch (GTCompilationException e) {
      // just throw it
      throw e;
    } catch (Throwable e) {
      // wrap it in a GTRuntimeException
      throw templateRepo.fixException(e);
    }
  }
Ejemplo n.º 2
0
  protected void invokeTagFile(
      String tagName,
      String tagFilePath,
      GTContentRenderer contentRenderer,
      Map<String, Object> tagArgs) {

    GTTemplateLocationReal tagTemplateLocation =
        GTFileResolver.impl.getTemplateLocationReal(tagFilePath);
    if (tagTemplateLocation == null) {
      throw new GTTemplateRuntimeException("Cannot find tag-file '" + tagFilePath + "'");
    }
    GTJavaBase tagTemplate = templateRepo.getTemplateInstance(tagTemplateLocation);
    // must set contentRenderes so that when the tag/template calls doBody, we can inject the output
    // of the content of this tag
    tagTemplate.contentRenderer = contentRenderer;
    // render the tag
    // input should be all org args
    Map<String, Object> completeTagArgs = new HashMap<String, Object>(orgArgs);

    // and all scoped variables under _caller
    completeTagArgs.put("_caller", this.binding.getVariables());

    // Add new arg named "body" which is a fake closure which can be used to get the text-output
    // from the content of this tag..
    // Used in selenium.html-template and by users (eg: Greenscript)
    completeTagArgs.put("_body", new GTContentRendererFakeClosure(this, contentRenderer));

    // TODO: Must handle tag args like  _:_

    // and of course the tag args:
    // must prefix all tag args with '_'
    for (String key : tagArgs.keySet()) {
      completeTagArgs.put("_" + key, tagArgs.get(key));
    }

    // Must also add all tag-args (the map) with original names as a new value named '_attrs'
    completeTagArgs.put("_attrs", tagArgs);

    tagTemplate.internalRenderTemplate(completeTagArgs, false);
    // grab the output
    insertOutput(tagTemplate);
  }