/** * 基于当前模板路径,转换子模板相对路径为绝对路径。 */ public String getAbsolutionName(String name) { return PathUtils.getAbsolutionName(template.getName(), name); } }
@Override public JetTemplate getTemplate(String name) throws ResourceNotFoundException { JetTemplate template = internalGetTemplate(name); if (template != null) { try { template.reload(); return template; } catch (ResourceNotFoundException e) { cache.remove(template.getName()); throw e; } } throw new ResourceNotFoundException(name); }
public static String fileGet(String relativeName, String encoding) { InterpretContext ctx = InterpretContext.current(); String name = PathUtils.getRelativePath(ctx.getTemplate().getName(), relativeName); Resource resource = ctx.getEngine().getResource(name); return IoUtils.toString(resource.openStream(), encoding); }
@SuppressWarnings("unchecked") @Override public void execute(InterpretContext ctx) throws InterpretException { Object file = fileExpression.execute(ctx); if (file == null) { throw new InterpretException(Errors.EXPRESSION_NTH_ARGUMENT_IS_NULL, "1st").set(fileExpression.getPosition()); } if (!(file instanceof String)) { throw new InterpretException(Errors.VARIABLE_TYPE_MISMATCH, "1st", file.getClass(), "String").set(fileExpression.getPosition()); } Object parameters; if (parametersExpression == null) { parameters = Collections.emptyMap(); } else { parameters = parametersExpression.execute(ctx); if (parameters == null) { parameters = Collections.emptyMap(); } else if (!(parameters instanceof Map)) { throw new InterpretException(Errors.VARIABLE_TYPE_MISMATCH, "2nd", parameters.getClass(), "Map<String, Object>").set(parametersExpression.getPosition()); } } try { String fileName = PathUtils.getRelativePath(ctx.getTemplate().getName(), (String) file); ctx.doIncludeCall(fileName, (Map<String, Object>) parameters, returnName); } catch (ResourceNotFoundException e) { throw new InterpretException(Errors.INCLUDE_FILE_NOT_FOUND, file).set(fileExpression.getPosition()); } }
public static String includeGet(String relativeName, Map<String, Object> parameters) throws ResourceNotFoundException { InterpretContext ctx = InterpretContext.current(); String name = PathUtils.getRelativePath(ctx.getTemplate().getName(), relativeName); JetWriter originWriter = ctx.getWriter(); UnsafeCharArrayWriter out = new UnsafeCharArrayWriter(1024); JetWriter writer = JetWriter.create(out, originWriter.getCharset(), false, false); ctx.setWriter(writer); ctx.doIncludeCall(name, parameters, null); ctx.setWriter(originWriter); // reset return out.toString(); }