Template method

FutureOr Template(
  1. String html, [
  2. Map? data = const {'' : ''}
])

Implementation

FutureOr Template(String html, [Map? data=const{'':''}]) async {
  try{
    res?.statusCode = HttpStatus.ok;
    res?.headers.contentType =  ContentType.html;

      File file = File(Directory.current.path+'/templates/'+html);
      if(await file.exists()){
        if(data?.length!=0){
          var parsed;
          var content = await file.readAsString();

          data?.forEach((key, value) {
            var replaced = content.replaceAll('#$key', value);
            parsed = replaced;
          });

          res?.write(parsed);
          await res?.close();
        }else{

          res?.addStream(file.openRead());
          await res?.close();
        }
      }else{
        await sendNotFound();
      }
  }on Exception{
    throw CustomException('Template engine error!').showError();
  }

}