int i = name.indexOf('#');
if (i > 0) {
macro = name.substring(i + 1);
name = name.substring(0, i);
}
Template template = Context.getContext().getTemplate();
if (template != null) {
if (StringUtils.isEmpty(encoding)) {
encoding = template.getEncoding();
}
name = UrlUtils.relativeUrl(name, template.getName());
if (locale == null) {
locale = template.getLocale();
}
}
if (StringUtils.isNotEmpty(extendsDirectory)) {
name = extendsDirectory + name;
}
Template extend = engine.getTemplate(name, locale, encoding);
if (StringUtils.isNotEmpty(macro)) {
extend = extend.getMacros().get(macro);
}
if (template != null) {
if (template == extend) {
throw new IllegalStateException("The template " + template.getName() + " can not be recursive extending the self template.");
}