renderAsync method

String renderAsync({
  1. required String path,
  2. Map<String, Object?> viewParams = const {},
  3. bool isFile = true,
  4. int status = 200,
})

Renders a template with the given parameters and configuration.

This method supports rendering from a file or a raw template string. It can handle different types of rendering based on the isFile and toData parameters. It also manages the localization and filtering of data.

path - The path or content of the template to be rendered. If isFile is true, this should be the file path. viewParams - A map of parameters to be passed to the template. Default is an empty map. isFile - A flag indicating whether path refers to a file (true) or a string template (false). Default is true. status - The HTTP status code to be used. Default is 200.

Returns a Future<String> containing the rendered template as a string.

Implementation

String renderAsync({
  required String path,
  Map<String, Object?> viewParams = const {},
  bool isFile = true,
  int status = 200,
}) {
  if (isClosed) return '';

  if (isFile) {
    File file = File(joinPaths([
      FinchApp.config.widgetsPath,
      "$path.${FinchApp.config.widgetsType}",
    ]));

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

  var env = getTemplateEnvironment();
  var params = getParams();
  params.addAll(viewParams);
  Template template;
  if (isFile) {
    template = env.getTemplate(File(
      joinPaths([
        FinchApp.config.widgetsPath,
        "$path.${FinchApp.config.widgetsType}",
      ]),
    ).path);
  } else {
    template = env.fromString(path);
  }
  var renderString = template.render(params);
  return renderString;
}