What is a templating language?

后端 未结 4 1024
执念已碎
执念已碎 2020-12-08 18:54

I was reading somewhere that PHP is a templating language. What is exactly a templating language? What makes PHP one? What are the other templating languages?

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 19:25

    PHP isn't necessarily a templating language, but it can kind of pass that test due to the way it is interpreted.

    When the PHP file is read, only code within PHP block tags ( and ?>) is looked at, the rest is passed to the output. So a .html file could be processed by the PHP interpreter, and nothing would happen, the HTML would be output.

    If some of the areas of the HTML file had PHP tags and code, it would be interpreted and likely output some data in those locations. This could be considered a template. Usually it's the idea of having an output layer, and then having dynamic content that goes with it. You could have a product page template, and then based on some input variable, populate just a single products details/images/etc into the template.

    There are actual template engines for PHP (Smarty for one.. http://www.smarty.net/) that you can take a look at.

    I've never been a fan of them, but a lot of people find success with it.

提交回复
热议问题