views:

366

answers:

5

Is there a simple solution to do the equivalent of Java's comments:

<%-- this is a comment inside a template, it does not appear in the output HTML --%>

Even if you use short php tags, you still have to wrap the comments with comment syntax, on top of the php tags:

<? /* this is a comment of the html template */ ?>

I'm considering doing some kind of filter on the output templates, to remove all html comments, or better yet, custom comments like the Java syntax above, but how would you do that in the most efficient way? You'd have to run a regexp right?

The reason for my question is simply that in a MVC framrwork, using components, and re-usable html templates (think YUI), I need to document clearly those templates, in a readable way..

A: 

Shouldn't these work?

<? # This is a comment ?>
<? // This is a comment too ?>

That's not too verbose is it?

Triptych
A: 

Triptych is correct for inside of php tags. outside, <!-- Comment here --> still works.

Jekke
And it still sends your comments out to the browser... Which is never a good thing.
Chris Lively
Indeed. My question concerns the kind of comments that documents php templates for reusable components and the like, developer comments that should not appear in the final output. Besides you don"t want to strip html comments because IE uses proprietary conditional comments that can be of use.
faB
+1  A: 

Not that I know of, but the short-tag plus the block comments are very easy - about as easy to type as the JSP comments you mentioned above:

<?/* This is a comment */?>

or even

<?// this is a comment ?>

With a more elaborate PHP templating systems, such as Smarty, there are other syntaxes.

Guss
It doesn't look nice "syntactically" but it works. However I think there can bea golbal setting that disables the php short tags? That's why I'm looking for another solution. I would prefer to stricly use one style of php tag over the entire application.
faB
I always use the long tag - <?php . Its also XML compliant which is cool and lets you validate your templates for well-formdness and even validity using xmllint. Still, I never found use for comments as you require - I simply comments my code in PHP, and comment my HTML with visible HTML comments.
Guss
A: 

Hi fAB,

if you really want/have to use plain php for templating

    <?# this seems the best way to do comments ?>
    <?/* but
         block comments
         look ugly IMHO */?>

But I think there's a big conceptional difference between PHP and JSP - not in its real world usage scenarios, but what they were created/designed for.

While JSP was clearly built to be the "view" or "presentation" part of a java webapplication with as little controlling logic as possible, PHP started out as something like Perl but far more focused on web development.

Thus your usecase (re-usable MVC-framework) seems to call for one of the separate template-languages for PHP like Smarty or PHPTAL which are better suited to separate presentation from logic than plain PHP. This way, you're gonna use PHP for the model and controller-parts and the template-language like you'd use JSP (and yes, most templating engines support commenting the templates/template blocks far better than PHP).

Kind regards

Argelbargel

Argelbargel
+1  A: 
Erick