|
Previous: GET-POST Variables
Generating HTML In PHPThe Andromeda philosophy is to eliminate and automate as many tasks as possible, but every project will always require a little custom work. Some of this work will involve generating HTML, so Andromeda needs a simple and clean way to do this. The Andromeda HTML library is a very small set of functions that allows you to generate very complex HTML without messy "echo" commands, and without a jumble of interspersed HTML and PHP. Introducing The HTML FunctionThe basic function for creating an HTML element is simply "html()". This function returns an object that can then contain other HTML elements. The first parameter is the HTML tag, the second parameter is the parent (optional) and the third parameter is the (optional) innerHTML value. <?php
$div = html('div',null,'This is the innerHTML!');
$div->render();
?>
HTML elements can be nested inside of each other to any depth (up to what your hardware can support). When you have finished building your HTML, you always invoke the "render" method on the top-most object. You can also use the "renderBuffered" method to capture the output. <?php
$table = html('table');
$tr = html('tr',$table);
html('td',$tr,'This is td 1 of 2');
html('td',$tr,'This is td 2 of 2');
# This will render the entire table
$table->render();
# Return the output as a string
$html = $table->bufferedRender();
?>
Setting the inner HTMLThe html object returned by the html() function has a special method, setHTML(), for setting the innerHTML of the object. You can also set it directly by assigning to the "innerHtml" (note capitalization) of the object's "hp" array: <?php
$div = html('div');
$div->setHtml("I am now setting innerHTML");
# Or, you can set the "hp" array
# (hp standards for HTML Properties)
$div->hp['innerHtml'] = ['I am overwriting the innerHTML set above'];
?>
Setting the CSS ClassYou can assign multiple classes to an object using the addClass() method. <?php
$div = html('div');
$div->addClass('class1');
$div->addClass('class2');
# oops, let's change our mind:
$div->removeClass('class1');
?>
Setting Other HTML AttributesYou can set any HTML attributes by simply assigning values to the object's "hp" array, as indicated here: <?php
$textarea = html('textarea');
$textarea->hp['tabindex'] = 500;
$textarea->hp['cols'] = 60;
$textarea->hp['rows'] = 20;
$textarea->render();
?>
Setting Custom AttributesYou can set custom attributes on objects also, by making assignments to the object's "ap" array: <?php
$textarea = html('textarea');
$textarea->hp['id'] = 'customers_notes';
$textarea->ap['table_id'] = 'customers';
$textarea->ap['column_id']= 'notes';
$textarea->render();
?>
When accessing this object in Javascript, you do not need the "getNamedAttributes()" funtion, you can access the properties directly: // Javascript example
var widget = $("#customers_notes")[0];
alert(widget.table_id);
A Shortcut for Void HyperlinksSometimes you want to create hyperlinks with no href, and then put the action into the click event. You can do this by creating an "A-VOID" element: <?php
$a = html('a-void');
$a->hp['onclick']='javascript:alert("In the onclick!")';
?>
Creating Multiple ElementsYou can pass in an array of values to the html function and it will make one element for each. When you do this the return value is an array: <?php
$row = SQL_OneRow("Select * from customers where skey=5");
$table = html('table');
$tr = html('tr',$table);
# $aTr will be an array of td objects
$aTr = html('td',$tr,$row);
?>
Creating InputsThe html() function can be used to generate your own input elements, but there is also a special function that uses the Data Dictionary to generate inputs that completely respect the data dictionary. This is the "input()" function, which returns either an HTML INPUT, and HTML TEXTAREA, or an HTML SELECT depending on the column you pass in: The input's ID and several extended parameters are set automatically. You must set a tab index. <?php
$customers_dd = ddTable('customers');
$table = html('table');
$tabindex = 500;
foreach($customers_dd['flat'] as $colname=>$colinfo) {
$tr = html('tr',$table);
// First column is the description
$td = html('td',$tr,$colinfo['description']);
// Second column is the input
$input = input($colinfo);
$input->hp['tabindex'] = ++$tabindex;
$td = html('td',$tr,$input->bufferedRender());
}
?>
Previous: GET-POST Variables
|
