3

I'm working with a basic *.html file in Google App Engine.

How can I put in comments that will not churn out an html comment? E.g. in Rails I would do <%# comment %>

Also, how can I detect it is a development environment (or check if the url is localhost) and hence only trigger a certain block of html? E.g. in Rails I would do:

<% if Rails.env.development? %>
    <p>comment visible only in localhost!</p>
<% end %>

Thanks!

Community
  • 1
  • 1
Sayanee
  • 4,957
  • 4
  • 29
  • 35

2 Answers2

3

As for the comment piece, you can use similar syntax to what you have above:

{# some comment here #}
{{ variable }}
{% for some item in another_variable %}
    <p>{{ item }}</p>
{% endfor %}

Regarding the hostname part, I'm not aware of any template built-in that would handle that. I would suggest making this check in your server-side code and passing the result to the template. Here is a question/answer that should accomplish what you need. Pilfering code from that answer, you could define a variable in you code such as (using Python, as I'm not sure which runtime you are using):

dev_server = os.environ['SERVER_SOFTWARE'].startswith('Development')

You could then reference this in your template (assuming you pass the variable into the template as dev_server) as follows:

{% if dev_server %}
    <p>comment visible only in localhost!</p>
{% endif %}
Community
  • 1
  • 1
RocketDonkey
  • 36,383
  • 7
  • 80
  • 84
  • Thanks! This works perfectly! for the last line `{% end %}` should be `{% endif %}`. Please edit it as I can't edit below 6 characters. – Sayanee Jan 14 '13 at 01:41
1

If you want to have conditional code using Java on App Engine, you could write your html as a JSP file. Then you can use conditional blocks, and have comments that don't show up in the final output e.g.,

  <%-- This comment will get removed by the JSP compiler --%>
  <!-- This is a regular html comment and will survive the JSP compiler untouched -->
  <p>Just some ordinary html in a JSP file here...</p>
  <h1>Hello StackOverflow!</h1>
<% if ( isSayGoodbye ) { %>
      <h3>Goodbye!</h3>
<% } %>

As for testing if you are on AppEngine vs. dev environment, check these (Java) docs from Google: https://developers.google.com/appengine/docs/java/runtime#The_Environment

if (SystemProperty.environment.value() ==
    SystemProperty.Environment.Value.Production) {
    // The app is running on App Engine...
}
broc.seib
  • 21,643
  • 8
  • 63
  • 62