php经典面试题五 -- 模板引擎

臭大佬 2018-04-02 10:04:44 2317
php 
简介 php经典面试题五 -- 模板引擎

<h1 ms="" white-space:="" background-color:=""><span>五、smarty模板引擎</span></h1><p ms="" font-size:="" white-space:="" background-color:=""><span><strong> 1. 编译和缓存区别?</strong></span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>   smarty的编译过程就是把模板拿过来,把里面的标签替换成相应php代码,这就是smarty的编译, 其实就是php和html混合的过程</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>   smarty的缓存需要手动开启,smarty的缓存就是把编译好的文件执行后,同时生成一份静态的html页面,再次访问的时候,你访问的就是是html文件了,所以就效率来说,要高一些</span></p><p ms="" font-size:="" white-space:="" background-color:=""><br></p><p ms="" font-size:="" white-space:="" background-color:=""><span><strong> 2.什么是smarty? Smarty的优点是什么?</strong></span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>     Smarty是一个使用PHP写出来的PHP<strong>模板引擎</strong>,目的是要使用PHP程序同美工<strong>分离</strong>,使的程序员改变程序的逻辑内容时不会影响到美工的页面设计,美工重新修改页面时不会影响到程序的程序逻辑,这在多人合作的项目中显的尤为重要。(也易于程序的多样式开发)</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>Smarty优点</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>1. 速度快:相对其他模板引擎。</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>2. 编译型:采用smarty编写的程序在运行时要编译成一个非模板技术的PHP文件</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>3 缓存技术:它可以将用户最终看到的HTML文件缓存成一个静态的HTML页</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>4. 插件技术:smarty可以自定义插件。</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>不适合使用smarty的地方</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>1. 需要实时更新的内容。例如像股票显示,它需要经常对数据进行更新 2.小项目。小项目因为项目简单而美工与程序员兼于一人的项目</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span> </span></p><p ms="" font-size:="" white-space:="" background-color:=""><span><strong>3.在模板中使用{$smarty}保留变量</strong></span></p><p ms="" font-size:="" white-space:="" background-color:=""><span><br></span></p><pre lay-lang="JavaScript"><code class="JavaScript"> {$smarty.get.page} //类似在php脚本中访问$_GET[page]

{smarty.cookies.}

{smarty.post.}

{smarty.session.}

{smarty.server.}</code></pre><p ms="" font-size:="" white-space:="" background-color:=""><span><strong> </strong></span></p><p ms="" font-size:="" white-space:="" background-color:=""><span><strong>4.在模板中访问php中的变量</strong></span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>在php脚本中有系统常量和自定义常量两种,同样这两种常量在Smarty模板中也可以被访问,而且不需要从php中分配,只要通过{$smarty}保留变量就可以直接输出常量的值。在模板中输出常量的示例:</span></p><pre lay-lang="JavaScript"><code class="JavaScript">{$smarty.const._MY_CONST_VAL}

{$smarty.const.FILE}</code></pre><p ms="" font-size:="" white-space:="" background-color:=""><span><strong> </strong></span></p><p ms="" font-size:="" white-space:="" background-color:=""><span><strong>5.变量调解器</strong></span></p><p ms="" font-size:="" white-space:="" background-color:=""><span> </span></p><pre lay-lang="JavaScript"><code class="JavaScript">{$var|modifier1|modifier2|……..}

<{$str}>

<{$str|capitalize}><{首字母大写}>

<{$str|upper}><{全部大写}>

<{$str|lower}><{全部小写}>

<{$str|lower|upper}><{全部大写,调节从左至右}>

<{$leg|truncate}><{字符串截取,默认80个字符}>

<{$leg|truncate:10}><{字符串截取,前10个,包括…三个字符}></code></pre><p ms="" font-size:="" white-space:="" background-color:=""><span> </span></p><p ms="" font-size:="" white-space:="" background-color:=""><span> </span></p><p ms="" font-size:="" white-space:="" background-color:=""><span><strong>6. php查询mysql数据库时,查询中文结果时出现的乱码。怎么解决????</strong></span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>  1.文件属性(另存为)</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>2.文件meta(设置浏览器解析的时候)</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>3. 连接数据库时编码设定</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>4. PHP文件中使用header函数确定编码</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span><strong> </strong></span></p><p ms="" font-size:="" white-space:="" background-color:=""><span><strong>7.缓存机制</strong></span></p><p ms="" font-size:="" white-space:="" background-color:=""><span><strong>   </strong>如果开启了缓存,smarty同时生成一份静态的html页面,如果在设定的时间没有过期,再次访问的时候,你访问的就是是html文件了,减少了读取数据库,所以就效率来说,要高一些。</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span><strong> </strong></span></p><p ms="" font-size:="" white-space:="" background-color:=""><span><strong>8. smarty的赋值和载入模板</strong></span></p><pre lay-lang="JavaScript"><code class="JavaScript"> $Smarty->assign(name,value)

$Smarty->display(‘index.html’)</code></pre><p ms="" font-size:="" white-space:="" background-color:=""><span> </span></p><p ms="" font-size:="" white-space:="" background-color:=""><span><strong>9.marty模板技术的用途是什么?</strong></span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>为了php与html分开,美工和程序员各司其职,互不干扰。</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span> </span></p><p ms="" font-size:="" white-space:="" background-color:=""><span><strong>10. smarty配置主要有哪几项?</strong></span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>1. 引入smarty.class.php;</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>2. 实例化smarty对象;</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>3. 重新修改默认的模板路径;</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>4. 重新修改默认的编译后文件的路径;</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>5. 重新修改默认的配置文件的路径;</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>6. 重新修改默认的cache的路径。</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>7. 可以设置是否开启cache。</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>8. 可以设置左侧和右侧定界符。</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span> </span></p><p ms="" font-size:="" white-space:="" background-color:=""><span><strong>11. smarty在使用过程中需要注意哪些细节?</strong></span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>Smarty是基于MVC概念的一种模板引擎,它将一个页面程序分成了两部分来实现:即视图层和控制层,</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>也就是说smarty技术将用户UI与php代码分离开。</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>这样程序员和美工各司其职,互不干扰。</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span> </span></p><p ms="" font-size:="" white-space:="" background-color:=""><span><strong>12. smarty运用过程中要注意以下几个问题:</strong></span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>1.正确配置smarty。主要要实例化smarty对象,配置smarty模板文件的路径;</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>2.php页面中使用assign赋值 和display显示页面;</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>3.smarty模板文件中不允许出现php代码段,所有的注释,变量,函数都要包含在定界符内。</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>A.{}</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>B. foreach</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>C. if else</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>D. include</span></p><p ms="" font-size:="" white-space:="" background-color:=""><span>E. Literal</span></p>