BadScript 2
Loading...
Searching...
No Matches
BadHtml.BadHtmlTemplate Class Reference

Implements a Html Template. More...

Public Member Functions

void Reload ()
 Reloads the Template Source.
 
HtmlDocument RunTemplate (object? model=null, BadHtmlTemplateOptions? options=null, BadScope? caller=null)
 Runs the Template with the specified arguments.
 
string Run (object? model=null, BadHtmlTemplateOptions? options=null, BadScope? caller=null)
 Runs the Template with the specified arguments.
 

Static Public Member Functions

static BadHtmlTemplate Create (string file, IFileSystem? fileSystem=null)
 Creates a new Template.
 

Properties

string FilePath [get]
 The Filepath of the Template.
 

Private Member Functions

 BadHtmlTemplate (string filePath, IFileSystem fileSystem)
 Constructs a new Template.
 
string GetSource ()
 Returns the source code of the template. Loads the source code if it is not loaded yet.
 

Private Attributes

readonly IFileSystem m_FileSystem
 The Filesystem used to load the template.
 
string? m_Source
 The Source code of the Template(gets loaded on first template run)
 

Detailed Description

Implements a Html Template.

Definition at line 16 of file BadHtmlTemplate.cs.

Constructor & Destructor Documentation

◆ BadHtmlTemplate()

BadHtml.BadHtmlTemplate.BadHtmlTemplate ( string  filePath,
IFileSystem  fileSystem 
)
private

Constructs a new Template.

Parameters
filePathFile path of the template
fileSystemThe Filesystem that is used to Load the Template

Definition at line 33 of file BadHtmlTemplate.cs.

34 {
35 FilePath = filePath;
36 m_FileSystem = fileSystem;
37 }
readonly IFileSystem m_FileSystem
The Filesystem used to load the template.
string FilePath
The Filepath of the Template.

Member Function Documentation

◆ Create()

static BadHtmlTemplate BadHtml.BadHtmlTemplate.Create ( string  file,
IFileSystem fileSystem = null 
)
static

Creates a new Template.

Parameters
fileTemplate File Path
fileSystemThe Filesystem that the Template is loaded from
Returns
A Html Template Instance

Definition at line 133 of file BadHtmlTemplate.cs.

134 {
135 return new BadHtmlTemplate(file, fileSystem ?? BadFileSystem.Instance);
136 }
BadHtmlTemplate(string filePath, IFileSystem fileSystem)
Constructs a new Template.
Public interface for the filesystem abstraction of the BadScript Engine.
static IFileSystem Instance
File System implementation.

◆ GetSource()

string BadHtml.BadHtmlTemplate.GetSource ( )
private

Returns the source code of the template. Loads the source code if it is not loaded yet.

Returns
Template Source

Definition at line 49 of file BadHtmlTemplate.cs.

50 {
51 if (m_Source == null)
52 {
53 Reload();
54 }
55
56 return m_Source!;
57 }
void Reload()
Reloads the Template Source.
string? m_Source
The Source code of the Template(gets loaded on first template run)

◆ Reload()

void BadHtml.BadHtmlTemplate.Reload ( )

Reloads the Template Source.

Definition at line 62 of file BadHtmlTemplate.cs.

63 {
64 m_Source = m_FileSystem.ReadAllText(FilePath);
65 }

◆ Run()

string BadHtml.BadHtmlTemplate.Run ( object?  model = null,
BadHtmlTemplateOptions options = null,
BadScope caller = null 
)

Runs the Template with the specified arguments.

Parameters
modelThe Model used within the template
optionsThe Template Options
callerOptional Caller (if executed from within badscript, is used to get a full stacktrace on errors)
Returns
The Html Source of the Generated Document

Definition at line 122 of file BadHtmlTemplate.cs.

123 {
124 return RunTemplate(model, options, caller).DocumentNode.OuterHtml;
125 }
HtmlDocument RunTemplate(object? model=null, BadHtmlTemplateOptions? options=null, BadScope? caller=null)
Runs the Template with the specified arguments.

◆ RunTemplate()

HtmlDocument BadHtml.BadHtmlTemplate.RunTemplate ( object?  model = null,
BadHtmlTemplateOptions options = null,
BadScope caller = null 
)

Runs the Template with the specified arguments.

Parameters
modelThe Model used within the template
optionsThe Template Options
callerOptional Caller (if executed from within badscript, is used to get a full stacktrace on errors)
Returns
The Html Document that was generated

Definition at line 74 of file BadHtmlTemplate.cs.

78 {
79 options ??= new BadHtmlTemplateOptions();
80 string src = GetSource();
81
82 // ReSharper disable once UseObjectOrCollectionInitializer
83 HtmlDocument input = new HtmlDocument();
84 input.OptionUseIdAttribute = true;
85 input.LoadHtml(src);
86
87 // ReSharper disable once UseObjectOrCollectionInitializer
88 HtmlDocument output = new HtmlDocument();
89 output.OptionUseIdAttribute = true;
90 output.LoadHtml("");
91 BadExecutionContext executionContext = (options.Runtime ?? new BadRuntime()).CreateContext(Path.GetDirectoryName(m_FileSystem.GetFullPath(FilePath)) ?? "/");
92 executionContext.Scope.SetCaller(caller);
93
94 if (model != null)
95 {
96 BadObject mod = model as BadObject ?? BadObject.Wrap(model);
97 executionContext.Scope.DefineVariable(
98 "Model",
99 mod,
100 executionContext.Scope,
102 );
103 }
104
105 foreach (HtmlNode node in input.DocumentNode.ChildNodes)
106 {
107 BadHtmlContext ctx =
108 new BadHtmlContext(node, output.DocumentNode, executionContext, FilePath, src, options, m_FileSystem);
109 BadHtmlNodeTransformer.Transform(ctx);
110 }
111
112 return output;
113 }
string GetSource()
Returns the source code of the template. Loads the source code if it is not loaded yet.
Exposes the BadScript Runtime Functionality to Consumers.
Definition BadRuntime.cs:28
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
void SetCaller(BadScope? caller)
Sets the Caller of the Scope.
Definition BadScope.cs:512
void DefineVariable(string name, BadObject value, BadScope? caller=null, BadPropertyInfo? info=null, BadObject[]? attributes=null)
Defines a new Variable in the current scope.
Definition BadScope.cs:929
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
Stores Meta Information about a Property.
The Any Prototype, Base type for all types.
static readonly BadAnyPrototype Instance
The Instance of the BadAnyPrototype.
string GetFullPath(string path)
Returns the full path of the given path.

Member Data Documentation

◆ m_FileSystem

readonly IFileSystem BadHtml.BadHtmlTemplate.m_FileSystem
private

The Filesystem used to load the template.

Definition at line 21 of file BadHtmlTemplate.cs.

◆ m_Source

string? BadHtml.BadHtmlTemplate.m_Source
private

The Source code of the Template(gets loaded on first template run)

Definition at line 26 of file BadHtmlTemplate.cs.

Property Documentation

◆ FilePath

string BadHtml.BadHtmlTemplate.FilePath
get

The Filepath of the Template.

Definition at line 43 of file BadHtmlTemplate.cs.

43{ get; }

The documentation for this class was generated from the following file: