Customised templates: Difference between revisions

m
mNo edit summary
 
If a particular webpage on a local web server includes a script requiring loading a local file like 'dayfile.txt', then just for that webpage enter its remote web file name, and select Binary and File Transfer Process, as per the previous set of instructions. (The remaining pages will be copied across locally using the specified full path without FTP as per instructions directly above).
 
 
= Using PHP Hypertext Pre-processor =
 
Many web servers support the PHP scripting language, this means that the web server will parse the contents and obey all the script instructions to generate a pure HTML page that it forwards to a user agent (e.g. browser).
This Wiki article cannot teach you PHP, nor tell you if your web server supports it, but the examples below give a brief hint of how PHP can be used.
 
== Using PHP templates ==
 
This approach incorporates some PHP script in a page that still contains Cumulus web tags so still requires processing by Cumulus. This is suitable when you want to try PHP, but do not want a big redesign of your set-up.
*The file still contains the HTML shown at the start of this page, indeed it might be a simple edit of a standard cumulus template.
*It still contains Cumulus web tags, but as it is no longer a standard page, it has to be listed on the ''Files'' tab of the '''Internet''' screen within the 'Configuration' menu; the remote file name must have the extension ".php" so that the PHP processor will parse the file, before it is passed to the browser as a HTML page.
*The php script might be used for a decision more complicated than the [[Webtags_as_boolean_operators_in_HTML|boolean options]] that can be achieved just with standard HTML. For example, in the UK vehicle headlights must be used between half-an hour after sunset to half-an-hour before sunrise, so maybe you want to modify a standard "xxxxT.htm" page that is currently processed into "xxxx.htm". Change it to be processed into "xxxx.php" instead and incorporate 2 new chunks of code:
<pre>
&lt;?php // start of PHP script
// setting PHP variables using web tags available in Cumulus (version 1.x.x)
$latestDay = '<#metdate format=yyyy>'.'-'; # Year could be treated as integer converted to string, but easier to make it literal
$latestDay .= '<#metdate format=mm>'; # Read as literal so any leading zeros do not make it Octal integer
$latestDay .= '-'.'<#metdate format=dd>'; # Read as literal so any leading zeros do not make it Octal integer
$sunrise = "<#sunrise format=h:nn>"; # use format to remove any leading zero, so can not read it as Octal integer
$sunset = "<#sunset>";
// setting new PHP variables (not available in Cumulus web tags)
// end of lighting up time (hours of darkness)
$darkness_end = new DateTime($latestDay.' '.$sunrise); // sunrise
$darkness_end -> sub(DateInterval::createFromDateString('30 minutes')); // subtract 30 minutes
// start of lighting up time (hours of darkness)
$darkness_start= new DateTime($latestDay.' '.$sunset); // sunset
$darkness_start-> add(new DateInterval('P0Y0DT0H30M')); // add 30 minutes
?> <!-- end of PHP script -->
 
....
&lt;tr>
&lt;th title="Darkness times calculated by PHP">Darkness starts</th>
&lt;td>&lt;?php
echo ($darkness_start->format('g:i a'));
?></td>
</tr>&lt;tr>
&lt;th>Darkness ends</th>
&lt;td>&lt;?php
echo ($darkness_end->format('g:i a'));
?></td>
</tr>
</pre>
 
== Replacing HTML by PHP ==
 
#A more efficient approach, this is a bigger implementation change, is to set Cumulus to process just [[Php_webtags| something like this file]] to create PHP variables/arrays from the Cumulus web tags you want to use.
#Then you create a new PHP script that does not get processed by Cumulus. For example "thismonth.php" would start with the same HTML5 as shown above, but the table is output by the PHP script:
<pre>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="<#location> weather data" />
<meta name="keywords" content="Cumulus, <#location> weather data, weather, data, weather station" />
<title>&lt;?php
include 'cumuluswebtags.php';
echo "$location";
?>weather</title>
<link href="weatherstyle.css" rel="stylesheet" type="text/css" />
</head>
&lt;body>
&lt;div id="content">
&lt;h1>&lt;?php
echo "$location";
?> weather</h1>
....
 
&lt;table style="width:100%;border-collapse: collapse; border-spacing: 0;" >
&lt;caption>This month's highs and lows</caption>
&lt;tr>
&lt;td colspan="3" class="tableseparator_temp">Temperature and Humidity </td>
</tr>
&lt;tr class="td_temperature_data">
&lt;?php
echo "&lt;th>Highest</th>";
echo "&lt;td>$MonthTempH $tempunit</td>";
echo "&lt;td>$MonthTempHT on $MonthTempHD</td>";
echo "&lt;th>Lowest</th>";
echo "&lt;td>$MonthTempL $tempunit</td>";
echo "&lt;td>$MonthTempLT on $MonthTempLD</td>";
?>
&lt;/tr>
 
....
 
echo &lt;&lt;&lt; END_OF_QUOTE
&lt;tr>
&lt;td colspan="2" class="spacer">Pressure (sea level)</td>
</tr>
&lt;tr>
&lt;th class="labels">Lowest</th>
&lt;td class="site_data">$MonthPressL {$pressunit}</td>
&lt;td class="time_stamp">at $MonthPressLT on $MonthPressLD</td>
</tr>
&lt;tr>
&lt;th class="labels">Highest</th>
&lt;td class="site_data">$MonthPressH {$pressunit}</td>
&lt;td class="time_stamp">at $MonthPressHT on $MonthPressHD</td>
</tr>
</table>
END_OF_QUOTE;
 
....
 
</code>
Note that only excerpts from the code are shown. Note the two ways of getting PHP to output HTML code, either by using separate echo commands for each line or using the "heredoc" approach so that one echo can be applied to multiple lines. In the latter way, the syntax allows a space after echo and after <<< but does not allow any other spaces (or tabs) in the two enclosing lines (that precede and follow all the listed output).
5,838

edits