Web server comments

In web2py view, how do I comment server side code? In ASP.NET, I can surround any HTML or code tags with <% - and -%>, and this block will not be compiled or sent to the client. Velocity does the same with C # * 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. An imperfect workaround that leaves the code mostly unchanged is to delete two-line figures from code blocks with comments.

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) 

Single line comment issue

{{#}} successfully comments, but also does not work properly. However, this can be more difficult to fix and should be easily circumvented. The following HTML will display the two ending brackets for the final HTML, while I think it should not do anything.

HTML

 {{#{{somefunction(42)}}}} 
+4
source share
2 answers

In web2py, you add code to {{}} not <%%>. You can comment on this as you would comment on Python code. For single line code you do

 {{#.....}} 

for multi-line

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

or

 {{"""......"""}} 
+8
source

you can do as Massimo suggested, or often I just comment on the resulting HTML for temporary changes:

+1
source

Source: https://habr.com/ru/post/1310444/


All Articles