Editing
PRS/WebTemplate
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== 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. The page is then able to contain embedded C# code, using the Razor syntax. Roughly, one can embed a C# expression by preceding it with an <code>@</code>, e.g. <code>@employee.Name</code>. If the expression is too complex, surround it with parentheses, <code>@(GenericMethod<string>())</code>. To execute blocks of code, use <code>@{ ... }</code>, and for control structures like <code>if</code> and <code>foreach</code>, the <code>@</code> should come before the keyword, e.g. <code>@if(condition){ ... }</code>. Within a block of code, one can return to HTML simply by using HTML tags, e.g. <pre> @if(condition){ <p>Condition is true</p> } else { string error = "Condition is false"; <p>@error</p> } </pre> === Styling === To add a stylesheet, you can create a [[PRS/WebStyle|WebStyle]], which can be loaded with <code>WebDatabaseInterface.GetStylesheet(string code)</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 <code><head></code> tag:<br> <code> <style> @(WebDatabaseInterface.GetStylesheet("MAIN").Style) </style> </code><br> This would load that stylesheet into the current web page.<br>If multiple stylesheets need to be loaded, consider using <code>WebDatabaseInterface.GetStylesheets(params string[] stylesheetCodes)</code>, which gets all the stylesheets in request. === Using DataModels === The Razor engine provides to each template page a parameter called <code>Model</code>, 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 <code>Model.LoadModel(string[] requiredTables, params IDataModelQueryDef[] requiredQueries)</code> must be executed. The first parameter, <code>requiredTables</code>, specifies which tables should be loaded. By default, <code>LoadModel</code> loads nothing. In general, the CompanyInformation, CompanyLogo and the current entity are useful to load, so a call could look like:<br> <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: <pre> Model.LoadModel( new string[] {"CompanyInformation", "CompanyLogo", "Employee"}, new DataModelQueryDef<Employee>( null, new Columns<Employee>(x => x.ID).Add(x => x.Name), 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. (The other two fields, set to <code>null</code>, specify a filter and sort order for the retrieved data.) ==== Retrieving Data ==== To get a table that has been loaded, use <code>Model.GetTable<T>(alias)</code>. This returns an object of type <code>CoreTable</code>, which has a property <code>Rows</code>, through which one can retrieve data. An example is shown here. <pre> Model.LoadModel(new string[] {"Employee"}); // Load Model CoreTable employeeTable = Model.GetTable<Employee>(); // Get Table from Model // Iterate through the rows foreach(CoreRow row in employeeTable.Rows){ // Convert the row to an Employee. This is not necessary, and the data can also be retrieved with row[ColumnName] or row.ToDictionary()[ColumnName] var employee = row.ToObject<Employee>(); <p>@employee.ID: @employee.Name</p> } </pre> === Partial Templates === Sometimes it is useful to write a ''partial template'', that is, one that only represents part of a web page and is used elsewhere, e.g. a header which is used site-wide. This is possible. First, create a web template with the Visible property unchecked. Leave the DataModel blank, since that is only useful when loading the page from an HTTP request. For this example, give it the Slug ''header'', and set its content to be: <pre> <header> <h1>The Title</h1> </header> </pre> Then, within the main page, call <code>@Raw(WebHandler.RunTemplate(WebDatabaseInterface.GetWebTemplateBySlug("header"), Model))</code> Through this, the header will be inserted into the main page. Note that @Raw is required, for otherwise the retrieved HTML will be displayed as plain text, rather than interpreted as HTML. This method can also be used for loading stylesheets which must load styling information non-statically. Simply save the stylesheet as a WebTemplate instead of as a WebStyle, and the load it as a template.
Summary:
Please note that all contributions to PRS may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
PRS:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
Edit source
View history
More
Refresh
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information