ScriptParser Class

A very simple C# script parser that parses the provided script as a text string with embedded expressions and code blocks.

Literal text:

Parsed as plain text into the script output.

Expressions {{ expr }}

<i>{{ DateTime.Now.ToString("d") }}</i>

Code Blocks {{% C# code }}

{{% for(int x; x<10; x++  { }}     
    <div>{{ x }}. Hello World</div>
{{% } }}

To execute script:

var model = new TestModel {Name = "rick", DateTime = DateTime.Now.AddDays(-10)};

string script = @"
Hello World. Date is: {{ Model.DateTime.ToString(""d"") }}!
{{% for(int x=1; x<3; x++) 
{ }}
    {{ x }}. Hello World {{Model.Name}}
{{% } }}

And we're done with this!
";

var scriptParser = new ScriptParser();

// add dependencies - sets on .ScriptEngine instance
scriptParser.AddAssembly(typeof(ScriptParserTests));
scriptParser.AddNamespace("Westwind.Scripting.Test");

// Execute the script
string result = scriptParser.ExecuteScript(script, model);

Console.WriteLine(result);

Console.WriteLine(scriptParser.ScriptEngine.GeneratedClassCodeWithLineNumbers);
Console.WriteLine(scriptParser.ErrorType);  // if there's an error

Uses the .ScriptEngine property for execution and provides error information there.

System.Object
   Westwind.Scripting.ScriptParser

Class Members

MemberDescription
Constructor
AddAssemblies list of meta references to assemblies. Can be used with `Basic.References
public void AddAssemblies(String[] assemblies)
AddAssembly Adds an assembly to the list of references for compilation using a type that is loaded and contained in the assembly
public void AddAssembly(string assemblyFile)
AddNamespace Add a namespace for compilation of the template
public void AddNamespace(string nameSpace)
AddNamespaces Add a list of namespaces for compilation of the template
public void AddNamespaces(String[] nameSpaces)
CreateScriptEngine Creates an instance of a script engine with default configuration settings set and the abililty to quickly specify addition references and namespaces. You can pass this to…
public CSharpScriptExecution CreateScriptEngine(String[] references, String[] namespaces, Type[] referenceTypes)
EncodeStringLiteral Encodes a string to be represented as a C# style string literal. Example output: "Hello "Rick"!\r\nRock on"
public string EncodeStringLiteral(string plainString, bool addQuotes)
ExecuteScript Executes a script that supports {{ expression }} and {{% code block }} syntax and returns a string result. You can optionally pass in a pre-configured CSharpScriptExecution instance which allows…
public string ExecuteScript(string script, object model, CSharpScriptExecution scriptEngine, string basePath)
ExecuteScriptAsync Executes a script that supports {{ expression }} and {{% code block }} syntax and returns a string result. This version allows for async code inside of the template. You can optionally pass in a…
public Task ExecuteScriptAsync(string script, object model, CSharpScriptExecution scriptEngine, string basePath)
ExecuteScriptFile Executes a script that supports {{ expression }} and {{% code block }} syntax and returns a string result. You can optionally pass in a pre-configured CSharpScriptExecution instance which allows…
public string ExecuteScriptFile(string scriptFile, object model, CSharpScriptExecution scriptEngine, string basePath)
ExecuteScriptFileAsync Executes a script that supports {{ expression }} and {{% code block }} syntax and returns a string result. You can optionally pass in a pre-configured CSharpScriptExecution instance which allows…
public Task ExecuteScriptFileAsync(string scriptFile, object model, CSharpScriptExecution scriptEngine, string basePath)
FileScriptParsing This parses the script file and extracts layout and section information and updates the Script property
public bool FileScriptParsing(ScriptFileContext context)
HtmlEncode Encodes a value using Html Encoding by first converting
public string HtmlEncode(object value)
ParseLayoutPage This is a helper function that ooks at the content page and retrieves the ScriptLayout directive, and then tries to the load the layout template. The code then looks for the content page and merges…
public void ParseLayoutPage(ScriptFileContext context)
ParseScriptToCode Passes in a block of 'script' code into a string using code that uses a text writer to output. You can feed the output from this method in ExecuteCode() or similar to parse the script into an output…
public string ParseScriptToCode(ScriptFileContext scriptContext)
ParseSections Parses out sections from the content page and assigns them into the ScriptContext.Sections dictionary to be later expanded into the layout page.
public void ParseSections(ScriptFileContext scriptContext)
StripComments Strips {{@ commented block @}} from script
public string StripComments(string script)
AdditionalMethodHeaderCode Allows you to inject additional code into the generated method that executes the script.
Error Determines whether the was a compile time or runtime error
ErrorMessage Error Message if an error occurred
ErrorType Type of error that occurred during compilation or execution of the template
GeneratedClassCode Generated code that is compiled
GeneratedClassCodeWithLineNumbers Generated code with line numbers that is compiled. You can use this to match error messages to code lines.
SaveGeneratedClassCode
ScriptEngine Script Engine used if none is passed in
ScriptingDelimiters Delimiters used for script parsing
Namespace: Westwind.Scripting

Assembly: Westwind.Scripting.dll