想了解PHPsmarty的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于phpsmarty框架的相关问题,此外,我们还将为您介绍关于PHP+Smarty+MySQL、PHP/Smarty–如
想了解PHP smarty的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于php smarty框架的相关问题,此外,我们还将为您介绍关于PHP + Smarty + MySQL、PHP / Smarty – 如何检查数组中是否有具有特定值的对象,并创建这些对象的临时数组、php autoload:解决 PHP 中的 __autoload 和 Smarty 冲突、PHP CodeIngiter 2.x.x 框架与 Smarty 3.x.x 模板引擎整合的新知识。
本文目录一览:- PHP smarty(php smarty框架)
- PHP + Smarty + MySQL
- PHP / Smarty – 如何检查数组中是否有具有特定值的对象,并创建这些对象的临时数组
- php autoload:解决 PHP 中的 __autoload 和 Smarty 冲突
- PHP CodeIngiter 2.x.x 框架与 Smarty 3.x.x 模板引擎整合
PHP smarty(php smarty框架)
*//* 可以预先规定一系列的变量与值的对应,并放在配置文件中,在使用时载入。 配置文件默认放在configs文件夹中,可以自定义修改文件夹名。 *//* #模板test.conf文件: #键对应的值可以不用引号括起来 title = Welcome to Smarty!! cutoff_size = 40 [china] language = chinese [england] language = english #[china],[england]为标签,未设置标签的键值为全局的只要调用该配置文件就可以在模版中使用,设置了标签的键值只有在调用配置文件时指定了对应标签才可以使用 #在PHP源文件调用配置文件语句$smarty->configLoad(''test.conf'', $sections = ''england'');该语句下面调用的模版才可以使用该配置文件,通过$sections属性指定使用哪个标签下的键和值 #$sections参数可以不写,默认值为null,$smarty->configLoad(''test.conf'')则只使用全局的键值,而不能使用标签下的键值 #在模版下通过{config_load file="test.conf" section="china" scope="global"}语句调用配置文件 #section属性可以不写,默认是null,scope属性必须写{config_load file="test.conf" scope="global"} #section属性可赋三种值 #local 只有当前模版可以使用该配置文件 #parent 只有当前模版引入该配置文件语句后包含的模版中,或在php源文件中smarty对象调用该配置文件后调用的模版中可以使用该配置文件中的键值 #global 测试效果和parent相同 #在模版中通过{#language#}来使用键值,也可以通过{$smarty.config.language}来访问配置文件键值 #PHP源文件中可以使用$smarty->getConfigVars(''language'')或$smarty->getConfigVariable(''language'')来获取键值,$smarty->getConfigVars(''language'')获取的还可能是数组 *//* tpl文件中常用函数 tpl文件: {capture name="testCapture"} {include file="f1.tpl"} {/capture} {if true} {$smarty.capture.testCapture} {/if} {if $name == "wang"} Welcome wang. {elseif $name == "zhang"} Welcome zhang. {else} Welcome, whatever you are. {/if} {*操作符可以是 ==,>= 等也可以是 eq,ne等*} {for $x=0; $x
{$smarty.foreach.testForeach.index} {$smarty.foreach.testForeach.iteration} {$smarty.foreach.testForeach.first} {$smarty.foreach.testForeach.last} {$smarty.foreach.testForeach.total}
{foreachelse} $testArr is null {/foreach} {*也可以如下两种类PHP格式*} {foreach $testArr as $n} {$n} {/foreach} {foreach $testArr as $key=>$n} {$key} {/foreach} {$sectionArr = [0=>"a",4=>"b","c","d","e",6,7,8,9,10,11,12,13,14,15,16]} {section name="testSection" loop=$sectionArr start=0 step=4 max=6 show=true} {$smarty.section.testSection.index}- {$sectionArr[testSection]}- {$smarty.section.testSection.iteration}-
{sectionelse} $sectionArr is null {/section}
{/block} {block name=''middle''} f1.middle
{/block} {block name=''buttom''} f1.buttom
{/block} f2.tpl: {extends file="f1.tpl"} {block name=''top''} f2.header
{/block} {block name=''other''} it can`t be show
{/block} {* 如果f2.tpl中没有block标签,或f2.tpl中没有和f1.tpl中相同命名的block标签,则f2.tpl完整引入显示f1.tpl中所有内容包括block标签的内容,而f2.tpl中所有内容将忽略 如果f2.tpl中有和f1.tpl中同名block标签,则在f2.tpl显示时f2.tpl中block标签内容将覆盖f1.tpl中同名block标签的内容,在f2.tpl页面显示时,内容仍将按f1.tpl设置的格式位置显示,f2.tpl其他所有文本包括未同名的block标签及其内容都将被忽略,不显示。 block标签的内容只会覆盖父模版中同名block标签的内容,或在子模版中显示,在本页面中如果没有调用父模版或父模版中没有要覆盖的同名block标签,block标签内容在本页面中不显示 这种继承支持多文件,多重继承,意味着可以无限的继承下去 *} {fetch file="http://www.126.com" assign="testAssign"} {$testAssign} {fetch file="http://www.126.com"} {*fetch可以引用外部http,ftp的页面,如指定assign的值则把引用的内容存在指定名的变量中,否则在哪有fetch在哪显示*} *///php页面: //调用模版也可以使用此方法,在输出前做一些处理 //$output = $smarty->fetch("index.tpl"); //do something with $output here对将要输出的内容进行处理 //echo $output;//然后将模板输出/* 模板中提交表单 *///连接数据库mysql_connect("localhost","root","root"); mysql_select_db("test"); $smarty->assign(''webDir'',$_SERVER[''DOCUMENT_ROOT'']);//$_SERVER[''DOCUMENT_ROOT'']为当前项目文件夹的绝对路径 //配置JQuery的src路径最好写绝对路径或写要运行文件能找到该JQuery的相对路径因为要编译成编译文件,而编译后的文件和原路径环境不一样 ?>
以上就介绍了PHP smarty,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
PHP + Smarty + MySQL
Help me please! How to transfer data from table to smarty?
Function:
public function getBanLog() {
global $mysqli;
$result = $query = $mysqli->query("SELECT * FROM `bans`") or die($mysqli->error);
$rows = array();
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
$rows[] = $row;
}
}
index.php:
$user = new UserInfo();
$smarty = new Smarty();
$smarty->assign("userInfo", $user);
$smarty->assign(''ban'', $user->getBanLog());
$smarty->display(''template/ban.tpl'');
ban.tpl:
{foreach from=$ban item=row}
<td>{$row.id}</td>
<td>{$row.banned}</td>
<td>{$row.admin}</td>
<td>{$row.reason}</td>
{/foreach}
-
1code looks fine whats the problem ? – M Khalid Junaid Jul 27 ''13 at 12:10
-
Only thing suspect here is the lack of a
WHERE
clause in your query, which appears to return all bans for all users, not just the user as implied by theUserInfo()
. – Michael Berkowski Jul 27 ''13 at 12:11 -
@dianuj
getBanLog()
returns nothing, and$result = $query = $mysqli->query
– bansi Jul 27 ''13 at 12:14
1 Answer
Your getBanLog()
function returns nothing, need to add a return statement. Also $result = $query = $mysqli->..
is not correct.
Try this
public function getBanLog() {
global $mysqli;
$result = $mysqli->query("SELECT * FROM `bans`") or die($mysqli->error);
$rows = array();
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
$rows[] = $row;
}
return $rows;
}
PHP / Smarty – 如何检查数组中是否有具有特定值的对象,并创建这些对象的临时数组
这里的第一个计时器,我希望我能够解释得这么好……
PHP / Smarty,我正在处理页面的一部分,该页面显示与常规页面或该页面上的各个位置相关联的注释的项目符号列表.有些地方没有笔记.就像是:
一般
>注1
纽约
>注意2
>注3
波士顿
>注4
我有两个分配给我正在使用的.tpl的数组,并由UI类填充:$places和$notes
$places包含place对象,每个对象由place_id唯一
$notes包含唯一标识的注释对象,每个注释对象可能有也可能没有place_id作为其中一个属性
这就是我的想法:
{if (**ANY NOTES EXIST IN $notes WITH NO place_id**)}
<ul id="list-general">
<h4>General</h4>
{foreach from=**[NOTES WITH NO place_id]** item=note}
<li id="note-{$note->get_id()}">$note->get_text()</li>
{/foreach}
</ul>
{else}
<ul id="list-general">
<h4>General</h4>
</ul>
{/if}
{foreach from=$places item=place}
{assign var=curr_place_id value=$place->get_id()}
{if (**ANY NOTES EXIST IN $notes WHERE place_id == $curr_place_id**)}
<ul id="list-{$curr_place_id}">
<h4>{$place->get_name()}</h4>
{foreach from=**[NOTES WHERE place_id == $curr_place_id]** item=note}
<li id="note-{$note->get_id()}">$note->get_text()</li>
{/foreach}
</ul>
{else}
<ul id="list-{$curr_place_id}">
<h4>{$place->get_name()}</h4>
</ul>
{/if}
{/foreach}
我对这一切都很陌生,而且我很难掌握CAPS中的项目……所以,我的问题是:什么是评估两个不同数组中对象属性的最佳方法,以及可能构建只包含我需要的笔记的临时数组.
(如果你想知道,我正在创建隐藏的节点,所以我可以通过javascript访问它们,而不必担心订单)
解决方法:
虽然我猜测在Smarty中可以做到这一点,但似乎你在这里混合了你的业务逻辑和表示逻辑.就个人而言,我会考虑在调用此模板的PHP中将数组拼接成两部分(一般和地点).
$placesNotes = array();
$generalNotes = array();
array_filter($notes, function($note) {
global $placesNotes, $generalNotes;
if($note->get_place_id() == null) {
$generalNotes[] = $note;
} else {
$placesNotes[$note->get_place_id()][] = $note;
}
});
(注意:这使用匿名函数,因此只有> 5.3 – 如果您希望它在旧版本上运行,请将函数移动到它自己的命名函数并使用Callback类型)
然后你最终得到两个你可以轻松循环的数组,在Places循环中使用isset($notes_places.$curr_place_id)来查明这个地方是否有任何注释.
另外,你忘记了get_text()调用周围的{}.
我发了一个快速的样本,发现于:http://gist.github.com/479392 – 希望有帮助吗?
顺便说一句:你的问题非常明确,并解释了你想做什么,甚至详细说明了你做的事情背后的决定,当时不是很明显 – 所以这是第一次:优秀的问题!
php autoload:解决 PHP 中的 __autoload 和 Smarty 冲突
最近,在项目中发现,PHP 的 __autoload 方法失效了。调试了好久,百思不得其解,查了下资料才知道原来是 Smarty 的原因。新版的 Smarty 改变了autoload的方式。
解决方法是:在 Smarty 的包含类文件后加一段代码,spl_autoload_register("__autoload");
如下:
define(''ROOT_PATH'', dirname(__FILE__));
require_once ROOT_PATH . ''/includes/smarty/Smarty.class.php'';
spl_autoload_register("__autoload"); // 添加这段代码
?>
本文链接http://www.cxybl.com/html/wlbc/Php/20121013/32997.html
PHP CodeIngiter 2.x.x 框架与 Smarty 3.x.x 模板引擎整合
目前 CI 框架已经推出了 2.0 的版本了。一直想尝试修改我的网站的核心。经过测试可以正常使用,以下提供配置方案和已经配置好的文件,
下面是配置步骤的详细说明一下:
第一步:安装 CodeIngiter。 这个不需要详细说,下载地址为:http://codeigniter.com/downloads/
第二步:下载最新版本的 Smarty 库,下载地址:http://www.smarty.net/download
第三步:在安装好的 ci 项目的根目录下的入口文件 index.php 文件开头添加如下代码
define(''APP_NAME'',''application'');
if(!defined(''ROOT''))define(''ROOT'',dirname(__FILE__));
if (!defined(''DS'')) define(''DS'', DIRECTORY_SEPARATOR);
第四步:在安装好的 ci 项目的根目录下 application\config 下添加 smarty.php 配置文件
<php
if ( ! defined(''BASEPATH'')) exit(''No direct script access allowed'');
/* smarty 设置 */
$config[''caching''] = false;
$config[''cache_lifetime''] = 3600;
$config[''template_dir''] = ROOT.DS.APP_NAME.DS.''views''.DS;
$config[''compile_dir''] = ROOT.DS.APP_NAME.DS.''cache''.DS.''views_c''.DS;
$config[''cache_dir ''] = ROOT.DS.APP_NAME.DS.''cache''.DS.''smarty_cache''.DS;
$config[''left_delimiter''] = ''<!--{'';
$config[''right_delimiter ''] = ''}---->'';
第五步:在安装好的 ci 项目的根目录下 \application\libraries, 把下载好的 smarty 文件包中 libs 放到这里,并改名为 smarty
第六步:在安装好的 ci 项目的根目录下 \application\libraries 下添加 smarty 扩展类 CI_Smarty.php
<?php
require (ROOT.DS.APPPATH."libraries".DS."smarty".DS."Smarty.class.php");
class CI_Smarty extends Smarty
{
protected $ci;
public function __construct()
{
parent::__construct();
$this->ci = & get_instance();
$this->ci->load->config(''smarty'');
self::loadSetting();
}
public function loadSetting()
{
$this->caching = $this->ci->config->item(''caching'');
$this->cache_lifetime = $this->ci->config->item(''cache_lifetime'');
$this->template_dir = $this->ci->config->item(''template_dir'');
$this->compile_dir = $this->ci->config->item(''compile_dir'');
$this->cache_dir = $this->ci->config->item(''cache_dir'');
$this->left_delimiter = $this->ci->config->item(''left_delimiter'');
$this->right_delimiter = $this->ci->config->item(''right_delimiter'');
}
}
第七步:在 application\config\autoload.php, 设置自动加载 smarty 扩展类 CI_Smarty.php
$autoload[''libraries''] = array(''ci_smarty'');
第八步:在 application\core 添加 MY_Controller.php 文件
<?php if ( ! defined(''BASEPATH'')) exit(''No direct script access allowed'');
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function assign($key,$val)
{
$this->ci_smarty->assign($key,$val);
}
public function display($html)
{
$this->ci_smarty->display($html);
}
}
第九步:已经配置完成 开始测试 在 application\controllers\welcome.php
<?php if ( ! defined(''BASEPATH'')) exit(''No direct script access allowed'');
class Welcome extends MY_Controller {
public function index()
{
$this->assign("hello","<a href=''http://www.pvsky.com/blog''>我的博客</a>");
$this->display("test.html");
}
}
模板文件 \application\views\test.html
<html>
<head></head>
<body>
<!--{$hello}-->
</body>
</html>
访问你的本地 index.php 就可以看到
关于PHP smarty和php smarty框架的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于PHP + Smarty + MySQL、PHP / Smarty – 如何检查数组中是否有具有特定值的对象,并创建这些对象的临时数组、php autoload:解决 PHP 中的 __autoload 和 Smarty 冲突、PHP CodeIngiter 2.x.x 框架与 Smarty 3.x.x 模板引擎整合等相关内容,可以在本站寻找。
本文标签: