php variable in html no other way than: <?php echo $var; ?>

I work a lot in mixed HTML and PHP and most time I just want solid HTML with a few PHP variables in it so my code look like this:

<tr><td> <input type="hidden" name="type" value="<?php echo $var; ?>" ></td></tr>

Which is quite ugly. Isn't there something shorter, more like the following?

<tr><td> <input type="hidden" name="type" value="$$var" ></td></tr>

This is possible to but you get stuck with the "" (you have to replace them all with '') and the layout is gone

echo "<tr><td> <input type="hidden" name="type" value="$var" ></td></tr>"

Is there anything better?

292402 次浏览

There's the short tag version of your code, which is now completely acceptable to use despite antiquated recommendations otherwise:

<input type="hidden" name="type" value="<?= $var ?>" >

which (prior to PHP 5.4) requires short tags be enabled in your php configuration. It functions exactly as the code you typed; these lines are literally identical in their internal implementation:

<?= $var1, $var2 ?>
<?php echo $var1, $var2 ?>

That's about it for built-in solutions. There are plenty of 3rd party template libraries that make it easier to embed data in your output, smarty is a good place to start.

Use the HEREDOC syntax. You can mix single and double quotes, variables and even function calls with unaltered / unescaped html markup.

echo <<<MYTAG
<tr><td> <input type="hidden" name="type" value="$var1" ></td></tr>
<tr><td> <input type="hidden" name="type" value="$var2" ></td></tr>
<tr><td> <input type="hidden" name="type" value="$var3" ></td></tr>
<tr><td> <input type="hidden" name="type" value="$var4" ></td></tr>
MYTAG;

There are plenty of templating systems that offer more compact syntax for your views. Smarty is venerable and popular. This article lists 10 others.

I'd advise against using shorttags, see Are PHP short tags acceptable to use? for more information on why.

Personally I don't mind mixing HTML and PHP like so

<a href="<?php echo $link;?>">link description</a>

As long as I have a code-editor with good syntax highlighting, I think this is pretty readable. If you start echoing HTML with PHP then you lose all the advantages of syntax highlighting your HTML. Another disadvantage of echoing HTML is the stuff with the quotes, the following is a lot less readable IMHO.

echo '<a href="'.$link.'">link description</a>';

The biggest advantage for me with simple echoing and simple looping in PHP and doing the rest in HTML is that indentation is consistent, which in the end improves readability/scannability.

I really think you should adopt Smarty template engine as a standard php lib for your projects.

http://www.smarty.net/

Name: {$name|capitalize}<br>

In a php section before the HTML section, use sprinf() to create a constant string from the variables:

$mystuff = sprinf("My name is %s and my mother's name is %s","Suzy","Caroline");

Then in the HTML section you can do whatever you like, such as:

<p>$mystuff</p>

I was about to use a template engine like Smarty but my use case is very basic (rendering PHP variables in my emails templates), so I created this class... I hope this can help:

class HtmlRender
{
public function __construct($template, array $data)
{
$this->template = $template;
$this->data = $data;
}
private static function fileGetContentsUFT8($file)
{
$content = file_get_contents($file);
return mb_convert_encoding($content, "HTML-ENTITIES", "UTF-8");
}
public function render()
{
$template = self::fileGetContentsUFT8($this->template);
foreach ($this->data as $key => $value) {
$template = str_replace('\{\{ ' . $key . ' }}', $value, $template);
}
return $template;
}
}

And this is how I use:

 $renderer = new HtmlRender(
__DIR__ . '/templates/account.create.html',
[
'name' => $clientName,
'email' => $clientEmail
]
);
$body = $renderer->render();

And inside your Html use the following syntax :

<p>Your name is: <span> \{\{ name }} </span></p>
<p>Your email is: <span> \{\{ email }} </span></p>