public static void encodeIndexedId(FacesContext context, UIComponent component, int index) throws IOException {
        // swap writers
        ResponseWriter originalWriter = context.getResponseWriter();
        FastStringWriter fsw = new FastStringWriter();
        ResponseWriter clonedWriter = originalWriter.cloneWithWriter(fsw);
        context.setResponseWriter(clonedWriter);

        // encode the component
        component.encodeAll(context);

        // restore the original writer
        context.setResponseWriter(originalWriter);

        // append index to all id's
        char separator = UINamingContainer.getSeparatorChar(context);
        String encodedComponent = fsw.toString();

        if (clonedWriter instanceof CspResponseWriter) {
            CspResponseWriter cspWriter = (CspResponseWriter) clonedWriter;
            // find all id's to replace
            Matcher matcher = ID_PATTERN.matcher(encodedComponent);
            while (matcher.find()) {
                String oldId = matcher.group(1);
                String newId = oldId + separator + index;
                cspWriter.updateId(oldId, newId);
            }
        }

        // replace 'id=' and 'source:' values
        encodedComponent = ID_PATTERN.matcher(encodedComponent).replaceAll(" id=\"$1" + separator + index + "\"");
        encodedComponent = SOURCE_PATTERN.matcher(encodedComponent).replaceAll(" source:&quot;$1" + separator + index + "&quot;");

        originalWriter.write(encodedComponent);
    }
GitHub #7763 Renders an in memory UIComponent and updates its ID with an index.
For example id="form:test" becomes id="form:test:0".

@param context the {@link FacesContext}.
@param component the {@link UIComponent} to render.
@param index the index number to append to the ID
@throws IOException if any IO error occurs
primefaces/src/main/java/org/primefaces/util/ComponentUtils.java