public static ScribanTemplate Parse(string script, bool cache = true) { if (cache) { if (_scriptLookup.TryGetValue(script, out var cached)) { return(cached); } } var template = Template.Parse(script); if (template.HasErrors) { Log.Error(GetTextWithLineNumbers(script)); foreach (var message in template.Messages) { Log.Error(message.ToString()); } } var result = new ScribanTemplate(script, template); if (cache) { _scriptLookup.Add(script, result); } return(result); }
static ScribanRuntime() { _initTemplate = ScribanTemplate.Parse(@" {{- func setscripterror (error) RosiScriptError = error end -}}"); }
internal ScribanResult(ScribanResultType resultType, ScribanRuntime runtime, ScribanTemplate template, string message) : this(resultType, runtime, template) { if (resultType == ScribanResultType.Success) { Output = message; } else { Error = message; } }
public ScribanResult Render(ScribanTemplate template) { try { if (!template.IsValid) { return(new ScribanResult(ScribanResultType.TemplateError, this, template, template.ErrorMessage)); } Globals["RosiScriptError"] = string.Empty; var output = template.Template.Render(Context); if (!string.IsNullOrEmpty(output) && _forceLinefeed) { output = output.Replace("\r\n", "\n"); } ; var scriptError = (string)Globals["RosiScriptError"]; if (!string.IsNullOrWhiteSpace(scriptError)) { return(new ScribanResult(ScribanResultType.ScriptError, this, template, scriptError)); } return(new ScribanResult(ScribanResultType.Success, this, template, output)); } catch (Exception ex) { Log.Error(template.GetTextWithLineNumbers()); Log.Error(ex.ToString(), this); if (ex.InnerException != null) { Log.HandleException(ex.InnerException, this); } return(new ScribanResult(ScribanResultType.RenderError, this, template, ex.ToString())); } }
ScribanResult(ScribanResultType resultType, ScribanRuntime runtime, ScribanTemplate template) { ResultType = resultType; Runtime = runtime; Template = template; }