{"record":{"id":"868d30812642ed8a","repo":"elastic/elasticsearch","slug":"cannot-expand-s-because-dynamic-partial-templat","errorCode":null,"errorMessage":"Cannot expand '%s' because dynamic partial templates are not supported","messagePattern":"Cannot expand '(.+?)' because dynamic partial templates are not supported","errorType":"exception","errorClass":"MustacheException","httpStatus":null,"severity":"error","filePath":"modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomMustacheFactory.java","lineNumber":152,"sourceCode":"            } else if (UrlEncoderCode.match(variable)) {\n                list.add(new UrlEncoderCode(templateContext, df, mustache, variable));\n            } else {\n                list.add(new IterableCode(templateContext, df, mustache, variable));\n            }\n        }\n\n        @Override\n        public void partial(TemplateContext tc, String variable, String indent) {\n            // throwing a mustache exception here is important because this gets caught, handled (closing readers, etc.),\n            // and re-thrown in the mustache parser itself\n            throw new MustacheException(Strings.format(\"Cannot expand '%s' because partial templates are not supported\", variable));\n        }\n\n        @Override\n        public void dynamicPartial(TemplateContext tc, final String variable, String indent) {\n            // throwing a mustache exception here is important because this gets caught, handled (closing readers, etc.),\n            // and re-thrown in the mustache parser itself\n            throw new MustacheException(Strings.format(\"Cannot expand '%s' because dynamic partial templates are not supported\", variable));\n        }\n    }\n\n    /**\n     * Base class for custom Mustache functions\n     */\n    private abstract static class CustomCode extends IterableCode {\n\n        private final String code;\n\n        CustomCode(TemplateContext tc, DefaultMustacheFactory df, Mustache mustache, String code) {\n            super(tc, df, mustache, extractVariableName(code, mustache, tc));\n            this.code = Objects.requireNonNull(code);\n        }\n\n        @Override\n        public Writer execute(Writer writer, final List<Object> scopes) {\n            Object resolved = get(scopes);","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomMustacheFactory.java#L134-L170","documentation":"Thrown by CustomMustacheVisitor.dynamicPartial() when a Mustache template uses the dynamic partial syntax ({{$name}} blocks with {{<name}} inclusion). Dynamic partials are a mustache extension for template inheritance/overriding that Elasticsearch's implementation does not support, for the same reason regular partials are unsupported: templates are inline strings with no file or classpath resolution.","triggerScenarios":"Including dynamic partial syntax in a search template source. For example, {{$content}}...{{/content}} block definitions or {{<base}} template inheritance directives. The mustache parser invokes the visitor's dynamicPartial() method during compilation, which immediately throws.","commonSituations":"Using mustache template inheritance features from the mustache specification extensions. Porting templates from systems that use {{$block}} / {{<parent}} inheritance. Attempting to override sections of a base template dynamically.","solutions":["Remove all {{$...}} and {{<...}} dynamic partial syntax from the template","Use conditional sections ({{#var}}...{{/var}}) and parameters to achieve dynamic content variation","Create separate stored templates for each variant and select the appropriate one client-side","Use the {{#toJson}}var{{/toJson}} function for complex parameter-driven JSON injection"],"exampleFix":"// before — template with dynamic partial block:\n{{$filter}}{\"term\": {\"x\": \"{{val}}\"}}{{/filter}}\n\n// after — use a parameterized section instead:\n{{#val}}{\"term\": {\"x\": \"{{val}}\"}}{{/val}}","handlingStrategy":"validation","validationCode":"// Validate template source for dynamic partial syntax\nString templateSource = \"...\";\nif (templateSource.contains(\"{{$\") || templateSource.contains(\"{{<\")) {\n    throw new IllegalArgumentException(\"Dynamic partial templates are not supported in Elasticsearch\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never use {{$block}} or {{<parent}} syntax in Elasticsearch templates","Use parameterized sections {{#var}}...{{/var}} for dynamic content","Avoid template inheritance patterns from other mustache implementations"],"tags":["elasticsearch","lang-mustache","dynamic-partial","template","unsupported-feature"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:17:08.281Z"}