render static method

dynamic render({
  1. required String path,
  2. Map<String, Object?> viewParams = const {},
  3. bool isFile = true,
  4. String language = 'en',
})

Implementation

static render({
  required String path,
  Map<String, Object?> viewParams = const {},
  bool isFile = true,
  String language = 'en',
}) {
  if (isFile) {
    File file = File(joinPaths([
      WaServer.config.widgetsPath,
      "$path.${WaServer.config.widgetsType}",
    ]));

    if (!file.existsSync()) {
      if (WaServer.config.isLocalDebug) {
        return "The path: ${file.path} is not correct!";
      } else {
        return "The path: ${file.uri.pathSegments.last} is not correct!";
      }
    }
  }

  var env = Environment(
      globals: _getGlobalEvents(language),
      autoReload: false,
      loader: FileSystemLoader(paths: <String>[WaServer.config.widgetsPath]),
      leftStripBlocks: false,
      trimBlocks: false,
      blockStart: WaServer.config.blockStart,
      blockEnd: WaServer.config.blockEnd,
      variableStart: WaServer.config.variableStart,
      variableEnd: WaServer.config.variableEnd,
      commentStart: WaServer.config.commentStart,
      commentEnd: WaServer.config.commentEnd,
      filters: {
        'dateFormat': (DateTime dt, String format) {
          return DateFormat(format).format(dt);
        },
      },
      getAttribute: (String key, dynamic object) {
        try {
          if (object is TString) {
            return object.writeByLang(language);
          }
          if (object is String && key == 'tr') {
            return object.tr.writeByLang(language);
          }

          if (object[key] != null) {
            if (object[key] is ObjectId) {
              (object[key] as ObjectId).oid;
            }
          }
          return object[key];
        } on NoSuchMethodError {
          Console.e({
            'error': {
              'object': object,
              'key': key,
              'error': 'The key "$key" on "$object" not found',
            }
          });

          if (object == null) {
            if (WaServer.config.isLocalDebug) {
              return 'The key "$key" on "$object" not found';
            } else {
              return null;
            }
          }

          return null;
        } catch (e) {
          Console.e({
            'error': {
              'object': object,
              'key': key,
              'error': e,
            }
          });
          return null;
        }
      });
  var params = viewParams;
  Template template;
  if (isFile) {
    template = env.getTemplate(File(
      joinPaths([
        WaServer.config.widgetsPath,
        "$path.${WaServer.config.widgetsType}",
      ]),
    ).path);
  } else {
    template = env.fromString(path);
  }
  var renderString = template.render(params);
  return renderString;
}