new HTML_Template_Flexy_Element

new HTML_Template_Flexy_Element – Class constructor

Synopsis

require_once 'HTML/Template/Flexy/Element.php';

new HTML_Template_Flexy_Element ( string $tag = '' , array $attributes = null )

Description

Flexy uses a single lightweight class to represent All HTML Tags, All the variables of the class are public, and you are encouraged to use them. And the methods provide generic assignment and conversion abilities.

To force the toHtml() method to generate XHTML, rather than standard HTML, use $element->setAttributes(array('flexy:xhtml'=>true)); or add flexy:xhtml="true" to the attribute of the element in the template.

Parameter

  • $tag - The name of the HTML Tag eg. img for <img ....

  • $attributes - Associative array of attributes, where key="value" is output when you turn it in toHtml(), If you need to represent a attribute without a value, use TRUE as the value. This also accepts a string in the format "href='/test.jpg' alt='test'", which will be parsed into the attributes array of the object.

Public Properties

string $element->tag

The name of the html element eg. img for <img...

array $element->attributes

Attributes for the element

array $element->children

All the sub elements inside this, can be any object that implements toHtml(), or a string.

string $element->override

this value of thiswill be output when toHtml() is called, rather than the tags.

string|object $element->prefix

string or object that implements toHtml() method, and is returned by toHtml() before the tag HTML

string|object $element->suffix

string or object that implements toHtml() method, and is returned by toHtml() after the tag HTML

mixed $element->value

when you create an element, that is to be merged later with a full definition, you can assign the value here, and during toHtml(), the toValue() method will be called and select options, checkboxes and input values will be correctly filled in.

Throws

throws no exceptions thrown

Note

This function can not be called statically.

Example

Using an element to change a template.

<?php
$form 
= new HTML_Template_Flexy();
$form->compile('some_file_name');

// create an instance (note you dont have to specify any details..)

$elements['test'] = new HTML_Template_Flexy_Element;

// change an attribute
$elements['test']->attributes['class'] = 'bold';

// sets the value
$elements['test']->setValue('Fred');


// wrap it with something
$elements['test']->prefix '******';
$elements['test']->suffix '!!!!!!';



// for the different types of elements:
$elements['test_textarea'] = new HTML_Template_Flexy_Element;
$elements['test_textarea']->setValue('Blogs');


// select options
$elements['test_select'] = new HTML_Template_Flexy_Element;
$elements['test_select']->setOptions( array(
  
'123' => 'a select option',
  
'1234' => 'another select option'
));
$elements['test_select']->setValue('1234');

// checkboxes
$elements['test_checkbox'] = new HTML_Template_Flexy_Element;
$elements['test_checkbox']->setValue(1);
$elements['test_checkbox']->setAttributes(array('flexy:xhtml'=>true));

// array type checkboxes..
$elements['test_checkbox_array[]'] = new HTML_Template_Flexy_Element;
$elements['test_checkbox_array[]']->setValue(array(1,2));

// radio buttons
$elements['test_radio'] = new HTML_Template_Flexy_Element;
$elements['test_radio']->setValue('yes');


$form->outputObject(new StdClass$elements);

// in the example below, the new data you have added is to the existing attributes
?>

template example

<body>
  <form name="xxxx">

    <input name="test" length="12">

    <textarea name="test_textarea"></textarea>

    <select name="test_select"></select>

    <input name="test_checkbox" type="checkbox" value="1">

    <input name="test_checkbox_array[]" type="checkbox" value="1" />1<br />
    <input name="test_checkbox_array[]" type="checkbox" value="2" />2<br />
    <input name="test_checkbox_array[]" type="checkbox" value="3" />3<br />

    <input name="test_radio" type="radio" id="yes" value="yes" />yes<br />
    <input name="test_radio" type="radio" id="no" value="no" />no<br />

  </form>
</body>

output from the Template

<body>
  <form name="xxxx">

    ******<input name="test"  length="12" class="bold" value="fred">!!!!!!

    <textarea name="test_textarea">blogs</textarea>

    <select name="test_select">
      <option value="123">a selection option</option>
      <option value="1234" selected>another selection option</option>

    </select>

    <input name="test_checkbox" type="checkbox" value="1" checked="checked" />

    <input name="test_checkbox_array[]" type="checkbox" value="1" checked>1<br />
    <input name="test_checkbox_array[]" type="checkbox" value="2" checked>2<br />
    <input name="test_checkbox_array[]" type="checkbox" value="3">3<br />

    <input name="test_radio" type="radio" id="yes" value="yes" checked>yes<br />
    <input name="test_radio" type="radio" id="no" value="no">no<br />


  </form>
</body>