Maison développement back-end tutoriel php F3-fatfree小型php框架课程(五)

F3-fatfree小型php框架课程(五)

Jun 13, 2016 am 11:53 AM
gt lt quot span template

F3-fatfree小型php框架教程(五)

看到这里我想再写一个helloworld的程序,不过这次的程序需要调用htm来实现,首先我们知道fatfree主要是以php为引擎的,我们定义一个template.htm文件:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span>Hello, <?php echo $name; ?>!<span class="xml_tag" style="color:rgb(30,144,255)"></span></p></span><span class="xml_tag" style="color:rgb(30,144,255)">></span></code>
Copier après la connexion
注意,这里的name是还没初始化的变量

然后在主函数里面:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="variable" style="color:rgb(58,135,173)">$f3</span>=<span class="require" style="color:rgb(255,20,147)">require</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'lib/base.php'</span>);<span class="whitespace"></span><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">route</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'GET /'</span>,<span class="whitespace">    </span><span class="function" style="color:rgb(255,20,147)">function</span>(<span class="variable" style="color:rgb(58,135,173)">$f3</span>)<span class="whitespace"> </span>{<span class="whitespace">        </span><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'name'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'world'</span>);<span class="whitespace">        </span><span class="variable" style="color:rgb(58,135,173)">$view</span>=<span class="new" style="color:rgb(255,20,147)">new</span><span class="whitespace"> </span><span class="string" style="color:black">View</span>;<span class="whitespace">        </span><span class="echo" style="color:rgb(255,20,147)">echo</span><span class="whitespace"> </span><span class="variable" style="color:rgb(58,135,173)">$view</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">render</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'template.htm'</span>);<span class="whitespace">        </span><span class="comment" style="color:rgb(184,134,11)">// Previous two lines can be shortened to:</span><span class="whitespace">        </span><span class="comment" style="color:rgb(184,134,11)">// echo View::instance()->render('template.htm');</span><span class="whitespace">    </span>}<span class="whitespace"></span>);<span class="whitespace"></span><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">run</span>();</code>
Copier après la connexion

这里面的view是内置对象,用于调用htm文件,然后这里给name初始化了然后调用template,输出helloworld,应该没什么问题。


这里也有另外一种fatfree本身的模板,格式是:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span>Hello, {{ @name }}!<span class="xml_tag" style="color:rgb(30,144,255)"></span></p></span><span class="xml_tag" style="color:rgb(30,144,255)">></span></code>
Copier après la connexion
<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="variable" style="color:rgb(58,135,173)">$f3</span>=<span class="require" style="color:rgb(255,20,147)">require</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'lib/base.php'</span>);<span class="whitespace"></span><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">route</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'GET /'</span>,<span class="whitespace">    </span><span class="function" style="color:rgb(255,20,147)">function</span>(<span class="variable" style="color:rgb(58,135,173)">$f3</span>)<span class="whitespace"> </span>{<span class="whitespace">        </span><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'name'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'world'</span>);<span class="whitespace">        </span><span class="variable" style="color:rgb(58,135,173)">$template</span>=<span class="new" style="color:rgb(255,20,147)">new</span><span class="whitespace"> </span><span class="string" style="color:black">Template</span>;<span class="whitespace">        </span><span class="echo" style="color:rgb(255,20,147)">echo</span><span class="whitespace"> </span><span class="variable" style="color:rgb(58,135,173)">$template</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">render</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'template.htm'</span>);<span class="whitespace">        </span><span class="comment" style="color:rgb(184,134,11)">// Above lines can be written as:</span><span class="whitespace">        </span><span class="comment" style="color:rgb(184,134,11)">// echo Template::instance()->render('template.htm');</span><span class="whitespace">    </span>}<span class="whitespace"></span>);<span class="whitespace"></span><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">run</span>();</code>
Copier après la connexion
变动就是new的类不同和文件中变量的引用不同。

[email protected],而且框架会自动产生跟跟文件名一样的类,在这里是template,也就是文件的名字。

再举个例子,如果你定义了:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'buddy'</span>,<span class="array" style="color:rgb(255,20,147)">array</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'Tom'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'Dick'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'Harry'</span>));</code>
Copier après la connexion

然后再在template里面写下:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span>{{ @buddy[0] }}, {{ @buddy[1] }}, and {{ @buddy[2] }}<span class="xml_tag" style="color:rgb(30,144,255)"></span></p></span><span class="xml_tag" style="color:rgb(30,144,255)">></span></code>
Copier après la connexion
就可以输出buddy的数组元素了,但是如果你只在文件里写 {{ @buddy }}的话,那么因为传输进来的是一个数组,所以输出的是‘Array’这个字符串。

文件里面的调用还支持一系列的操作:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word">{{ 2*(@page-1) }}{{ (int)765.29+1.2e3 }}<span class="xml_tag" style="color:rgb(30,144,255)"><option> <span class="xml_attr" style="color:rgb(138,43,226)">value</span>=<span class="xml_data" style="color:red">"F"</span> <span class="xml_tag" style="color:rgb(30,144,255)">{{ @active?'selected="selected"':'' }}</span><span class="xml_tag" style="color:rgb(30,144,255)">></span>Female<span class="xml_tag" style="color:rgb(30,144,255)"></span>
</option></span><span class="xml_tag" style="color:rgb(30,144,255)">></span>{{ var_dump(@xyz) }}<span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span>That is {{ preg_match('/Yes/i',@response)?'correct':'wrong' }}!<span class="xml_tag" style="color:rgb(30,144,255)"></span></p></span><span class="xml_tag" style="color:rgb(30,144,255)">></span>{{ @obj->property }}</code>
Copier après la connexion
只要你源文件里面给这些变量定义完全了,这些操作都是有效的。

还有fatfree里面给函数定义是这样的:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'func'</span>,<span class="whitespace">    </span><span class="function" style="color:rgb(255,20,147)">function</span>(<span class="variable" style="color:rgb(58,135,173)">$a</span>,<span class="variable" style="color:rgb(58,135,173)">$b</span>)<span class="whitespace"> </span>{<span class="whitespace">        </span><span class="return" style="color:rgb(255,20,147)">return</span><span class="whitespace"> </span><span class="variable" style="color:rgb(58,135,173)">$a</span>.<span class="constant_encapsed_string" style="color:rgb(178,34,34)">', '</span>.<span class="variable" style="color:rgb(58,135,173)">$b</span>;<span class="whitespace">    </span>}<span class="whitespace"></span>);</code>
Copier après la connexion
定义完之后就可以调用了:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word">{{ @func('hello','world') }}</code>
Copier après la connexion

还有一招在文件里面调用另外一个文件:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="xml_tag" style="color:rgb(30,144,255)"><include> <span class="xml_attr" style="color:rgb(138,43,226)">href</span>=<span class="xml_data" style="color:red">"header.htm"</span> <span class="xml_tag" style="color:rgb(30,144,255)">/</span><span class="xml_tag" style="color:rgb(30,144,255)">></span></include></span></code>
Copier après la connexion
如果你怕麻烦也可以用变量的形式调用,首先:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="comment" style="color:rgb(184,134,11)">// switch content to your blog sub-template</span><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'content'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'blog.htm'</span>);<span class="whitespace"></span><span class="comment" style="color:rgb(184,134,11)">// in another route, switch content to the wiki sub-template</span><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'content'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'wiki.htm'</span>);</code>
Copier après la connexion
然后:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="xml_tag" style="color:rgb(30,144,255)"><include> <span class="xml_attr" style="color:rgb(138,43,226)">href</span>=<span class="xml_data" style="color:red">"{{ @content }}"</span> <span class="xml_tag" style="color:rgb(30,144,255)">/</span><span class="xml_tag" style="color:rgb(30,144,255)">></span></include></span></code>
Copier après la connexion
而且这个include还可以设置为有条件限制的:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="xml_tag" style="color:rgb(30,144,255)"><include> <span class="xml_attr" style="color:rgb(138,43,226)">if</span>=<span class="xml_data" style="color:red">"{{ count(@items) >= 2 }}"</span> <span class="xml_attr" style="color:rgb(138,43,226)">href</span>=<span class="xml_data" style="color:red">"items.htm"</span> <span class="xml_tag" style="color:rgb(30,144,255)">/</span><span class="xml_tag" style="color:rgb(30,144,255)">></span></include></span></code>
Copier après la connexion
[email protected],这里就不详细说了。



注释:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="xml_tag" style="color:rgb(30,144,255)"><exclude><span class="xml_tag" style="color:rgb(30,144,255)">></span>    <span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span>A chunk of HTML we don't want displayed at the moment<span class="xml_tag" style="color:rgb(30,144,255)"></span></p></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"></span></exclude></span><span class="xml_tag" style="color:rgb(30,144,255)">></span></code>
Copier après la connexion
还有

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word">{* <span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span>A chunk of HTML we don't want displayed at the moment<span class="xml_tag" style="color:rgb(30,144,255)"></span></p></span><span class="xml_tag" style="color:rgb(30,144,255)">></span> *}</code>
Copier après la connexion
都是fatfree里面的注释。


条件语句:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="xml_tag" style="color:rgb(30,144,255)"><check> <span class="xml_attr" style="color:rgb(138,43,226)">if</span>=<span class="xml_data" style="color:red">"{{ @page=='Home' }}"</span><span class="xml_tag" style="color:rgb(30,144,255)">></span>    <span class="xml_tag" style="color:rgb(30,144,255)"><false><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"><span><span class="xml_tag" style="color:rgb(30,144,255)">></span>Inserted if condition is false<span class="xml_tag" style="color:rgb(30,144,255)"></span></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"></span></span></false></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"></span></check></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"><check> <span class="xml_attr" style="color:rgb(138,43,226)">if</span>=<span class="xml_data" style="color:red">"{{ @gender=='M' }}"</span><span class="xml_tag" style="color:rgb(30,144,255)">></span>    <span class="xml_tag" style="color:rgb(30,144,255)"><true><span class="xml_tag" style="color:rgb(30,144,255)">></span>        <span class="xml_tag" style="color:rgb(30,144,255)"><div>
<span class="xml_tag" style="color:rgb(30,144,255)">></span>Appears when condition is true<span class="xml_tag" style="color:rgb(30,144,255)"></span>
</div></span><span class="xml_tag" style="color:rgb(30,144,255)">></span>    <span class="xml_tag" style="color:rgb(30,144,255)"></span></true></span><span class="xml_tag" style="color:rgb(30,144,255)">></span>    <span class="xml_tag" style="color:rgb(30,144,255)"><false><span class="xml_tag" style="color:rgb(30,144,255)">></span>        <span class="xml_tag" style="color:rgb(30,144,255)"><div>
<span class="xml_tag" style="color:rgb(30,144,255)">></span>Appears when condition is false<span class="xml_tag" style="color:rgb(30,144,255)"></span>
</div></span><span class="xml_tag" style="color:rgb(30,144,255)">></span>    <span class="xml_tag" style="color:rgb(30,144,255)"></span></false></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"></span></check></span><span class="xml_tag" style="color:rgb(30,144,255)">></span></code>
Copier après la connexion
跟ifelse一样,这里就不说了,但是如果不写false那就全部默认为true:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="xml_tag" style="color:rgb(30,144,255)"><check> <span class="xml_attr" style="color:rgb(138,43,226)">if</span>=<span class="xml_data" style="color:red">"{{ @loggedin }}"</span><span class="xml_tag" style="color:rgb(30,144,255)">></span>    <span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span>HTML chunk to be included if condition is true<span class="xml_tag" style="color:rgb(30,144,255)"></span></p></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"></span></check></span><span class="xml_tag" style="color:rgb(30,144,255)">></span></code>
Copier après la connexion


然后说一个数组输出的方法:

首先定义:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'fruits'</span>,<span class="array" style="color:rgb(255,20,147)">array</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'apple'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'orange '</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">' banana'</span>));</code>
Copier après la connexion
然后文件中

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="xml_tag" style="color:rgb(30,144,255)"><repeat> <span class="xml_attr" style="color:rgb(138,43,226)">group</span>=<span class="xml_data" style="color:red">"{{ @fruits }}"</span> <span class="xml_attr" style="color:rgb(138,43,226)">value</span>=<span class="xml_data" style="color:red">"{{ @ifruit }}"</span><span class="xml_tag" style="color:rgb(30,144,255)">></span>    <span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span>{{ trim(@ifruit) }}<span class="xml_tag" style="color:rgb(30,144,255)"></span></p></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"></span></repeat></span><span class="xml_tag" style="color:rgb(30,144,255)">></span></code>
Copier après la connexion
这样就可以出来效果:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span>apple<span class="xml_tag" style="color:rgb(30,144,255)"></span></p></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span>orange<span class="xml_tag" style="color:rgb(30,144,255)"></span></p></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span>banana<span class="xml_tag" style="color:rgb(30,144,255)"></span></p></span><span class="xml_tag" style="color:rgb(30,144,255)">></span></code>
Copier après la connexion

然后我们来个复杂的例子:

定义:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'div'</span>,<span class="whitespace">    </span><span class="array" style="color:rgb(255,20,147)">array</span>(<span class="whitespace">        </span><span class="constant_encapsed_string" style="color:rgb(178,34,34)">'coffee'</span><span class="double_arrow">=></span><span class="array" style="color:rgb(255,20,147)">array</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'arabica'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'barako'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'liberica'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'kopiluwak'</span>),<span class="whitespace">        </span><span class="constant_encapsed_string" style="color:rgb(178,34,34)">'tea'</span><span class="double_arrow">=></span><span class="array" style="color:rgb(255,20,147)">array</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'darjeeling'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'pekoe'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'samovar'</span>)<span class="whitespace">    </span>)<span class="whitespace"></span>);</code>
Copier après la connexion
然后文件中:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="xml_tag" style="color:rgb(30,144,255)"><repeat> <span class="xml_attr" style="color:rgb(138,43,226)">group</span>=<span class="xml_data" style="color:red">"{{ @div }}"</span> <span class="xml_attr" style="color:rgb(138,43,226)">key</span>=<span class="xml_data" style="color:red">"{{ @ikey }}"</span> <span class="xml_attr" style="color:rgb(138,43,226)">value</span>=<span class="xml_data" style="color:red">"{{ @idiv }}"</span><span class="xml_tag" style="color:rgb(30,144,255)">></span>    <span class="xml_tag" style="color:rgb(30,144,255)"><div>
<span class="xml_tag" style="color:rgb(30,144,255)">></span>        <span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"><span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"><b><span class="xml_tag" style="color:rgb(30,144,255)">></span>{{ @ikey }}<span class="xml_tag" style="color:rgb(30,144,255)"></span></b></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"></span></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"></span></span></p></span><span class="xml_tag" style="color:rgb(30,144,255)">></span>        <span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span>        <span class="xml_tag" style="color:rgb(30,144,255)"><repeat> <span class="xml_attr" style="color:rgb(138,43,226)">group</span>=<span class="xml_data" style="color:red">"{{ @idiv }}"</span> <span class="xml_attr" style="color:rgb(138,43,226)">value</span>=<span class="xml_data" style="color:red">"{{ @ispan }}"</span><span class="xml_tag" style="color:rgb(30,144,255)">></span>            <span class="xml_tag" style="color:rgb(30,144,255)"><span><span class="xml_tag" style="color:rgb(30,144,255)">></span>{{ @ispan }}<span class="xml_tag" style="color:rgb(30,144,255)"></span></span><span class="xml_tag" style="color:rgb(30,144,255)">></span>        <span class="xml_tag" style="color:rgb(30,144,255)"></span></span></repeat></span><span class="xml_tag" style="color:rgb(30,144,255)">></span>        <span class="xml_tag" style="color:rgb(30,144,255)"></span></p></span><span class="xml_tag" style="color:rgb(30,144,255)">></span>    <span class="xml_tag" style="color:rgb(30,144,255)"></span>
</div></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"></span></repeat></span><span class="xml_tag" style="color:rgb(30,144,255)">></span></code>
Copier après la connexion
输出:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="xml_tag" style="color:rgb(30,144,255)"><div>
<span class="xml_tag" style="color:rgb(30,144,255)">></span>    <span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"><span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"><b><span class="xml_tag" style="color:rgb(30,144,255)">></span>coffee<span class="xml_tag" style="color:rgb(30,144,255)"></span></b></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"></span></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"></span></span></p></span><span class="xml_tag" style="color:rgb(30,144,255)">></span>    <span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span>        <span class="xml_tag" style="color:rgb(30,144,255)"><span><span class="xml_tag" style="color:rgb(30,144,255)">></span>arabica<span class="xml_tag" style="color:rgb(30,144,255)"></span></span><span class="xml_tag" style="color:rgb(30,144,255)">></span>        <span class="xml_tag" style="color:rgb(30,144,255)"><span><span class="xml_tag" style="color:rgb(30,144,255)">></span>barako<span class="xml_tag" style="color:rgb(30,144,255)"></span></span><span class="xml_tag" style="color:rgb(30,144,255)">></span>        <span class="xml_tag" style="color:rgb(30,144,255)"><span><span class="xml_tag" style="color:rgb(30,144,255)">></span>liberica<span class="xml_tag" style="color:rgb(30,144,255)"></span></span><span class="xml_tag" style="color:rgb(30,144,255)">></span>        <span class="xml_tag" style="color:rgb(30,144,255)"><span><span class="xml_tag" style="color:rgb(30,144,255)">></span>kopiluwak<span class="xml_tag" style="color:rgb(30,144,255)"></span></span><span class="xml_tag" style="color:rgb(30,144,255)">></span>    <span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"></span></p></span></span></span></span></span></p></span>
</div></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"><div>
<span class="xml_tag" style="color:rgb(30,144,255)">></span>    <span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"><span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"><b><span class="xml_tag" style="color:rgb(30,144,255)">></span>tea<span class="xml_tag" style="color:rgb(30,144,255)"></span></b></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"></span></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"></span></span></p></span><span class="xml_tag" style="color:rgb(30,144,255)">></span>    <span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span>        <span class="xml_tag" style="color:rgb(30,144,255)"><span><span class="xml_tag" style="color:rgb(30,144,255)">></span>darjeeling<span class="xml_tag" style="color:rgb(30,144,255)"></span></span><span class="xml_tag" style="color:rgb(30,144,255)">></span>        <span class="xml_tag" style="color:rgb(30,144,255)"><span><span class="xml_tag" style="color:rgb(30,144,255)">></span>pekoe<span class="xml_tag" style="color:rgb(30,144,255)"></span></span><span class="xml_tag" style="color:rgb(30,144,255)">></span>        <span class="xml_tag" style="color:rgb(30,144,255)"><span><span class="xml_tag" style="color:rgb(30,144,255)">></span>samovar<span class="xml_tag" style="color:rgb(30,144,255)"></span></span><span class="xml_tag" style="color:rgb(30,144,255)">></span>    <span class="xml_tag" style="color:rgb(30,144,255)"></span></span></span></span></p></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"></span>
</div></span><span class="xml_tag" style="color:rgb(30,144,255)">></span></code>
Copier après la connexion
现在发现挺好用了吧,然后要说明一下,key对应的值是数组当前指针(多维才适用),value对应的值是数组的值,会根据要求罗列出来。而且如果定义的数组是多维的,例如上面那个div -> coffee -> arabica这个二维数组,repeat也要调用两次,第一次的repeat是进入coffee的第一层,然后第二次调用进入arabica这一层,key对应的就是当时的指针。


当然也可以像之前那样判断类别:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="xml_tag" style="color:rgb(30,144,255)"><repeat> <span class="xml_attr" style="color:rgb(138,43,226)">group</span>=<span class="xml_data" style="color:red">"{{ @fruits }}"</span> <span class="xml_attr" style="color:rgb(138,43,226)">value</span>=<span class="xml_data" style="color:red">"{{ @fruit }}"</span> <span class="xml_attr" style="color:rgb(138,43,226)">counter</span>=<span class="xml_data" style="color:red">"{{ @ctr }}"</span><span class="xml_tag" style="color:rgb(30,144,255)">></span>    <span class="xml_tag" style="color:rgb(30,144,255)"><p> <span class="xml_attr" style="color:rgb(138,43,226)">class</span>=<span class="xml_data" style="color:red">"{{ @ctr%2?'odd':'even' }}"</span><span class="xml_tag" style="color:rgb(30,144,255)">></span>{{ trim(@fruit) }}<span class="xml_tag" style="color:rgb(30,144,255)"></span></p></span><span class="xml_tag" style="color:rgb(30,144,255)">></span><span class="xml_tag" style="color:rgb(30,144,255)"></span></repeat></span><span class="xml_tag" style="color:rgb(30,144,255)">></span></code>
Copier après la connexion
如果ctr是奇数就进odd类,如果是偶数就是even类


字符编码:

UTF-8:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'ENCODING'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'ISO-8859-1'</span>);</code>
Copier après la connexion

email模板:

首先下面先说一下email的逻辑定义:

welcome.txt文件里面是这样的

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word">MIME-Version: 1.0Content-type: text/html; charset={{ @ENCODING }}From: {{ @from }}To: {{ @to }}Subject: {{ @subject }}<span class="xml_tag" style="color:rgb(30,144,255)"><p><span class="xml_tag" style="color:rgb(30,144,255)">></span>Welcome, and thanks for joining {{ @site }}!<span class="xml_tag" style="color:rgb(30,144,255)"></span></p></span><span class="xml_tag" style="color:rgb(30,144,255)">></span></code>
Copier après la connexion
我们的定义:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'from'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">''</span>);<span class="whitespace"></span><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'to'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">''</span>);<span class="whitespace"></span><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'subject'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'Welcome'</span>);<span class="whitespace"></span><span class="string" style="color:black">ini_set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'sendmail_from'</span>,<span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">get</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'from'</span>));<span class="whitespace"></span><span class="string" style="color:black">mail</span>(<span class="whitespace">    </span><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">get</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'to'</span>),<span class="whitespace">    </span><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">get</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'subject'</span>),<span class="whitespace">    </span><span class="string" style="color:black">Template</span><span class="double_colon">::</span><span class="string" style="color:black">instance</span>()<span class="object_operator" style="color:black">-></span><span class="string" style="color:black">render</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'email.txt'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'text/html'</span>)<span class="whitespace"></span>);</code>
Copier après la connexion
这里有两个比较陌生的php函数,ini_php函数是用来修改php.ini基本配置文件的函数,但是会在脚本运行结束后恢复。而mail函数是php内置的核心函数,不需要额外安装,也就是发email的,这里是简单的使用模式,分别是接收者,主题,内容。但是要注意,发送成功不表示对方一定会接收到。


当然,上面这个代码只是给单一用户发送邮件的代码,但是我们经常要给一系列的用户发送邮件。所以就不能用这种简单的代码了。

我们可以用smtp的类来发送:教程

$mail=new SMTP('smtp.gmail.com',465,'SSL',[email protected]','secret');$mail->set('from','');$mail->set('to','"Slasher" ');$mail->set('subject','Welcome');$mail->send(Template::instance()->render('email.txt'));
Copier après la connexion


















Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

AI Hentai Generator

AI Hentai Generator

Générez AI Hentai gratuitement.

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
2 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Repo: Comment relancer ses coéquipiers
4 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD

Outils chauds

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Quelles sont les différences entre Huawei GT3 Pro et GT4 ? Quelles sont les différences entre Huawei GT3 Pro et GT4 ? Dec 29, 2023 pm 02:27 PM

De nombreux utilisateurs choisiront la marque Huawei lors du choix des montres intelligentes. Parmi eux, les Huawei GT3pro et GT4 sont des choix très populaires. De nombreux utilisateurs sont curieux de connaître la différence entre Huawei GT3pro et GT4. Quelles sont les différences entre Huawei GT3pro et GT4 ? 1. Apparence GT4 : 46 mm et 41 mm, le matériau est un miroir en verre + un corps en acier inoxydable + une coque arrière en fibre haute résolution. GT3pro : 46,6 mm et 42,9 mm, le matériau est du verre saphir + corps en titane/corps en céramique + coque arrière en céramique 2. GT4 sain : en utilisant le dernier algorithme Huawei Truseen5.5+, les résultats seront plus précis. GT3pro : ajout d'un électrocardiogramme ECG, d'un vaisseau sanguin et de la sécurité

Correctif : l'outil de capture ne fonctionne pas sous Windows 11 Correctif : l'outil de capture ne fonctionne pas sous Windows 11 Aug 24, 2023 am 09:48 AM

Pourquoi l'outil Snipping ne fonctionne pas sous Windows 11 Comprendre la cause première du problème peut aider à trouver la bonne solution. Voici les principales raisons pour lesquelles l'outil de capture peut ne pas fonctionner correctement : L'assistant de mise au point est activé : cela empêche l'ouverture de l'outil de capture. Application corrompue : si l'outil de capture plante au lancement, il est peut-être corrompu. Pilotes graphiques obsolètes : des pilotes incompatibles peuvent interférer avec l'outil de capture. Interférence provenant d'autres applications : d'autres applications en cours d'exécution peuvent entrer en conflit avec l'outil de capture. Le certificat a expiré : une erreur lors du processus de mise à niveau peut provoquer ce problème. Solution simple. Celles-ci conviennent à la plupart des utilisateurs et ne nécessitent aucune connaissance technique particulière. 1. Mettez à jour les applications Windows et Microsoft Store

Comment réparer l'erreur Impossible de se connecter à l'App Store sur iPhone Comment réparer l'erreur Impossible de se connecter à l'App Store sur iPhone Jul 29, 2023 am 08:22 AM

Partie 1 : étapes de dépannage initiales Vérification de l'état du système Apple : avant d'aborder des solutions complexes, commençons par les bases. Le problème ne vient peut-être pas de votre appareil ; les serveurs Apple sont peut-être en panne. Visitez la page État du système d'Apple pour voir si l'AppStore fonctionne correctement. S'il y a un problème, tout ce que vous pouvez faire est d'attendre qu'Apple le résolve. Vérifiez votre connexion Internet : assurez-vous que vous disposez d'une connexion Internet stable, car le problème "Impossible de se connecter à l'AppStore" peut parfois être attribué à une mauvaise connexion. Essayez de basculer entre le Wi-Fi et les données mobiles ou de réinitialiser les paramètres réseau (Général > Réinitialiser > Réinitialiser les paramètres réseau > Paramètres). Mettez à jour votre version iOS :

Comment résoudre l'erreur « undéfini : template.Must » dans Golang ? Comment résoudre l'erreur « undéfini : template.Must » dans Golang ? Jun 24, 2023 pm 09:00 PM

Le langage Go est un langage de programmation de plus en plus populaire avec sa syntaxe concise, ses performances efficaces et son développement facile. Le langage Go fournit un moteur de modèle puissant - "text/template", mais lors de son utilisation, certaines personnes peuvent rencontrer l'erreur "undefined:template.Must". Voici une méthode pour résoudre cette erreur. Importez le package correct Lorsque vous utilisez le moteur de modèle « texte/modèle », vous devez importer « texte/modèle ».

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Golang et le package Template : créer des interfaces utilisateur personnalisées Golang et le package Template : créer des interfaces utilisateur personnalisées Jul 18, 2023 am 10:27 AM

Package Golang et Template : créez une interface utilisateur personnalisée Dans le développement de logiciels modernes, l'interface utilisateur est souvent le moyen le plus direct pour les utilisateurs d'interagir avec le logiciel. Afin de fournir une interface utilisateur facile à utiliser et esthétique, les développeurs ont besoin d'outils flexibles pour créer et personnaliser l'interface utilisateur. Dans Golang, les développeurs peuvent utiliser le package Template pour atteindre cet objectif. Cet article présentera l'utilisation de base des packages Golang et Template et montrera comment créer une interface utilisateur personnalisée à travers des exemples de code.

Quelles sont les différences entre div et span ? Quelles sont les différences entre div et span ? Nov 02, 2023 pm 02:29 PM

Les différences sont les suivantes : 1. div est un élément de niveau bloc et span est un élément en ligne ; 2. div occupera automatiquement une ligne, tandis que span ne sera pas automatiquement renvoyé à la ligne ; 3. div est utilisé pour envelopper des structures et des mises en page plus volumineuses ; span est utilisé pour envelopper du texte ou d'autres éléments en ligne ; 4. div peut contenir d'autres éléments de niveau bloc et des éléments en ligne, et span peut contenir d'autres éléments en ligne.

Visualisation des données via le package Template de Golang Visualisation des données via le package Template de Golang Jul 17, 2023 am 09:01 AM

Visualisation des données via le package Template de Golang Avec l'avènement de l'ère du Big Data, la visualisation des données est devenue l'un des moyens importants de traitement et d'analyse de l'information. La visualisation des données peut présenter les données de manière concise et intuitive, aidant ainsi les utilisateurs à mieux comprendre et analyser les données. Dans Golang, nous pouvons utiliser le package Template pour implémenter des fonctions de visualisation de données. Cet article expliquera comment utiliser le package Template de Golang pour réaliser la visualisation des données et fournira des exemples de code. GolangTem

See all articles