The preparation of well-ordered functional mesoporous polymers through the direct doping and soft templating approach remains challenging and has so far been only limited to specific systems, because of the difficulty to balance the interaction between templates and precursors. Herein we demonstrate the successful preparation of an imide-functionalized mesoporous polymer and its derived nitrogen-doped mesoporous carbon with high regularity, tunable morphology, and high surface area, using highly stable aromatic imide as the nitrogen source. Amic acid, the precursor of imide, undergoes a co-self-assembly process with an amphiphilic block copolymer template and transforms into imide on heating. Upon calcination at 350 [degree]C the template F127 is removed without damaging the imide linkage, leading to a highly ordered polymer network with the intrinsic imide functionality. Further carbonization at 600 [degree]C allows the formation of nitrogen-doped mesoporous carbon. This newly developed system, which exhibits a high degree of flexibility, allows precise morphological control of 1-dimensional lamellar, 2-dimensional hexagonal and 3-dimensional cubic mesostructures on the basis of adjustments of each component in the system. This homologous series of imide functionalized mesoporous polymers and nitrogen-doped mesoporous carbons have potential applications in the field of advanced materials.