If you don't want to use a templating engine, you can make use of PHP's basic templating capabilities.
Actually, you should just write the HTML, and whenever you need to output a variable's value, open a PHP part with <?php and close it with ?>. I will assume for the examples that $data is your data object.
Please note that, all PHP control structures (like if, foreach, while, etc.) also have a syntax that can be used for templating. You can look these up in their PHP manual pages.
If you know that short tag usage will be enabled on the server, for simplicity you can use them as well (not advised in XML and XHTML). With short tags, you can simply open your PHP part with <? and close it with ?>. Also, <?=$var?> is a shorthand for echoing something.
First example with short tags:
<div id="fos"><?=$data->getWhatever()?></div>
You should be aware of where you use line breaks and spaces though. The browser will receive the same text you write (except the PHP parts). What I mean by this:
Because in the second one you have an actual \n between the img elements, which will be translated by the browser as a space character and displayed as an actual space between the images if they are inline.
发布评论
评论(3)
如果您不想使用模板引擎,则可以利用 PHP 的基本模板功能。
实际上,您应该只编写 HTML,每当需要输出变量的值时,使用
打开 PHP 部分,并使用
?>
关闭它。对于示例,我假设$data
是您的数据对象。例如:
请注意,所有 PHP 控制结构(如
if
、foreach
、while
等)也有一个语法,可以用于模板化。您可以在他们的 PHP 手册页中查找这些内容。例如:
如果您知道服务器上将启用短标记,那么为了简单起见,您也可以使用它们(不建议在 XML 和 XHTML 中使用)。使用短标签,您可以简单地使用
打开 PHP 部分,并使用
?>
关闭它。另外,是回显某些内容的简写。
第一个带有短标签的示例:
您应该注意在哪里使用换行符和空格。浏览器将收到与您编写的相同的文本(PHP 部分除外)。我的意思是:
编写此代码:
与此代码不同:
因为在第二个代码中,您在
img
元素之间有一个实际的\n
,它将是由浏览器翻译为空格字符,并显示为图像之间的实际空格(如果它们是内联的)。If you don't want to use a templating engine, you can make use of PHP's basic templating capabilities.
Actually, you should just write the HTML, and whenever you need to output a variable's value, open a PHP part with
<?php
and close it with?>
. I will assume for the examples that$data
is your data object.For example:
Please note that, all PHP control structures (like
if
,foreach
,while
, etc.) also have a syntax that can be used for templating. You can look these up in their PHP manual pages.For example:
If you know that short tag usage will be enabled on the server, for simplicity you can use them as well (not advised in XML and XHTML). With short tags, you can simply open your PHP part with
<?
and close it with?>
. Also,<?=$var?>
is a shorthand for echoing something.First example with short tags:
You should be aware of where you use line breaks and spaces though. The browser will receive the same text you write (except the PHP parts). What I mean by this:
Writing this code:
is not equivalent to this one:
Because in the second one you have an actual
\n
between theimg
elements, which will be translated by the browser as a space character and displayed as an actual space between the images if they are inline.使用单独的文件读取数据:
然后在 HTML 文件中引用结果状态:
Use a separate file to read the data:
Then reference the resulting state in the HTML file:
我不知道我真的明白你的问题。所以如果我的答案不准确,我想删除
这个类,它将创建简单的视图,
所以现在您可以创建模板并可以从任何地方调用它,就像
在您的index/index.php上您可以调用数据$this- >我的数据;
i dont know it i get realy your question. so if my answer not exaclty i will like to de deleted
this class will create simple view
soo now you can create your templates and can call it from any where just like
and on your index/index.php you can call the data $this->my_data;