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使用就报错请熟悉的指点下,谢谢!

解决方案 »

  1.   

    tpl 中 smarty 不认识“::”这个符号。你只能是在PHP文件中注册一个变量,在tpl文件调用。
    例如:
    $smarty->assing("pwd",$class_obj::PWD);tpl调用:
    {$pwd}
      

  2.   

    您需要遵守 smarty 的规则,否则将被 smarty 开除
      

  3.   

    http://wenku.baidu.com/view/c2aa98ef551810a6f52486af.html
      

  4.   


    谢谢你的链接
    虽然你给了一大堆
    虽然你没给我答案
    但是关键是你没给回答我的问题我还是要再次
    请问smarty 如何调用 php类的常量成员???
      

  5.   

    试下 my_class::PWD还不行看这里
    http://www.smarty.net/docs/en/advanced.features.static.classes.tpl
      

  6.   

    把类名传过去。$smarty->assign('my_class', my_class);
    tpl调用:
    {php}
         echo my_class::PWD;
    {/php}
    PHP 5.3.0之后才可以用 $class_obj::PWD 调用。之前的版本是不能的。不知道你php版本是多少?
      

  7.   


    哥们我已经assign了
    补充一点,我的php版本是5.3.8.。。
      

  8.   

    参考smarty手册:{$smarty.const}部分
    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.
      

  9.   


    谢谢你的回复和例子
    请问如何在tpl 中 调用 php class的常量成员??
      

  10.   


    拜托你的方法就是在 smarty 嵌入 php 代码不是 php 注册给 smarty 使用不同概念的
      

  11.   

    不知道你在纠结什么:<?php
    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
      

  12.   

     $smarty->assign("str1",$str1);
     $smarty->assign("str2",$str2);
    为测试数据,没用,删掉
      

  13.   

    理解你的需求, 很有可能smarty还没有收到这方面的需求,
    所以也没有做出这个功能,你完全可以上他们的网站提下这个需求,
    或者自己写个plugin
      

  14.   

    谢谢你的理解!
    虽然楼上几位没理解需要,
    但是也要感谢关注想不明白的是
    为什么 smarty 模板实现了 调用 php 类的普通成员,
    却不能调用 php 类的常量成员???难道真的smarty尚未支持这样的调用??
    继续等待达人的出现
      

  15.   

    我求你了
    求你看看我的需求好吗???你那是在 smarty 调用 php 的变量
    而不是我的需求----在smarty 调用 php 类的常量成员????
      

  16.   

    还在纠结这个问题吗?{$class_obj::PWD}<br>
    被翻译成了
    <?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 
      

  17.   


    这不叫无理需求吧
    smarty 能调用 php 的普通类成员,
    按道理也应该可以 调用 php 的类常量成员啊??
    请问这不合理吗??
      

  18.   

    谢谢大家的回复虽然我不熟php,smarty
    但是哥们举的这个例子
    肯定有办法访问类作为成员的成员
    只是俺找不到那个办法
      

  19.   

    我查过手册,只有变量可以那样传值,常量不能这样传值!!
    只能这样:
    php:$PWD=$class_obj::PWD; $smarty->assign("PWD",$PWD);tpl:{$PWD}
      

  20.   


    如果伱说的是Smarty引用PHP常量,那个就只能用这样的格式{$smarty.const._MY_CONST_VAL}
    即:
    {$smarty.const.gogo}
    这样能输出常量!!
      

  21.   

    哥们,我同意这不算"无理需求", 
    如果你只是想用,那么楼上已经提出了很多变通方法可以使用.
    如果你是想把这个问题搞清楚,我觉得在楼上这么多人回复的情况下,
    已经基本可以认为,smarty没有提供此方法,或者我们都不知道
    所以,
    1.你完全可以自己研究下源码,看看是否真的没有方法,
    2.或者可以和smarty社区联系一下,证实有或没有此方法,smart的网站我上面已经给出.
    3.如果证实没有此方法,你可以向smarty提需求,或者自己写plugin
    所有的开源软件都是根据社区的需求开发的,没有人提需求,就没有人写相应的代码,
    所以,提出你的需求而不是认为"肯定有办法"
      

  22.   

    没问题。
    在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>
      

  23.   

    你好,
    谢谢你的回复我的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> // 到这里就出错了请问这是为什么呢?
    麻烦指点指点
    谢谢
      

  24.   

    这里有点写错了,访问变量成员要用 assign_by_ref 
    $smarty->assign_by_ref('class_obj_ref', $class_obj);// 才可以 tpl 只能访问 class 的变量,但是常量成员仍然访问不到
      

  25.   

    $smarty->register_object('class_obj_ref', $class_obj);// 才可以 tpl 只能访问 class 的变量,但是常量成员仍然访问不到
    ----
    这里写错了
    访问成员用 $smarty->assign_by_ref('class_obj_ref', $class_obj);