Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        static ScribanRuntime()
        {
            _initTemplate = ScribanTemplate.Parse(@"
{{-
func setscripterror (error)
  RosiScriptError = error
end
-}}");
        }
Esempio n. 3
0
 internal ScribanResult(ScribanResultType resultType, ScribanRuntime runtime, ScribanTemplate template, string message) : this(resultType, runtime, template)
 {
     if (resultType == ScribanResultType.Success)
     {
         Output = message;
     }
     else
     {
         Error = message;
     }
 }
Esempio n. 4
0
        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()));
            }
        }
Esempio n. 5
0
 ScribanResult(ScribanResultType resultType, ScribanRuntime runtime, ScribanTemplate template)
 {
     ResultType = resultType;
     Runtime    = runtime;
     Template   = template;
 }