A valid and facile template carbonization route for producing nanoporous carbons with hierarchical porosities has been implemented by using thiocarbanilide as carbon/nitrogen precursor, and commercially available Mg(OH)2 or Ca(OH)2 powder as template. This work is to clarify the significance and difference between Mg(OH)2 and Ca(OH)2 being as templates. The mass ratio of thiocarbanilide and Mg(OH)2 or Ca(OH)2, as well as the carbonization temperature plays crucial role in determining the pore structures and the resultant capacitive behaviors. It reveals that carbon-Mg sample whose template is Mg(OH)2 (with a mass ratio of 1:2 at 700°C) exhibits the amorphous feature with low graphitization degree. Similar result also occurs in the case of the carbon-Ca sample. The carbon-Mg sample presents high specific surface area (1018.48m2 g−1), large pore volume (5.29cm3 g−1), while those of the carbon-Ca sample are 429.11m2 g−1 and 2.52cm3 g−1, respectively. The carbon-Mg sample delivers a high specific capacitance of 327.4Fg−1 at a current density of 1.0Ag−1, as well as a large energy density of 45.47Whkg−1 at a power density of 0.5kWkg−1 in comparison with carbon-Ca sample of (260.0Fg−1) and (36.11Whkg−1). What’s more, the carbon-Mg sample exhibits higher capacitance retention of 95.45% after 10,000 charge/discharge cycles than carbon-Ca sample of 91.62% in 6molL−1 KOH electrolyte. Using Ca(OH)2 or Mg(OH)2 as template by a template carbonization route provides a simple but feasible protocol to achieve nanoporous carbons with precisely controlled architecture and hierarchical porosities.