Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

web2py server-side comments

In a web2py view, how do I comment out server-side code? In ASP.NET, I can surround any HTML or code tags with <%-- and --%> and that block will not be compiled or sent to the client. Velocity does the same thing with #* and *#. Is there an equivalent in web2py?

ASP.NET

<div>
    <p><%=foo.bar%></p>
    <%-- don't print twice! <p><%=foo.bar%></p> --%>
</div>

web2py

<div>
    <p>{{=foo.bar}}</p>
    ??? don't print twice! <p>{{=foo.bar}}</p> ???
</div>

EDIT: Fixed web2py code tags.


Problem with block comments

An exception is thrown if {{'''...'''}} and {{"""..."""}} are used with code blocks inside. A non-ideal workaround that leaves the code mostly unchanged is removing the double-braces from the commented-out code blocks.

HTML

{{'''{{somefunction(42)}}'''}}

Error

Traceback (most recent call last):
  File "gluon/restricted.py", line 176, in restricted
  File "gluon/restricted.py", line 163, in compile2
  File "C:\development\web2py\applications\SpaceCorps/views\default/index.html", line 74
    '''{{somefunction(42)\nresponse.write("'''}}\r\n\t\t\r\n\t</div>\r\n</div>\n\t</body>\n</html>\n",escape=False)
                                          ^
SyntaxError: invalid syntax

Generated View code

'''{{somefunction(42)\nresponse.write("'''}}\r\n\t\t\r\n\t</div>\r\n</div>\n\t</body>\n</html>\n",escape=False)

Problem with single-line comment

{{#}} successfully comments, but also doesn't quite work as expected. This may be more difficult to fix, however, and should be easy to work around. The following HTML will render two end brackets to the final HTML, while I think it should render nothing.

HTML

{{#{{somefunction(42)}}}}
like image 286
MikeWyatt Avatar asked Dec 29 '22 16:12

MikeWyatt


1 Answers

In web2py you enclose code in {{ }} not <% %>. You can comment is as you would comment Python code. For single line code you do

{{#.....}}

for multiline

{{'''......'''}}

or

{{"""......"""}}
like image 121
mdipierro Avatar answered Feb 28 '23 02:02

mdipierro