【问题标题】:Display php Variable on Smarty Template 3在 Smarty 模板 3 上显示 php 变量
【发布时间】:2014-04-14 15:40:50
【问题描述】:

我一直在尝试在 smarty 模板上显示一个 php 变量,但这是不可能的。

在我的 tpl 上我有:

<a href="#">Welcome <strong><!--{$username}--></strong></a>

在我的 php 上我有:

<?php
require('../common/Smarty/config.php');             //Smarty Config Class
require_once('../common/config/config.php');        //Project Config Class  
require_once('../common/classes/Login.php');        //Login Class 

$smarty = new Smarty();

$template = "login.tpl";

$smarty->assign ( "username", $user_name );         //first: smarty variable, last: php variable.
$smarty->display ( "{$template}" );
?>

我想做的就是在登录页面上显示用户的用户名。

有什么帮助吗?

提前致谢。

【问题讨论】:

  • 顺便说一句,我使用 作为 Smarty 分隔符
  • 在$smarty-&gt;display ( "{$template}" );,smarty 需要模板文件名或另一个有效的模板资源。我认为滥用 html cmets 作为聪明的分隔符并不是最佳选择。想象一下你想在你的代码中实际使用 html cmets,那么你可能会得到一个 smarty 语法错误
  • 模板显示正常,图像和 css 一切正常,问题是试图显示 $user_name 变量。事实上,如果我删除:$user_name,并添加类似“Peter”的内容,模板会显示该名称。
  • 您有任何错误吗?你得到什么而不是翻译后的价值?
  • 我什么也没得到,但如果我添加:$smarty->assign ("username", Peter);例如,我得到:欢迎彼得!看起来没有从数据库中获取用户名(但它正在登录)。 $user_name 变量在 Login.php 类中

标签: php templates smarty smarty3


【解决方案1】:

在您的 Smarty lib tpl 中,您只需在您希望用户名出现在 login.tpl 文件中的位置写入 {$username}。

改变这个

<a href="#">Welcome <strong><!--{$username}-->``</strong></a>

到这里

<a href="#">Welcome <strong>{$username}</strong></a>

以上对我有用

【讨论】:

  • 不工作,我得到:欢迎 {$username}。 (我没有使用 {} 作为分隔符,我使用
猜你喜欢
  • 2012-05-03
  • 1970-01-01
  • 1970-01-01
  • 2014-11-22
  • 1970-01-01
  • 1970-01-01
  • 2011-05-17
  • 1970-01-01
  • 2020-08-21
相关资源
最近更新 更多