render method
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([
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(),
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.write(this);
}
if (object is String && key == 'tr') {
return object.tr.write(this);
}
if (object is Cookie) {
return key == 'name' ? object.name : object.value;
}
if (object[key] != null) {
if (object[key] is ObjectId) {
return (object[key] as ObjectId).oid;
}
}
return object[key];
} on NoSuchMethodError {
Console.w({
'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.w({
'error': {
'object': object,
'key': key,
'error': e,
}
});
return null;
}
});
var params = getParams();
params.addAll(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;
}