Registering Required Resources
Lynicon includes its own utility subsystem for registering the need of script files for certain resources, whether these be inline or included CSS or Javascript, or HTML snippets which should only appear once.
The functionality is accessed through some custom extensions to the HtmlHelper listed below:
@Html.RegisterScript(string script) @Html.RegisterScript(string id, string script, List<string> dependencies) @Html.RegisterHtmlBlock(string id, string html) @Html.RegisterHtmlBlock(string id, HtmlString html) @Html.RegisterLocalStyles(string id, string style) @Html.InsertIncludesBody() @Html.ScriptIsRegistered(string id) @Html.LocalStyleIsRegistered(string id) @Html.HtmlBlockIsRegistered(string id)
The RegisterScript functions add a script into the page once. Â If the 'script' argument starts with the string "javascript:" the script after this is taken to be Javascript to be inserted inline. Â If not, it is assumed to be the url for a script file. Â The second overload includes an id, which is set on the script element and used to identify where the same script is being registered from different places. Â It also includes a list of dependencies, which are the ids of scripts which this one depends on and which must precede it in the output file. Â Lynicon when sorting scripts in the file will sort the registered scripts to attempt to satisfy all such dependency requirements.
RegisterHtmlBlock adds a piece of HTML into the file (which may be at any point in the body), ensuring it is not repeated. Â This can be used for e.g. dialog boxes or overlays used in more than one place. Â Because this can take an MvcHtmlString as an argument, this argument could be the output of another Html.XYZ type call.
Scripts and html blocks registered in this way are inserted into the page at the point where Html.InsertIncludesBody() is called. This should be the last Html call on the page, inserting these items at the bottom of the <body> contents.
RegisterLocalStyles adds the style (or styles) given as the second parameter, using the first to avoid duplication. Styles are inserted directly at the point of the first call into the page in a <style> tag. This is not strictly correct HTML but works in nearly all browsers. Â It potentially slows page rendering, so avoid if there is a good alternative solution. ASP.Net Core sends markup to the output stream as soon as it is available and does not cache it for efficiency, this means we cannot output tags into the header once we are reading template script further down the page.
The last 3 calls are tests for whether a requirement is registered already which return a bool result.