Difference between revisions of "PRS/WebTemplate"
(Created page with "== Introduction == Web Templates are the manner in which the Web Engine interfaces with a user, written in HTML with C# support using the Razor templating engine. Upon receiving a request from a user == Fields == === Slug === The DataModel field is a name of an entity type, such as Equipment or Employee, which specifies the specific data to load for the page template. For example, if you are writing a page which lists employees, it is useful to preload the data necessa...") |
|||
Line 40: | Line 40: | ||
<code>Model.LoadModel(new string[] {"CompanyInformation", "CompanyLogo", "Employee"})</code>. | <code>Model.LoadModel(new string[] {"CompanyInformation", "CompanyLogo", "Employee"})</code>. | ||
The second parameter specifies a way to narrow the load of the data, preventing unnecessary data from being loaded. For example, if one only requires the ID and Name fields of an employee, one might call: | The second parameter specifies a way to narrow the load of the data, preventing unnecessary data from being loaded. For example, if one only requires the ID and Name fields of an employee, one might call: | ||
< | <pre> | ||
Model.LoadModel(<br> | Model.LoadModel(<br> | ||
new string[] {"CompanyInformation", "CompanyLogo", "Employee"}, | new string[] {"CompanyInformation", "CompanyLogo", "Employee"}, | ||
Line 49: | Line 49: | ||
null) | null) | ||
) | ) | ||
</ | </pre><br> | ||
By default, all columns in the database are loaded, so this is a way to drastically reduce the amount of data loaded. | By default, all columns in the database are loaded, so this is a way to drastically reduce the amount of data loaded. | ||
(The other two fields, set to <code>null</code>, specify a filter and sort order for the retrieved data.) | (The other two fields, set to <code>null</code>, specify a filter and sort order for the retrieved data.) | ||
=== Partial Templates === | === Partial Templates === |
Revision as of 06:27, 18 August 2022
Introduction
Web Templates are the manner in which the Web Engine interfaces with a user, written in HTML with C# support using the Razor templating engine. Upon receiving a request from a user
Fields
Slug
The DataModel field is a name of an entity type, such as Equipment or Employee, which specifies the specific data to load for the page template. For example, if you are writing a page which lists employees, it is useful to preload the data necessary - this is done by setting DataModel to "Employee". This field is optional.
Slug
The Slug field denotes the name of the page itself, intended to be a short text field which describes the function of the page. Note that it cannot contain any slashes ('/', '\'), and should use only characters which are URL-safe; when in doubt, stick to just numbers and letters.
The DataModel and slug fields combine to form a path to the page, with the form www.domain.com/v1/DataModel/Slug?id=XXXXXXXX-XXXX-XXXX-XXXXXXXX. If the DataModel field is empty, then the URL is simply www.domain.com/v1/Slug. This allows you to create pages which do not have to be tied to a specific entity.
Description
The Description field is a non-functional field which allows you to add a description to the template - this is not necessary, but can be useful as a reference.
Visible
The Visible field denotes whether the page is accessibly via a URL - if it is not visible, then a 404 Not Found is returned when trying to access it - it is as if it didn't exist. Use this if you want to disable a page or if the template is only relevant in the context of other templates - see Partial Templates
Writing a Template
To write a template, you should be familiar with HTML, CSS and JavaScript as well as C#, which is used for non-static content.
Basics
At its base, a template is an HTML file - that is, that one can write a static web page, without the use of C#, and it would display as it would in a browser. Note that this has no means of accessing any resources outside of the page, so any styling with CSS must be embedded.
Styling
To add a stylesheet, you can create a WebStyle, which can be loaded with WebDatabaseInterface.GetStylesheet(string code)
, which retrieves the content of that stylesheet as raw text. Thus, to use a WebStyle with the code "MAIN", one would put in the <head>
tag:
<style>
@(WebDatabaseInterface.GetStylesheet("MAIN"))
</style>
This would load that stylesheet into the current web page.
If multiple stylesheets need to be loaded, consider using WebDatabaseInterface.GetStylesheets(params string[] stylesheetCodes)
, which gets all the stylesheets in request.
Using DataModels
The Razor engine provides to each template page a parameter called Model
, by which data can be retrieved. By default, it contains no data except for a table called "User", which contains the current user. To load the required data for the page, a call to Model.LoadModel(string[] requiredTables, params IDataModelQueryDef[] requiredQueries)
must be executed.
The first parameter, requiredTables
, specifies which tables should be loaded. By default, LoadModel
loads nothing. In general, the CompanyInformation, CompanyLogo and the current entity are useful to load, so a call could look like:
Model.LoadModel(new string[] {"CompanyInformation", "CompanyLogo", "Employee"})
.
The second parameter specifies a way to narrow the load of the data, preventing unnecessary data from being loaded. For example, if one only requires the ID and Name fields of an employee, one might call:
Model.LoadModel(<br> new string[] {"CompanyInformation", "CompanyLogo", "Employee"}, new DataModelQueryDef<Employee>( null, new Columns<Employee>(x => x.ID).Add(x => x.Name), null) )
By default, all columns in the database are loaded, so this is a way to drastically reduce the amount of data loaded.
(The other two fields, set to null
, specify a filter and sort order for the retrieved data.)