XPHP is an ever-growing library of scripting statements that you can use for powerful template control, widget behaviors, and more in LiveWhale.

In the below statements, you can use any of the XPHP variables from the XPHP Dictionary, or item-specific variables in widget results and details pages (i.e., "profiles_45" might be one of your custom profile fields).

XPHP Logic Operations

If/Then Example

The following examples demonstrate first, a simple check (is variable “X” empty?) and a more complicated one, using some of the tricks from the conditional functions below.

<xphp content="true">
   <if var="X" value=""/>
        <!– do something here –>
<xphp content="true">
   <if var="server_path" value="{group_directory}index.php"/>
        <!– show this if it's the group homepage –>

Whatever is in-between <content></content> will be shown if the if statement is true. It could be simple HTML, a widget, or more XPHP code.

If/Then Shorthand

The above syntax can be shortened to <xphp if . For example:

<xphp if="X">Variable X exists.</xphp>
<xphp if="X" value="Y">Variable X has value Y.</xphp>
<xphp if="X" matches="Y">Variable X contains the text Y.</xphp>


If/Else Example

Below is an example using If/Else in xphp to display a default image in your profile details page when the person does not have a photo:

<xphp content="true">
  <if var="profiles_image"/>
      <xphp var="profiles_image"/>
  <else content="true">
      <img src="/path/to/default_image.png"/>

In this example, we see we can use <xphp var="foo"/> to display an XPHP variable directly.

AND & OR Operators

By default, XPHP if statements are treated as ANDs when two or more variables must meet the conditions set, but you can switch to an OR relationship with the addition of only one attribute on the surrounding <xphp> tag.

<!– the default AND state –>
<xphp content="true">
  <if var="a"/>
  <if var="b"/>
      <p>A and B are both non-empty.</p>
<!– with the addition of the ifmode="or" attribute, the comparison becomes an OR –>
<xphp content="true" ifmode="or">
  <if var="a"/>
  <if var="b"/>
      <p>Either A or B is non-empty.</p>

XPHP Conditional Functions

You can alter the <if> statements above, mixing and matching from the following library, to execute all sorts of logical tests:

Description Code
Does variable foo exist and have a non-empty value? <if var="foo"/>
Does variable foo not exist or have an empty value? <if var="foo value=""/>
Does variable foo equal “bar”? <if var="foo" value="bar"/>
Does variable foo not equal “bar”? <if var="foo" not_equals="bar"/>
Does variable foo equal variable bar? <if var="foo" value="{bar}"/>
Does variable foo (a comma-separated list) contain value “bar”? <if var="foo" contains="bar"/>
Does variable foo contain the text “bar” anywhere in it? <if var="foo" matches="bar"/>
Does variable foo not contain the text “bar” anywhere in it? <if var="foo" not_matches="bar"/>
Does the tag foo exist on this page/item? <if var="has_tag_foo"/>
Does the tag foo not exist on this page/item? <if var="has_tag_foo" not_matches="1"/>
Is variable foo (a number) less than 100?
For dates, can use “today”, “yesterday”, etc as values.
<if var="foo" less_than="100"/>
Or: <if var="foo" before="yesterday"/>
Is variable foo (a number) greater than 100?
For dates, can use “today”, “yesterday”, etc as values.
<if var="foo" greater_than="100"/>
Or: <if var="foo" after="today"/>
Is variable foo (a number) in between 5 and 7 (inclusive)? <if var="foo" range="5 to 7"/>
Does variable foo match the regular expression "~^This.+?test"?~"? <if var="foo" regexp="^This.+?test"/>
Does variable foo not match the regular expression "~^This.+?test"?~"? <if var="foo" not_regexp="^This.+?test"/>

Using other variables (POST, GET) in your if statements

By default, the above tests will check for your variables in the XPHP Dictionary (also known as $_GLOBALS in PHP). You can use the type attribute to check variables from server, get, or post.

Variable Code
From URL string: ?foo=bar <if var="foo" type="get" ...
From form (POST) to page <input name="foo"> <if var="foo" type="post" ...
From the PHP server variables <if var="foo" type="server" ...

Checking for elements on the page

You can also use XPHP to check for certain elements in the source code of the current page, using the has_element function. Your check can match any valid single CSS selector in the following forms:

<if has_element="span"/>
<if has_element="#aaa"/>
<if has_element=".bbb"/>
<if has_element="span#aaa"/>
<if has_element="span.bbb"/>
<if has_element="span#aaa.bbb"/>
<if has_element="#aaa.bbb"/>
<if has_element="#aaa.bbb.ccc"/>

This is especially useful if, for example, you want a section of your page to display only if <div id="sidebar"> exists ( <if has_element="div#sidebar"/> ).

Note: The element must exist in the page before the <if> statement in question is reached. This may mean, if you have other page changes being output from another XPHP statement, it could not be caught by this logical test.

XPHP Tools

In addition to the above conditional statements and logical tests, XPHP has several tools that you can use to make your website more responsive, intelligent, or fun.

Function Code
Display a random integer from 5 to 10. <xphp random="5,10"/>
Limit the output of variable foo to 10 words. <xphp var="foo" words="10"/>
Limit the output of variable foo to 10 characters. <xphp var="foo" length="10"/>
Limit the output of a variable to a specific substring, following the offset, length syntax for substr (LiveWhale 2.0+) <xphp var="foo" substring="2,8"/>

Advanced: Extracting specific HTML elements from an XPHP variable

Sometimes you may want to extract only a specific HTML object from an existing XPHP variable. In LiveWhale, you can use selector="" in your <xphp> variables (and, in widget formats, with <field> variables) to do just that.

This advanced tool supports the XPath syntax (cheatsheet). selector="//foo will grab the element <foo> wherever it appears. See below for some examples.

Function Selector Code XPHP Example
Display only the h2 tags selector="//h2" In a details template:
<xphp var="details_body" selector="//h2"/>

In a widget format:
<field var="body" selector="//h2"/>
Display only the first paragraph tag selector="//p[1]" In a details template:
<xphp var="details_body" selector="//p[1]"/>

In a widget format:
<field var="body" selector="//p[1]"/>
Display only the last paragraph tag selector="//p[last()]" In a details template:
<xphp var="details_body" selector="//p[last()]"/>

In a widget format:
<field var="body" selector="//p[last()]"/>
Display only divs with class=”special” selector="//div[@class='special']" In a details template:
<xphp var="details_body" selector="//div[@class='special']"/>

In a widget format:
<field var="body" selector="//div[@class='special']"/>
Display only h3s inside of <aside> inside of <main> selector="//main/aside/h3" In a details template:
<xphp var="details_body" selector="//main/aside/h3"/>

In a widget format:
<field var="body" selector="//main/aside/h3"/>

Timely Content

You can use XPHP to show content before/after a certain date, or daily before/after a certain time.

You can define timely content with the simple attributes start and end . You can also preview timely content from other dates/times by adding ?lw_preview_date= to your URL. e.g., /my-page/?lw_preview_date=April 7

You can use any PHP-readable date/time strings in your schedules and preview links (dates, dates with times, or just times for daily repeating schedules).

<xphp content="true" end="2020-11-28">
<content>Countdown to our Day of Giving...</content>
<xphp content="true" start="2020-11-28">
<content>Today is Day of Giving!</content>
<xphp content="true" end="12pm">
<content>Good morning!</content>
<xphp content="true" start="12pm" end="5pm">
<content>Good afternoon!</content>
<xphp content="true" start="5pm">
<content>Good night!</content>

Using XPHP Variables Inside Quotes

You can use the following XHTML-friendly syntax to place XPHP variables inside other XPHP or HTML tags and attributes. You do so by replacing <xphp var="my_variable" /> with %%​xphp_my_variable%% . For example, if you have <xphp var="start_date" /> set, you can use:

<xphp content="true" after_date="%%xphp_start_date%%">
<content>Today is Day of Giving!</content>

Or even in simpler cases, you can use the %% format where you want to nest XPHP code inside other tags:

This is the URL: <xphp var="href" />
<a href="%%xphp_href%%">Read more</a>

Note, this syntax only works for individual XPHP variables and not for larger chunks of XPHP logic.

Specifying Type and Cast

Whether using the normal <xphp syntax or the in-quotes %%xphp_ format, you can also request variables of alternate types (GET, POST, COOKIE, SERVER) and casts (dashed, encoded, decoded, text, urlencoded, obfuscated, uppercase, lowercase, uppercase-first, uppercase-words, int, bool, float, string).

When in quotes, the format is %%xphp_variable|TYPE|CAST:value%%
with |TYPE and |CAST:value both being optional. You can also use multiple casts by separating them with spaces. A few examples are below:

<xphp> syntax %%xphp%% syntax
<xphp var="foo" /> %%xphp_foo%%
<xphp var="foo" type="GET" /> %%xphp_foo|GET%%
<xphp var="foo" cast="urlencoded" /> %%xphp_foo|CAST:urlencoded%%
<xphp var="foo" cast="urlencoded lowercase" /> %%xphp_foo|CAST:urlencoded lowercase%%
<xphp var="foo" type="POST" cast="lowercase" /> %%xphp_foo|POST|CAST:lowercase%%

XPHP Cast Dictionary

Cast Description Example Code
dashed Replaces spaces, underscores, and / with dashes and removes all non alphanumeric characters (appropriate for using as an ID or CSS class name) “Department of Biology, Chemistry, & Physics” => department-of-biology-chemistry-physics <xphp var="foo" cast="dashed"/>
encoded Encodes all HTML entities, quotes, and ampersands & => & <xphp var="foo" cast="encoded"/>
decoded Decodes any HTML entities in the string & => & <xphp var="foo" cast="decoded"/>
text Formats the string as text (removes all HTML tags) <h2>Homepage</h2> => Homepage <xphp var="foo" cast="text"/>
urlencoded URL-encodes the string (all non-alphanumeric characters are replaced with % versions) “Department of Journalism & Public Relations” => Department%20of%20Journalism%20
<xphp var="foo" cast="urlencoded"/>
obfuscated Obfuscates the string using AES-256 encryption “Default Group” => “MHpEYmdmeS80S2pJZkJ2VnN4aVFoUT09” <xphp var="foo" cast="obfuscated"/>
int, bool, float, string Reformats the variable as an integer, boolean, float, or string “1.0” as int => “1”
“1.0” as bool => true
<xphp var="foo" cast="int"/>
<xphp var="foo" cast="bool"/>
lowercase Reformat string to all lowercase “Welcome” => “welcome” <xphp var="foo" cast="lowercase"/>
uppercase Reformat string to all uppercase “Welcome” => “WELCOME” <xphp var="foo" cast="uppercase"/>
uppercase-first Reformat string to capitalize first word “request more information” => “Request more information” <xphp var="foo" cast="uppercase-first"/>
uppercase-words Reformat string to capitalize every word “request more information” => “Request More Information” <xphp var="foo" cast="uppercase-words"/>
flatten If variable is an array, return as , -separated list Array containing “tag 1”, “tag 2”, and “tag 3” => “tag 1, tag 2, tag 3” <xphp var="foo" cast="flatten"/>
json Formats the variable as JSON Array containing “tag 1”, “tag 2”, and “tag 3” => ["tag 1", "tag 2", "tag 3"] <xphp var="foo" cast="json"/>
alphanumeric Formats the string to allow only a-z, A-Z, and 0-9 (LiveWhale 2.12+) “2024 Commencement - Ceremony, Awards, & Dismissal” => “2024CommencementCeremonyAwardsDismissal” <xphp var="foo" cast="alphanumeric"/>

You can also re-cast variables in widget formats—for example, by replacing {title_clean} with <field var="title_clean" cast="uppercase"/> .

XPHP Variables

Different XPHP variables get populated depending on context. For a full list, see the XPHP Variables Dictionary.

On this page