Jump to content

Generating forms - XML or PHP?


WebCM

Recommended Posts

I need a solution for CMS. I would like to edit and create FORMS easily, especially list of settings. Which is better and why?

 

XML or HTML

Forms occur in View layer - in presentation file. To simplify editing forms and to improve readability, webmasters use additional tags and attributes, e.g. <checkbox>, <radio>, arrayname... The compiler of templates changes XML code into (X)HTML with conditional expressions in PHP and variables (until the template class doesn't compile templates but parse them).

 

PHP

Form fields and their properties are defined in PHP logic code. Form class generates the HTML form. However, the class needs HTML code of each <form> element - perhaps from template

 

Examples: http://code.bulix.org/so4zlh-66117

 

Example of unclear code:

 

<input type="checkbox" name="name"<!-- IF name --> checked="checked"<!-- END --> />

A lot of such constructions make the code unclear. Similar in pure PHP:

 

<input type="checkbox" name="name" <?= $name ? 'checked="checked"' : '' ?> />

 

So I'm looking for a good solution for making <form>s. There are 2 or more methods - XML-based or PHP-based. Which is better and why?

 

XML-based - example of checkbox:
<checkbox name="name" />

PHP-based:
$form = new Form('...');
$form -> set( /*fields, etc. */);
$template -> set('form', $form);

Link to comment
Share on other sites

I would think using the Composite Pattern would be your best choice, here. More info: http://en.wikipedia.org/wiki/Composite_Pattern

What this gives is true dynamic rendering based on, say, a List, Map, Hash(array) from any sort of Datasource. I suppose you could add templating on top of that, but I am not experienced with the existing templating engines because I don't like UI work :)

 

<?php

$f = new Form();
$f->setHandler("submit.php");
$f->setAction("POST");

$f->add(new Checkbox("lol"));
$f->add(new Input($name="name", $value="Default text"))

$f->flush();

?>

Link to comment
Share on other sites

I recommend pure HTML for invariant parts of forms, only dynamic parts should be generated from PHP.

 

$f = new Form();
$f->setHandler("submit.php");
$f->setAction("POST");

 

Dreadful, if handler is always "submit.php" and action always "POST" ...

 

But if you want to prepare form skeletons and paste their HTML code into final application - this is the way to go.

Link to comment
Share on other sites

But, for a CMS, it surely isn't (always submit/post).

 

If you don't want that complexity, make it in the form constructor with default options? Or initialize it with default options?

 

I was just making a clear example so I don't get asked the opposite: "Where would I set action/method?!"

 

 

Link to comment
Share on other sites

  • 2 weeks later...

When creating forms I always try to use something that will do the code for me! No php classes, but a scaffold system! Which in my opinion is better if you are working with a designer. The code html code is generated and then the designer can do his stuff :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.