render method

Future<String> render({
  1. required String path,
  2. Map<String, Object?> viewParams = const {},
  3. bool isFile = true,
  4. bool toData = false,
  5. 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. toData - A flag indicating whether to render the data as a parameter. Default is false. status - The HTTP status code to be used. Default is 200.

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

Implementation

Future<String> render({
  required String path,
  Map<String, Object?> viewParams = const {},
  bool isFile = true,
  bool toData = false,
  int status = 200,
}) async {
  if (isClosed) return '';
  if (toData) {
    return renderDataParam(status: status, data: viewParams);
  }

  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);
  if (buffer.isNotEmpty &&
      response.headers.contentType?.mimeType == 'text/html') {
    renderString += buffer.toString();
    buffer.clear();
  }
  return renderString;
}