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

WBOY
リリース: 2016-06-13 11:53:58
オリジナル
1252 人が閲覧しました

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>
ログイン後にコピー
注意,这里的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>
ログイン後にコピー

这里面的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>
ログイン後にコピー
<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>
ログイン後にコピー
变动就是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>
ログイン後にコピー

然后再在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>
ログイン後にコピー
就可以输出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>
ログイン後にコピー
只要你源文件里面给这些变量定义完全了,这些操作都是有效的。

还有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>
ログイン後にコピー
定义完之后就可以调用了:

<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>
ログイン後にコピー

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

<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>
ログイン後にコピー
如果你怕麻烦也可以用变量的形式调用,首先:

<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>
ログイン後にコピー
然后:

<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>
ログイン後にコピー
而且这个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>
ログイン後にコピー
[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>
ログイン後にコピー
还有

<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>
ログイン後にコピー
都是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>
ログイン後にコピー
跟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>
ログイン後にコピー


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

首先定义:

<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>
ログイン後にコピー
然后文件中

<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>
ログイン後にコピー
这样就可以出来效果:

<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>
ログイン後にコピー

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

定义:

<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>
ログイン後にコピー
然后文件中:

<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>
ログイン後にコピー
输出:

<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>
ログイン後にコピー
现在发现挺好用了吧,然后要说明一下,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>
ログイン後にコピー
如果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>
ログイン後にコピー

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>
ログイン後にコピー
我们的定义:

<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>
ログイン後にコピー
这里有两个比较陌生的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'));
ログイン後にコピー


















関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート