Seeking general advice: is there a better way to echo dynamic HTML than heredoc? (smarty / php / html)

856

Project: To create dynamic multi page form that populates the fields in different ways depending on various situations.

Context: Using CMSMS and Smarty tags to insert PHP into the requried pages. Each single page of the form is being built using a single smarty

Current Method: Using smarty tags, I am writing large chunks of HTML echoed in heredoc notation. When I reach a part of the form that needs to be dynamically genearted (such as a drop down menu) I escape the heredoc, write the PHP (using standard echo functions to generate the HTML required for that form element) then return to heredoc for more chunks of HTML.

Suspicion: this is inelegant, messy, tedious and just feels wrong.

Request: Any idea of a better way of doing this?

EDIT: An example of a chunk of my current setup, here you can see an initial chunk of static HTML is echoed - (the start of the form, and the first question) then a drop down menu which requires dynamic generation depending on the data that exists in the SESSION variables. After this, the heredoc resumes and echos more static HTML:

echo <<<EOD
<form id="myform" method="post" action="?page=2">
<div >

<div >
  <div ><label for="sv_01">Question?</label> <input type="text" name="sv_01" value="$sv_01" size="10" maxlength="10" /></div>
    <div ><label for="sv_02">What Year?</label>
EOD;
        echo '<select name="sv_02">';  
        $vars = array(
        '-Year' => 'Year',
        '-2012' => '2012', 
        '-2011' => '2011', 
        '-2010' => '2010', 
        '-2009' => '2009', 
        '-2008' => '2008', 
        '-2007' => '2007', 
        '-2006' => '2006', 
        '-2005' => '2005', 
        'Pre 2005' => 'Pre 2005', 
        );

        foreach($vars as $val => $name){
          if($_SESSION['sv_02'] == $val){
            echo '<option value="' . substr($val, 0, 1) . '" selected>' . $name . '</option>';
          } else {
            echo '<option value="' . substr($val, 0, 1) . '">' . $name . '</option>';
          }
        }

        echo '</select></div>';
echo <<<EOD

  <div ><br /> <label for="sv_04">Another question</label> <input type="text" name="sv_04" value="$sv_04" size="10" maxlength="10" />%</div>
    <div ><label for="sv_07">When was the data collected?</label> 
EOD;
        echo '<select name="sv_07">';  
        $vars = array(
        '-Month' => 'Month',
        '-January' => 'January', 
        '-February' => 'February', 
        '-March' => 'March', 
        '-April' => 'April', 
        '-May' => 'May', 
        '-June' => 'June', 
        '-July' => 'July', 
        '-August' => 'August', 
        '-September' => 'September', 
        '-October' => 'October', 
        '-November' => 'November', 
        '-December' => 'December', 
        );

        foreach($vars as $val => $name){
          if($_SESSION['sv_07'] == $val){
            echo '<option value="' . substr($val, 0, 1) . '" selected>' . $name . '</option>';
          } else {
            echo '<option value="' . substr($val, 0, 1) . '">' . $name . '</option>';
          }
        }

        echo '</select><select name="sv_08">';

        $vars = array(
        '-Year' => 'Year',
        '-2012' => '2012', 
        '-2011' => '2011', 
        '-2010' => '2010', 
        '-2009' => '2009', 
        '-2008' => '2008', 
        '-2007' => '2007', 
        '-2006' => '2006', 
        '-2005' => '2005', 
        'Pre 2005' => 'Pre 2005', 
        );

        foreach($vars as $val => $name){
          if($_SESSION['sv_08'] == $val){
            echo '<option value="' . substr($val, 0, 1) . '" selected>' . $name . '</option>';
          } else {
            echo '<option value="' . substr($val, 0, 1) . '">' . $name . '</option>';
          }
        }

        echo '</select></div>'; 

echo<<<EOD     


<div ><input type="submit" value="Continue" /></div>
</div>

</div>
</form>
EOD;
716

Answer

Solution:

As opposed to standard HTML+PHP? This works fine with templating tools too.

public function render() {
?>
<html>
    <head>
        <title><?= $this->title ?></title>
    </head>
    <body>
        <h1>This is my awesome page</h1>

        <p>Choose from a menu.</p>
        <?php $this->renderMenu() ?>

        <p>Or enter your details:</p>
        <?php $this->renderForm() ?>
    </body>
</html>
<?php
}

If this isn't what you're getting at, show some code.

281

Answer

Solution:

From your description of the code (why aren't you showing any?) I suspect a code somewhat similar to

<?php

$someVariable = "Hello < World";
echo <<<FOO
 <!-- chunk of HTML -->
 {$someVariable}
 <!-- chunk of HTML -->
FOO;

// simulating some function calls
echo str_repeat('A', 10);

If this is the case, then let me tell you that this is not Smarty. That's simply heredoc (and quoted variables).


If you've separated the above code from things like database access, I can't see anything messy about it. Messy would be mixing output generation with business logic.

But to me (being a Smarty)-developer, this does indeed look inelegant and feels wrong. But that's probably a subjective thing - being used to the "elegance" of a template solution like Smarty, Twig or phpTAL, Mustache (and five billion other engines).

These libraries do a great deal more than simply generating HTML. They take care of escaping values (is 4 < 5? is invalid HTML). They help you structuring and organizing your templates. They do caching for you. They've seen internationalization. and. a. whole. lot. more.

People are also looking for solutions to the problem: php - What is the most efficient method to store images and data on an iPhone app from the web?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.