Measurement of the charge asymmetry in top quark pair production in pp collisions at sqrt(s) = 8 TeV using a template method