Home Backend Development PHP Tutorial 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>
Copy after login
注意,这里的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>
Copy after login

这里面的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>
Copy after login
<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>
Copy after login
变动就是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>
Copy after login

然后再在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>
Copy after login
就可以输出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>
Copy after login
只要你源文件里面给这些变量定义完全了,这些操作都是有效的。

还有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>
Copy after login
定义完之后就可以调用了:

<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>
Copy after login

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

<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>
Copy after login
如果你怕麻烦也可以用变量的形式调用,首先:

<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>
Copy after login
然后:

<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>
Copy after login
而且这个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>
Copy after login
[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>
Copy after login
还有

<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>
Copy after login
都是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>
Copy after login
跟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>
Copy after login


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

首先定义:

<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>
Copy after login
然后文件中

<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>
Copy after login
这样就可以出来效果:

<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>
Copy after login

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

定义:

<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>
Copy after login
然后文件中:

<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>
Copy after login
输出:

<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>
Copy after login
现在发现挺好用了吧,然后要说明一下,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>
Copy after login
如果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>
Copy after login

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>
Copy after login
我们的定义:

<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>
Copy after login
这里有两个比较陌生的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'));
Copy after login


















Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

How to solve 'undefined: template.Must' error in golang? How to solve 'undefined: template.Must' error in golang? Jun 24, 2023 pm 09:00 PM

Go language is an increasingly popular programming language with its concise syntax, efficient performance, and easy development. The Go language provides a powerful template engine - "text/template", but when using it, some people may encounter the "undefined:template.Must" error. The following is a method to solve this error. Import the correct package. When using the "text/template" template engine, you need to import "text/template

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

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

Golang and the Template package: creating personalized user interfaces Golang and the Template package: creating personalized user interfaces Jul 18, 2023 am 10:27 AM

Golang and Template package: Create personalized user interface In modern software development, the user interface is often the most direct way for users to interact with the software. In order to provide a user interface that is easy to use and beautiful, developers need flexible tools to create and customize the user interface. In Golang, developers can use the Template package to achieve this goal. This article will introduce the basic usage of Golang and Template packages, and show how to create a personalized user interface through code examples.

What are the differences between div and span? What are the differences between div and span? Nov 02, 2023 pm 02:29 PM

The differences are: 1. div is a block-level element, and span is an inline element; 2. div will automatically occupy a line, while span will not automatically wrap; 3. div is used to wrap larger structures and layouts, and span is used to wrap Text or other inline elements; 4. div can contain other block-level elements and inline elements, and span can contain other inline elements.

Data visualization through Golang's Template package Data visualization through Golang's Template package Jul 17, 2023 am 09:01 AM

Data visualization through Golang's Template package. With the advent of the big data era, data visualization has become one of the important means of information processing and analysis. Data visualization can present data in a concise and intuitive way, helping people better understand and analyze data. In Golang, we can use the Template package to implement data visualization functions. This article will introduce how to use Golang's Template package to achieve data visualization and provide code examples. GolangTem

See all articles