public static void CompileTemplate(TemplateInfo ti) { ti.Programs = new Dictionary<string, TALProgram>(); ti.ImportedPrograms = new Dictionary<string, TALProgram>(); ti.ImportedNamespaces = new Dictionary<string, HashSet<string>>(); TALCompiler compiler = new TALCompiler(); // Compile main template TALProgram mainProg = compiler.Compile(ti.TemplateBody, "<main template>"); ti.Programs.Add("template", mainProg); // Compile imports of main template CompileImports(compiler, mainProg, ti); // Compile inline templates if (ti.InlineTemplates != null) { foreach (string key in ti.InlineTemplates.Keys) { TALProgram inlineProg = compiler.Compile(ti.InlineTemplates[key], string.Format("<inline template: {0}>", key)); ti.Programs.Add(key, inlineProg); // Compile Imports of inline template CompileImports(compiler, inlineProg, ti); } } // Compute template hash ti.TemplateHash = ComputeTemplateHash(ti); }
static void CompileImports(TALCompiler compiler, TALProgram program, TemplateInfo ti) { if (program.Imports != null && program.Imports.Count > 0) { foreach (string key in program.Imports) { // Split the Import key string destNs = key.Split(new char[] { ':' }, 2)[0]; string sourcePath = key.Split(new char[] { ':' }, 2)[1]; // Imported macros without namespace go into main template namespace. if (string.IsNullOrEmpty(destNs)) { destNs = "template"; } // Check if the template on path was not compiled TALProgram importedProg = null; if (!ti.ImportedPrograms.ContainsKey(sourcePath)) { // Compile Imported template string source = File.ReadAllText(sourcePath); importedProg = compiler.Compile(source, sourcePath); ti.ImportedPrograms.Add(sourcePath, importedProg); // Compile Imports of Imported template CompileImports(compiler, importedProg, ti); } else { importedProg = ti.ImportedPrograms[sourcePath]; } // Save info about Imported program by namespace and path if (!ti.ImportedNamespaces.ContainsKey(destNs)) { ti.ImportedNamespaces.Add(destNs, new HashSet<string>() { sourcePath }); } else { if (!ti.ImportedNamespaces[destNs].Contains(sourcePath)) { ti.ImportedNamespaces[destNs].Add(sourcePath); } } } } }