views:

438

answers:

5

hi i work a lot in mixed html and php and most time i just want solid html with a fet php variables in it so my code look like this

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

wich is quite crap isnt there little code something like:

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

but then working?

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

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

anything better? thanks Matthy

+5  A: 

There's the short tag version of your code

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

which 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.

meagar
Short tags are deprecated, and with php 6 they will be gone completely.
code_burgar
@code_burgar Short tags will not be removed from PHP6, nor are they currently deprecated.
meagar
@meagar Hmm, I could be wrong, but I was pretty sure they are getting removed. Even if they are staying, using them is, at best, a doubtful practice. <?xml version=”1.0″ encoding=”utf-8″?> = whoops. Not to mention they are disabled by default on some servers.
code_burgar
@code_burgar Don't write XML inside your PHP scripts, XML encode your data and write the resulting output. I've never encounter a server with short_open_tags disabled.
meagar
@meagar I've seen a number of servers that had them disabled by default.
code_burgar
+3  A: 

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;
code_burgar
My only issue with HEREDOC syntax is that it defeats my syntax highlighting for the HTML portions of my pages.
meagar
@meagar it does that in some editors, unless you have a ruleset to override that.
code_burgar
just pray you never need to index klingon ships :)about the editor, use VIM
gcb
@gcb Already using VIM.
meagar
+1  A: 

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

Jordan
+1  A: 

I'd advise against using shorttags, see http://stackoverflow.com/questions/200640/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.

Niels Bom
+1  A: 

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>
gcb
What are your arguments in favour?
Rob
arguments in favour:- separation of php code from html (better for maintenance)- a smarty template will display in a browser as is. (front end designers with no programing experience can make better sense of it)- caching. (you can hold multiple cached copies of your page according to the inputs, which can be much faster than generating the whole page again)disadvantages- smarty is a little too powerfull, and programers can put too much logic into a template... but at least it is display logic
Bingy
Exactly what he is saying. You don't write a database connection class to connect to your database right? why do you write a template engine to do templates?
gcb