1

Does anyone else experience issues with their templates when they nest code comments?

I've inherited a project with some code already commented like this

code code code
code code code
{!-- ... some code here ... --}
code code code
code code code

Then I've come along and commented more code out like this:

code code code
{!--
code code code
{!-- ... some code here ... --}
code code code
--}
code code code

Only to find that the templates completely break.

So i've ended up doing something similar to this:

code code code
{!--
code code code
<!-- ... some code here ... -->
code code code
--}
code code code

Which isn't ideal if I ever come back to this code as it has a completely different (undesired) effect.

Is there something I'm missing here?

3
  • Why not just move the opening and closing comment tags? I don't think there's a way around this due to the way the template parser works. Mar 6, 2014 at 16:22
  • 2
    Unfortunately it is not possible to nest comments
    – CreateSean
    Mar 6, 2014 at 16:51
  • @CreateSean thought so. just wanted to check
    – simonhamp
    Mar 6, 2014 at 17:11

3 Answers 3

4

Yes, ExpressionEngine comments stop at the next --} that they find. It's quite irritating.

The rule of thumb I use is to use HTML comments for notes about the frontend code, and ExpressionEngine comments about the EE code. That tends to eliminate the problem most of the time.

Sometimes I just cut + paste blocks of code into other documents while I'm debugging and put them back in later on.

1
  • Thanks for the suggestion. That's a workable solution for now. Just caught me by surprise as there's no clear documentation/prohibition from EllisLab.
    – simonhamp
    Mar 6, 2014 at 17:12
4

You can't actually nest comment blocks.

In PHP you wouldn't be able to nest a /* */ within itself, nor would you be able to nest comments in HTML <!-- <!-- --> -->, but you could try adding spaces arbitrarily in between for personal notes of where comment blocks used to be. Could be useful if it's a snippet of code you may need to reintroduce later or in a similar template, or just to say something.

E.g.

{!--
code code code
{!- - ... some code here ... - -}
code code code
--}

If you're going to go an arbitrary route you can really do whatever you want within a comment block. I use Coda 2 as my editor, I can edit multiple lines simultaneously and just prefix single line comments. In PHP it'd be //, but since everything is ultimately enclosed in a comment block you could just do whatever, though you'll have to bear in mind... they're not actually comments on their own.

2
  • 1
    You're absolutely right... not quite sure why I expected EE comment nesting to be acceptable... feeling pretty stupid now.
    – simonhamp
    Mar 7, 2014 at 11:33
  • No worries, wasn't what I was trying to highlight. An EE comment may have felt like an EE tag, so it's understandable.
    – notacouch
    Mar 8, 2014 at 2:13
1

So as has been mentioned in the comments on this post, it's not possible to nest EE's template code comments.

It results in unexpected behaviour. So if like me you suddenly and unexpectedly lose all output from a template, check that you haven't got nested comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.