web2py server-side comments - view

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)}}}}

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
{{"""......"""}}

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

Related

How to define a link in part of an i18n Thymeleaf / Spring message?

Using Spring / Thymeleaf i18n, I'd like to create a HTML paragraph message like "Click here", in which there is a link only for the word "here". What is the best way to do this?
The way I tried doesn't look nice and also results in a like break:
In messages.properties file:
error.generic.click=Click
error.generic.here=here
And in the HTML file:
<p th:text="#{error.generic.click}"></p><p><a th:text="#{error.generic.here}" th:href="#{/contact}"></a></p>
Answer
Your way seems okay to me. If you just want to fix the newline issue go ahead with the following one:
<p>
<span th:text="#{error.generic.click}"></span>
<a th:text="#{error.generic.here}" th:href="#{/contact}"></a>
</p>
The span will make "Click" stay on the same line as "here". However i'd just go for a link that say "Click here" instead of just "here".
For example in german you could say "Hier klicken". "Hier" would mean "here" and "klicken" would mean "click". The Problem is that the meaning for the words changed but the position didn't. You would end up with a link saying "klicken" instead of "Hier".
Not recommented
There is another approach, but it has some drawbacks. You could use:
<p th:utext="#{error.generic}"></p>
with the following messages.properties:
error.generic=Click here
The drawback on this one is that you can't use th:href anymore. I would not recomment this way. However this can be helpfull when using no th:* and just plain html tags. So i wanted to mention it.

Middleman & Haml with Github-style fenced code blocks

I'm starting using Middleman for static web pages & blogging purposes. I'm using it with a ZURB Fondation based template, Middleman-Foundation. It employs Haml, and I'm indeed inclined to use Haml, with Markdown files occasionally.
I'm using redcarpet for markdown, to also make use of Github-style fenced code blocks for source highlighting. But I could not figure out how to setup it for Markdown in Haml.
I've checked middleman-syntax which works for .html.md but not for .html.haml. I've tried to figure it out from Glorify but failed. I've checked this and this too.
What are the basics steps to achieve working fenced code blocks in Haml Markdown to produce highlighted source code.
It would be awesome to have a set of steps from start for this, from gem install middleman and cloning/employing Middleman-Foundation, but any short, actual answer is welcome.
EDIT
I was able to achieve pygmentized code blocks in Haml with the following (sad that it seems not possible to use markdown with fenced code blocks for this...):
%li#simple3Tab This is simple tab 3's content. It's, you know...okay.
%li#simple4Tab
-code("ruby") do
:plain
def my_cool_method(message)
puts message
end
%h3 Buttons
But there's a lasting problem, this is what I'm getting:
As can be seen the first line is not correctly being indented, this is happening because the previous code snippet is not producing a heading linebreak:
<li id='simple3Tab'>This is simple tab 3's content. It's, you know...okay.</li>
<li id='simple4Tab'>
<div class="highlight"><pre><span class="k">def</span> <span class="nf">my_cool_method</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
<span class="nb">puts</span> <span class="n">message</span>
<span class="k">end</span>
</pre></div>
</li>
</ul>
<h3>Buttons</h3>
I cannot figure out how to break the line before the first <span>, following the opening <pre>, so that the code gets correctly indented like the other lines.
Desired:
<li id='simple3Tab'>This is simple tab 3's content. It's, you know...okay.</li>
<li id='simple4Tab'>
<div class="highlight"><pre>
<span class="k">def</span> <span class="nf">my_cool_method</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
<span class="nb">puts</span> <span class="n">message</span>
<span class="k">end</span>
</pre></div>
</li>
</ul>
<h3>Buttons</h3>
I was able to figure it out through trial & error using the bits of information provided by #bhollis, Haml reference, and this SO question pointed by the Glorify author.
This is the magical combination:
%li#simple3Tab This is simple tab 3's content. It's, you know...okay.
%li#simple4Tab
=preserve do
-code("ruby") do
:plain
def my_cool_method(message)
puts "Hello" + message
end
%h3 Buttons
The result (for this one I've enabled an emacs stylesheet):
This not only solved the question about the "missing" heading newline, but also removed the extra indentation that the referred SO question talks about.
I'm still open for shorter and better approaches. Three lines of preamble to input code is a bit inconvenient.
Check out the docs for middleman-syntax: https://github.com/middleman/middleman-syntax
Code highlighting is automatically included in Markdown code blocks (via Redcarpet), but in Haml, it's better to use the "code" helper:
- code("ruby") do
My ruby code here

Passing JSON as HTML element text

Would there be bad consequences from transporting JSON in HTML like this:
<div id="json" style="display: none;">{"foo": "bar"}</div>
assuming HTML chars such as < are escaped as < in the element text?
The JSON could be strictly parsed:
var blah = $.parseJSON($('#json').html())
in a try/catch statement, for example. The rationale is to enable passing of JSON in Ajax'd HTML responses, when script tags are being stripped an not executed. An example would be Ajax requests made using the jQuery .load() special selector syntax:
$('#here').load('some.html #fragment')
...which ditches all script tags and thus prevents the use of:
<script>var blah = {"foo":"bar"}</script>
I've seen JSON being passed around in HTML attributes, and I'd guess this is equivalent - w.r.t. weirdness, security, etc - but is far less readable due to all the additional quote-escaping.
The natural way of passing JS data in HTML is through JavaScript code (if is a part of actual JavaScript code, like in the case of initial values/configuration) or by data- HTML5 attributes (whenever JS code is not necessary; always when data needs to be somehow attached to DOM elements).
In your example this would be probably the best:
<div id="json" style="display: none;"
data-something="{"foo":"bar"}">
</div>
but reorganize your data to actually follow HTML structure:
<div class="profile-container"
data-profile="{"name":"John Doe","id":123}">
... profile 123 ...
</div>
<div class="profile-container"
data-profile="{"name":"Jane Doe","id":321}">
... profile 321 ...
</div>
(quoting should be done server-side, eg. using PHP's htmlspecialchars(...), or Python's cgi.escape(..., True)).
And then you can obtain the data in one of multiple ways, eg. using jQuery's .data() method.
EDIT:
Yes, your approach with embedding JSON as content of HTML tags and hiding it using CSS styles has gotchas. As I said, if you want to pass data in HTML, the only "best practice" way is to attach it to one of HTML elements (you are kind-of doing it anyway, but you use CSS to hide it, while you can use existing solutions for passing JSON/data without affecting clients that could override your styles). The proof for one of disadvantages is here: http://jsfiddle.net/NY7Bs/ (data is passed both ways, but one simple external style overrides your inline styles and shows the content - not mentioning the influence on semantics of your document).
Why not simply use the .ajax() function then, you would get only the string with the json. Then you could parse it as you suggested.

HTML formatting in TextMate (automatic 'outdenting')

I'm using TextMate and indentation isn't working as I'd expect - more specifically, it doesn't 'outdent' closing tags for me.
I create a new HTML file, I move the cursor down to the body section ... and I type
<ul>(newline)
It appropriately indents the next line for me so I build the following
<ul>
<li>hello
</li>
</ul>
Clearly, the closing tags "could" be outdented to line up with their start tags. I'm surprised TextMate doesn't do this automatically. Am I missing something? Sometimes I like to type my opening and closing tags and then fill-in the blanks ... but I keep getting this:
<div>
</div>
It indents correctly - but it doesn't understand that this is a closing tag -- which means I've got to move the cursor behind the closing tag and outdent it myself.

Possible to have Visual Studio TODO comments in aspx/ascx files appear in task list?

We develop asp.net webforms using visual studio 2008. For multilingual support, we translate all our text. However, when designing, we usually just enter the english text and come back to translation later (it interrupts flow of work otherwise).
I've added a "ToTranslate" tag in the options. Adding //ToTranslate: something in C# code correctly adds the entry to the Task List. I haven't however figured out how to do the same for aspx and ascx files (where most of our user text lives).
Inserting <%-- //ToTranslate: something --%> or <%-- ToTranslate: something --%> doesn't work.
Any ideas?
It seems to me that it works fine if you put the delimiters <% and %> on a line by themselves. What I did was this: go to Tools menu and click on Options, then under Environment -> Task List add a new ToTranslate token. Click OK to accept the change. Back on the ASPX page I added the comments on a line by themselves and the code delimiters on lines by themselves.
FYI if you want to do this in a .Net MVC3 razor cshtml file the syntax this:
#{
//TODO: Move this inline <style> css to a file
}
Take note: that you need to put the trailing } bracket on a new line as otherwise it will be included in the // comment. You could do it using /**/ like below and keep it all on one line but it's more typing, and a bit harder to read. My take is, if it annoys you the comment takes up 3 lines in your file, all the more motivation to fix the issue and remove it completely :)
#{/*TODO: Move this inline <style> css to a file*/}
You do not need the <% %> on lines by themselves. This example shows what works and what does not:
<%//ToTranslate will work%>
<%/*ToTranslate will work*/%>
<!--ToTranslate won't work-->
<!--
ToTranslate won't work
-->
It may be due to the fact that what's differentiating between an HTML comment and some form of aspx comment is getting messed up by the -- because that's part of an html comment?

Resources