0

I'm trying to implement a simple templates engine with PHP. Everything works pretty nice except my form generator. I'm trying to render different form inputs based on the field type I get from backend. This is what I have got so far. It crashes with message

Parse error: syntax error, unexpected 'case' (T_CASE) in /var/www/cap/App/Template/form.php on line 34.case 'text': ?>` is my line 34.

It crashes no matter how I arrange the order of case statements - always at the begining of second case.

<table class="showTable">
    <?php foreach ($vars['fields'] as $name => $f) { ?>
        <tr>
            <td class="label"><?php echo $f['fancy']; ?></td>
            <td>
                <?php if(isset($f['readonly']) && $f['readonly'] === true) { ?>
                <?php echo $vars['entity']->$name; ?>
                <?php } else {
                    switch($f['type']) {
                        case 'string': ?>
                            <input type="text"
                                   name="entity[<?php echo $name; ?>]"
                                <?php if(isset($f['required']) && $f['required'] === true) {?> required<?php} ?>
                                   value="<?php echo $vars['entity']->$name; ?>" />
                            <?php break;
                        case 'text': ?>
                            <textarea name="entity[<?php echo $name; ?>]"
                                <?php if(isset($f['required']) && $f['required'] === true) {?> required<?php} ?>>
                                <?php echo $vars['entity']->$name; ?>
                            </textarea>
                            <?php break;
                        case 'float': ?>
                            <input type="number" step="0.01"
                                   name="entity[<?php echo $name; ?>]"
                                <?php if(isset($f['required']) && $f['required'] === true) {?> required<?php} ?>
                                   value="<?php echo $vars['entity']->$name; ?>" />
                            <?php break;
                        case 'integer': ?>
                            <input type="number" step="1"
                                   name="entity[<?php echo $name; ?>]"
                                <?php if(isset($f['required']) && $f['required'] === true) {?> required<?php} ?>
                                   value="<?php echo $vars['entity']->$name; ?>" />
                            <?php break;
                    }
                } ?>
            </td>
        </tr>
    <?php } ?>
</table>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
kacper3w
  • 84
  • 1
  • 10

1 Answers1

0

I can appreciate keeping a template as primarily HTML. I couldn't spot the error, because your code unfortunately is hard to follow from all the in-and-out of PHP, plus some verbosity in the code. So my advice is to simplify your code, and keep it DRY (don't repeat yourself)

There's also a few conventions that make it easier (IMHO) to read when you are writing primarily in HTML:

  1. Braces are hard to follow. Use the more verbose <?php if(condition): ?> ... markup ... <? endif; ?>
  2. Keep open and closing php tags on the same line
  3. Simplify your printing out of PHP variables: <?= $variable ?> is a more concise way than to write out the visually noisy <?php echo $variable; ?>

I tried my hand at writing the template, and I found that switch case just didn't lend itself to making the template easy to read; it's probably because switch is more a logic flow thing than a simple "show/don't show" decision.

Perhaps this will be useful to you. It doesn't answer your question per se, but it might help you get past it.

<table class="showTable">
    <?php foreach ($vars['fields'] as $name => $f): ?>
        <tr>
            <td class="label"><?= $f['fancy'] ?></td>
            <?php 
            // switch between read only, string,text, float, and integer
            // 'case' statement makes great sense in PHP script, 
            // but is not so intuitive in a template

            // reduce complicated, repeated terms to a simple variable
            $type     = isset($f['readonly']) && $f['readonly'] === true) ? 'readonly' : $f['type'];
            $required = isset($f['required']) && $f['required'] === true) ? 'required' : '';
            $value    = $vars['entity']->$name;

            // present alternate html for each presentation choice
            ?>
            <td>
              <?php if($type=='readonly'): ?>
                <?= $value ?>

              <?php elseif($type=='string'): ?>
                <input type="text" name="entity[<?= $name ?>]" <?= $required ?> value="<?= $value ?>">

              <?php elseif($type=='text'): ?>
                <textarea name="entity[<?= $name ?>]" <?= $required ?>>
                  <?= $value ?>
                </textarea>

              <?php elseif($type='float'): ?>
                <input type="number" step="0.01" name="entity[<?= $name ?>]" $required value="<?= $value ?>" />

              <?php elseif($type='integer'): ?>
                <input type="number" step="1" name="entity[<?= $name ?>]" $required value="<?= $value ?>" />

              <?php endif; ?>
            </td>
        </tr>
</table>
Tim Morton
  • 2,614
  • 1
  • 15
  • 23