php 内容:
<?php
require("libs/Smarty.class.php");
class my_class
{
const PWD = "gogo";
var $username = "深空";
}
$class_obj = new my_class;
echo $class_obj->username.'<br>';
echo $class_obj::PWD.'<br>';
$smarty->assign('class_obj', $class_obj);
$smarty->display('eg_4_2.tpl');
?>
tpl内容:
<html>
<body>
{$class_obj->username}<br>
{$class_obj::PWD}<br>
</html>
</body>对于$class_obj->username
在 php 和 tpl 两边使用都正常对于$class_obj::PWD
在php使用就正常
在tpl使用就报错请熟悉的指点下,谢谢!
<?php
require("libs/Smarty.class.php");
class my_class
{
const PWD = "gogo";
var $username = "深空";
}
$class_obj = new my_class;
echo $class_obj->username.'<br>';
echo $class_obj::PWD.'<br>';
$smarty->assign('class_obj', $class_obj);
$smarty->display('eg_4_2.tpl');
?>
tpl内容:
<html>
<body>
{$class_obj->username}<br>
{$class_obj::PWD}<br>
</html>
</body>对于$class_obj->username
在 php 和 tpl 两边使用都正常对于$class_obj::PWD
在php使用就正常
在tpl使用就报错请熟悉的指点下,谢谢!
例如:
$smarty->assing("pwd",$class_obj::PWD);tpl调用:
{$pwd}
谢谢你的链接
虽然你给了一大堆
虽然你没给我答案
但是关键是你没给回答我的问题我还是要再次
请问smarty 如何调用 php类的常量成员???
http://www.smarty.net/docs/en/advanced.features.static.classes.tpl
tpl调用:
{php}
echo my_class::PWD;
{/php}
PHP 5.3.0之后才可以用 $class_obj::PWD 调用。之前的版本是不能的。不知道你php版本是多少?
哥们我已经assign了
补充一点,我的php版本是5.3.8.。。
http://download.csdn.net/detail/dmtnewtons/4271275{$smarty.const}You can access PHP constant values directly. See also smarty constants.<?php
// the constant defined in php
define('MY_CONST_VAL','CHERRIES');
?>Output the constant in a template with{$smarty.const.MY_CONST_VAL}
NoteAlthough Smarty provides direct access to PHP constants for convenience, it is typically avoided as this is mixing underlying application code structure into the templates. A good practice is to assign specific needed values to template vars.
谢谢你的回复和例子
请问如何在tpl 中 调用 php class的常量成员??
拜托你的方法就是在 smarty 嵌入 php 代码不是 php 注册给 smarty 使用不同概念的
class C{
const AAA = "This is const<br>";
}
$obj = new C();
$a = C::AAA; $smarty->assign("A",$a);
$smarty->assign("str1",$str1);
$smarty->assign("str2",$str2);
$smarty->display("test.html");?>test.html{$A}<br>运行结果:
This is const
$smarty->assign("str2",$str2);
为测试数据,没用,删掉
所以也没有做出这个功能,你完全可以上他们的网站提下这个需求,
或者自己写个plugin
虽然楼上几位没理解需要,
但是也要感谢关注想不明白的是
为什么 smarty 模板实现了 调用 php 类的普通成员,
却不能调用 php 类的常量成员???难道真的smarty尚未支持这样的调用??
继续等待达人的出现
求你看看我的需求好吗???你那是在 smarty 调用 php 的变量
而不是我的需求----在smarty 调用 php 类的常量成员????
被翻译成了
<?php echo $_smarty_tpl->tpl_vars['class_obj']->value::PWD;?>
<br>虽然
$class_obj = new my_class;
echo $class_obj->username.'<br>';
echo $class_obj::PWD.'<br>';
可以得到正确的结果(但他是不严密的)但这样呢?
$class_obj->x = new my_class;
echo $class_obj->x->username.'<br>'; //这里能输出 深空
echo $class_obj->x::PWD.'<br>';//这里就要报错了:
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting ',' or ';' in
这不叫无理需求吧
smarty 能调用 php 的普通类成员,
按道理也应该可以 调用 php 的类常量成员啊??
请问这不合理吗??
但是哥们举的这个例子
肯定有办法访问类作为成员的成员
只是俺找不到那个办法
只能这样:
php:$PWD=$class_obj::PWD; $smarty->assign("PWD",$PWD);tpl:{$PWD}
如果伱说的是Smarty引用PHP常量,那个就只能用这样的格式{$smarty.const._MY_CONST_VAL}
即:
{$smarty.const.gogo}
这样能输出常量!!
如果你只是想用,那么楼上已经提出了很多变通方法可以使用.
如果你是想把这个问题搞清楚,我觉得在楼上这么多人回复的情况下,
已经基本可以认为,smarty没有提供此方法,或者我们都不知道
所以,
1.你完全可以自己研究下源码,看看是否真的没有方法,
2.或者可以和smarty社区联系一下,证实有或没有此方法,smart的网站我上面已经给出.
3.如果证实没有此方法,你可以向smarty提需求,或者自己写plugin
所有的开源软件都是根据社区的需求开发的,没有人提需求,就没有人写相应的代码,
所以,提出你的需求而不是认为"肯定有办法"
在3.1有个registerClass方法,模板中访问类属性就像在php操作一样:php 内容:
<?php
require("libs/Smarty.class.php");
class my_class {
const PWD = "gogo";
var $username = "深空";
}
$smarty = new Smarty;
$smarty->registerClass('CLA', 'my_class');
$smarty->display('eg_4_2.tpl');
?>
tpl内容:
<html>
<body>
{CLA::$username}<br>
{CLA::PWD}<br>
</html>
</body>
谢谢你的回复我的smarty版本是3.1.8
我的 class smarty 没有 registerClass 方法
然后我采用 class smartyBC 里的 register_object 方法
$smarty->register_object('class_obj', $class_obj);// 这个在 tpl 只能访问 class 的方法
$smarty->register_object('class_obj_ref', $class_obj);// 才可以 tpl 只能访问 class 的变量,但是常量成员仍然访问不到在tpl中
test print: {class_obj->my_print}<br>
test username : {$class_obj_ref->username}<br>
test PWD: {class_obj->PWD}<br> // 到这里就出错了请问这是为什么呢?
麻烦指点指点
谢谢
$smarty->assign_by_ref('class_obj_ref', $class_obj);// 才可以 tpl 只能访问 class 的变量,但是常量成员仍然访问不到
----
这里写错了
访问成员用 $smarty->assign_by_ref('class_obj_ref', $class_obj);