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.
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)
{{#}} 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)}}}}
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
{{"""......"""}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With