html中卷曲括号和百分号是什么意思? [英] What dose the curly-brace and percent sign mean in html?

查看:431
本文介绍了html中卷曲括号和百分号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何在Django框架中开发网站。
有一些我不了解的模板html文件。



我看到教程使用了花括号(< ;%if ...%><%endif%> 等)将代码嵌入逻辑。但是当我尝试在非Django服务器(Apache)中使用这种语法时,这个语法没有被解释,我可以看到我的<%if ...%> ...在我的页面上



我不知道为什么这是。



所以这个语法不是HTML,而是一部分的Django,所以它可以解释吗?

解决方案

首先,Django模板语言的正确标签是code> {%%} ,而不是<? ?>



你是正确的,这是Django的一部分。这是一个域专用语言的示例,如维基百科所说,这是一种特定语言任务。在这种情况下,Django模板语言被构建为处理好模板。



它的工作原理是因为Django附带了一个 parser 解释语言并对其进行理解。解析器接收一个字符串,否则HTML或任何其他语言(例如{%block pasta%})不会有任何意义,并将其转换为有意义的内容。在这种情况下,它最终会输出HTML。



发生的是,您可以用任何语言编写自己的解析器来解释Django模板语言,甚至完全写一种新的语言。但是,随着你的语言变得越来越复杂,这种任务的难度也越来越大,这也取决于你选择的语言。 PHP在解析中是非常糟糕的,而Perl等其他语言的知识更好。一般来说,只要运行搜索,并使用预先构建的解析器,而不是重新创建车轮。虽然这当然是一个很少有趣的方式来解决它:)



PS这可能是您感兴趣的。


I am learning how to develop website in Django framework. There is something I don't understand about the template html file.

I saw the tutorial used the curly-braces(<% if ... %> <% endif %> etc.) to embed logic in the code. But when I try to use this syntax in a non-Django server(Apache) this syntax was not interpreted and I can see my <% if ... %> ... right on my page.

I dont know why this is.

So this syntax is not a part if HTML but rather a part of Django so it can be interpreted by it?

解决方案

First and foremost, the correct tags for the Django template language are {% %}, and not <? ?>.

And you're correct that this is a part of Django. This is an example of a domain specific language, which, as the Wikipedia says, is a language invented for a particular task. In this case, the Django templating language is built to handle, well, templates.

It works because Django comes with a parser to interpret the language and make sense of it. A parser takes a string that might otherwise has no meaning in HTML or any other language, such as {% block pasta %} and converts it into something meaningful. In this case it ultimately outputs HTML.

It happens that you could write your own parser in any language to interpret the Django templating language, or even write a new language entirely. But the difficulty of such a task increases as your language gets more complex, and also hinges on the language you choose. PHP is notoriously bad at parsing, while other languages like Perl are known to be better at it. In general it's much easier to just run a search and use a pre-built parser instead of re-inventing the wheel. Though that of course is a lot less fun way to go about it :)

p.s. This might be of interest to you.

这篇关于html中卷曲括号和百分号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆