The issue is well-described in the trac ticket. Adding a Engine.GetTemplate(name) function to be used by the AI allows to remove the current hardcodings for dynamic templates (at the moment, a new hardcoding has to be added in shared.js for every new dynamic template added, an issue that has blocked the progress of diffs like D230).
CAIWorker now uses a TemplateLoader member var in a similar way to the MapGenerator and GUIManager. In another diff, the TemplateLoader can then be used to load templates on-demand from the AI as needed, instead of loading every single template in the game at the start (more than 1100), which will give a small performance boost. (This will be done by removing m_EntityTemplates from CAIWorker, and removing the methods StartLoadEntityTemplates, ContinueLoadEntityTemplates, ForceLoadEntityTemplates from CCmpAIManager.)
However, this diff also needs to deal with a design quirk in the AI's template handling in entity.js. It is possible to have components that simply act as "flags", i.e. the Wonder and Garrisonable components, which are non-system components with no schema. So they exist simply as f.e. <Wonder/>, <Garrisonable/> in templates. These are handled fine in the simulation, of course, as just Engine.QueryInterface(ent, IID_Garrisonable); can be done to check for the presence of the garrison component for an ent, but there is no equivalent check for the AI. this.get("Garrisonable") will always return undefined for such components, even if the Garrisonable property is present in the template. So the diff deals with this with a check for the property in the raw template (this will need to be improved to take into account entity and template modifications). The AI has been able to avoid this problem with wonder as it never checks for the wonder component, but instead checks for the Wonder class.
There was also another quirk/inconsistency found after I did some testing. The AI treats foundations as having territory influence, although they don't have it. So there either must be a check if the parent entity has territory influence, or the check can be removed, as Petra will only assign civic centers as bases anyways.