2013-01-10 30 views
3

我正在使用Google App Engine中的基本*.html文件。谷歌应用程序引擎评论和检查在HTML视图中的开发环境

我该如何放置评论,不会产生html评论?例如。 in Rails我会做<%# comment %>

此外,我怎么能检测到它是一个开发环境(或检查如果url是localhost),因此只触发一定的HTML块?例如。在Rails我会这样做:

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

谢谢!

回答

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

谢谢!这完美的作品!最后一行'{%end%}'应该是'{%endif%}'。请编辑它,因为我无法编辑低于6个字符。 – Sayanee

+0

@Sayanee Ha,错过了 - 现在编辑,很高兴它工作:) – RocketDonkey

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开发环境,检查谷歌这些(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... 
}