As you might have noticed by now that engine X configuration syntax closely resembles that of a programming
language implementing in some ways the concept of scope includes and in this lesson in variables and
conditionals variables exist in two forms variables.
We set ourselves and engine exis own built in variables we can find a list of these engine X variables
in the engine X dot org documentation alphabetical index of variables which lists each of the available
variables with the module that makes it available modules like hgt underscore core and h.t. dp log.
Really being part of engine X so you don't have to manually add them.
Now these native variables can be extremely useful.
So let's switch back to that configuration file and see how we could use them.
I'll clear all of these locations from the previous lesson create a new location for Slash inspect.
And in this location simply return a few of these.
Engine X variables return status 200 a string and using double quotation marks.
We can directly embed variables into this response string.
First one I'll add is the host variable variables being prefixed with a dollar sign.
Add a new line character.
You are a new line and ARGs which is the requests query string save this and reload the configuration.
Navigate to that new location.
Slash inspect.
And we see each variable on its own line.
The host which is the server I see as I'm not using the domain name and the request.
You are I slash inspect.
Of course we didn't pass a query string so the args variable is empty.
But if I add query name equals Ray that's now also returned albeit in its complete string form.
We can however use a variation of the args variable to get individual query parameters.
I'll change this string to name.
Argh underscore name.
So based on the query string engine it compiles a named variable for each parameter prefixed with Arg
save and reload this.
Reload and this time we get the value of the name parameter.
So already a very powerful feature set of engine X engine X also allows us to perform basic conditionals
or if statements which most commonly will be used in conjunction with one of these variables before
I demonstrate however.
Note that the use of engine X conditionals inside location contexts is highly discouraged as this can
lead to some very unexpected behaviour.
Now the technicalities of this is beyond the scope of what we can cover here but if you'd like to read
more on the subject I've linked to a very detailed explanation in the list and resources.
Righto.
Outside of this location context then I'll create a conditional which for the sake of demonstrating
chicks are hypothetical AP Iki if with braces.
Note the space between the F and the braces being required for this syntex to be valid.
Argh underscore a p Iki.
So checking the value of the API key query parameter not equal to 1 2 3 4.
Meaning if the user does not pass the value 1 2 3 4 as the API key parameter in the query string to
any you are a return of foro 1 which is the unauthorized status code.
Incorrect API key.
Save this.
Reload with alt that API parameter key this time just requesting the root.
You are right.
So their demo site and we're denied.
As per that conditional add the API key parameter.
However with a value of 1 2 3 4 and were served indexed update the amelle as the conditional did not
evaluate to true image and style sheet simply being missing as the internal requests for those didn't
include the API key parameter.
OK as I mentioned we can also set our own variables within range and X configuration.
I'll get rid of all this and to create a new variable we can say set the variable name.
I'll make this weekend and the variable value.
No so no equals sign.
Just the standard engine X director format variables can be set to simple strings such as this example
integers or booleans.
True and false then for another hypothetical use case we can check if it is weakened by creating an
if statement and using the date underscore local engine x variable.
This variable holding an ISO date time string for the local time and instead of a simple equals or not
equals operator.
We can use a regular expression as with location contexts folder checking on this variable for Saturday
or Sunday.
In which case if this is true set weekend 2 Yes.
So starting out we said weekend to be No.
Check the local date to see if it is in fact Saturday or Sunday which if it is we changed the value
of weekend to be yes.
Change this location to is WEEKEND.
Returning two hundred and the weekend variable.
So yes or no.
Save and reload the configuration.
Navigate to that you are I is under school weekend and it being a Monday at the time of recording this
we get no.
Change the condition to check for Monday instead then.
I'll rename this variable to mun.
And said the location to is Monday.
Reload.
Navigate to is Monday.
And this time we get yes as this conditional evaluator.
True true and updated the variable to be yes in the next place and we'll use some of these variables
to learn about rewrites and redirects.