Skip to content

TRlxRazorProcessor Component

Marco Cantu edited this page Sep 30, 2016 · 1 revision

This is the core component, capable of taking a HTML file embedding the razor notation and converting it to plain HTML after processing the @ tags.

Properties:

InputFilename, a string with the name of the file (either the full path of the relative path to the application)

**DataObjects **(readonly), a dictionary holding a collection of objects that can provide data to the processor. These can be either Delphi objects or datasets. The dictionary ties a name (used to refer to these objects inside the HTML) to the object itself. In case of an actual object, the second part of the dot notation will refer to a property and the engine uses RTTI to read the value. In case of a dataset the second part of the dot notation will refer to the name of a field, and the engine will use AsString to access the field value.

UserLoggedIn, indicates if the user is logged in. This is used when processing the @LoginRequired element. In case the property is false, the engine raises an exception of type ERlxLoginRequired.

LanguageId, is an integer ID referring to the current language in use while processing this page. The same page, in fact, can have tokens (with the @lang prefix) to access text tied to a translation engine, which is not included but can be easily plugged in.

RazorEngine, optionally the engine this processor is tied to. It can be omitted. Some of the local event handlers and configuration settings can be specified in the engine rather than in individual processor components.

**Request **(public), the TWebRequest originating the processing. When this component is placed in a web module and tied to an action, the information is extracted from the internal Dispatcher, and there is no need to set this property. (I think it could be removed is all cases, and replaced with code fetching the Request, but it is here for now.)

DictionaryDuplicates, indicates what to do in case you are adding the same “name” twice to the internal dictionary. Alternative options are ddIgnore (default), ddReplace, and ddError. Actual code is in the AddToDictionary method.

Methods:

**Create **and Destroy, provide initialization and cleanup

Content, returns a string with the transformed HTML, can be used as the value of the Response.Content. However, you can also connect a RazorProcessor directly to the action, as it supports the standard IProduceContent content interface (it inherits from TCustomContentProducer).

DoBlock processes a string with razor code. This is used internally for processing loops and conditional statements, but could also be invoked directly.

**AddToDictionary **is a configuration function used to add objects to the processor, and provide data to the script. Its parameters are the name of the object used in the script, the object itself, and a Boolean value indicating if the script owns the object and is responsible for deleting it at the end.

**InDictionay **is a function returning if a given name is already used, to avoid adding duplicates (like adding the same object twice).

Events:

OnValue: triggered when the @object.value notation refers to an object name that is not in the dictionary. If the handler is missing, the component will forward the event to the corresponding event of the engine (if connected).

OnWarn: triggered when there is a language error or any other unexpected behavior. Could be used to write to the user interface, or more likely to log to a local file (possibly using the CodeSite support built into Delphi XE). Again, if not defined this is forwarded to the engine.

OnLang: triggered for the @lang tag. The second element after the dot is used as token to be processed. The event handler can use the current language ID to determine the actual output. Again, if not defined this is forwarded to the engine.

Clone this wiki locally