Skip to content

Commit

Permalink
Add Generic Execute File operations to allow for strongly typed models.
Browse files Browse the repository at this point in the history
  • Loading branch information
RickStrahl committed Dec 31, 2024
1 parent e36ad1d commit 031b6df
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 39 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ For more detailed information and a discussion of the concepts and code that run
* Detailed compiler error messages
* Access to compiled output w/ line numbers
* Roslyn Warmup
* Template Scripting Engine using Handlebars-like with C# syntax
* [Template Scripting Engine using Handlebars-like C# syntax](ScriptAndTemplates.md)
* Script Execution from strings
* Script Execution from files
* Support for Partials, Layout and Sections

### CSharpScriptExecution: C# Runtime Compilation and Execution
Runtime code compilation and execution is handled via the `CSharpScriptExecution` class.
Expand Down
30 changes: 20 additions & 10 deletions ScriptAndTemplates.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
# Westwind.Scripting: Scripts and Template Processing

This library includes a scripting engine that allows you to execute string or file based templates and expanded embedd C# expressions and codeblocks that can be embedded using Handlebar-like syntax.
This library includes a scripting engine that allows you to execute string or file based templates and expanded embeded C# expressions and code blocks that can be embedded using Handlebars-like syntax.

The scripting engine supports:

* String based template execution
* File based script execution with optional Layout/Section support
* Support for Partials loaded from disk
* Support for Layout Pages and Sections (for file scripts)
* Expressions and Codeblocks use raw C# syntax
* Using familiar Handlebars syntax:
* Support for Partials loaded from disk (both string and file)
* Support for Layout Pages and Sections (for file only)
* Expressions and code blocks use raw C# syntax
* Familiar Handlebars syntax with C# code:
* `{{ C# expression }}`
* `{{% code block }}`
commands can be split across multiple lines, and any other content
block - literal text or expressions
* `{{: encode Expression }}`
* `{{: html encoded Expression }}`
* `{{! raw expression }}`
* `{{@ commented block @}}`
* Code blocks can be structured using compound statements (for, while, if, using { } etc.)
* Templates compile to C# code at runtime
* Code blocks can be build compound statements (for, while, if, using { } etc.)
* Script compiles to C# code at runtime
* Compiled code is very efficient
* Using a single ScriptParser instance allows caching compiled output
* Support for Web based execution and features (ie. optional automatic Html Encoding)
* ScriptParser instance caches compiled output

`ScriptParser` is built ontop of the CSharpScriptExecution` class which is used for compilation and script execution. The parser class adds the ability to parse the Handlebars-style scripts into C# code that is then executed by the script engine.

The parser has an internal `ScriptEngine` member, which allows full access to the script compilation and execution so you can add assemblies, get detailed error information, set compiler options etc.

```cs
var script = new ScriptParser();
script.AddAssembly(typeof(this));
script.ScriptEngine.Generate




There are two categories of processing:
Expand Down
15 changes: 15 additions & 0 deletions Westwind.Scripting.Test/FileScriptParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ public async Task LayoutFileAsyncScriptTest()

Assert.IsNotNull(result, scriptParser.ErrorMessage);
}

[TestMethod]
public async Task LayoutFileAsyncTypedModelScriptTest()
{
var scriptParser = new ScriptParser();

var result = await scriptParser.ExecuteScriptFileAsync<TestModel>("website/Views/Detail.html",
new TestModel { Name = "Rick" },
basePath: "website/Views/");

Console.WriteLine(result);
Console.WriteLine(scriptParser.ScriptEngine.GeneratedClassCodeWithLineNumbers);

Assert.IsNotNull(result, scriptParser.ErrorMessage);
}
}
}

Loading

0 comments on commit 031b6df

Please sign in to comment.