谷歌应用引擎评论和检查HTML视图中的开发环境

时间:2013-01-10 01:23:26

标签: google-app-engine

我正在使用Google App Engine中的基本*.html文件。

如何添加不会产生HTML评论的评论?例如。 in Rails我会做<%# comment %>

另外,我如何检测它是一个开发环境(或检查url是否为localhost),因此只触发某个html块?例如。在Rails我会这样做:

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

谢谢!

2 个答案:

答案 0 :(得分:3)

对于评论文章,您可以使用与上述内容类似的语法:

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

关于主机名部分,我不知道任何可以处理的内置模板。我建议在服务器端代码中进行此检查并将结果传递给模板。 Here是一个应该完成你需要的问题/答案。从该答案中提取代码,您可以在代码中定义一个变量,例如(使用Python,因为我不确定您使用的是哪个运行时):

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

然后,您可以在模板中引用它(假设您将变量作为dev_server传递给模板),如下所示:

{% if dev_server %}
    <p>comment visible only in localhost!</p>
{% endif %}

答案 1 :(得分:1)

如果您想在App Engine上使用Java创建条件代码,可以将html编写为JSP文件。然后你可以使用条件块,并且在最终输出中没有显示注释,例如。,

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

至于测试您是否在AppEngine与开发环境中,请查看来自Google的这些(Java)文档: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...
}