【问题标题】:Smarty Resource directorySmarty 资源目录
【发布时间】:2014-10-06 17:42:02
【问题描述】:

我正在为我最近的个人项目使用 smarty,但遇到了问题。

$smarty = new Smarty;
$smarty->template_dir = 'templates/dashboard/';

上面可以从这个目录加载 .tpl 文件,这对我很有用,但是在 .tpl 里面我想加载一个图像,我必须这样做:

<img src="templates/dashboard/images/avatar/full/{$user->username}.jpg" class="profile-avatar img-thumbnail">

我想做但不会工作的是:

<img src="images/avatar/full/{$user->username}.jpg" class="profile-avatar img-thumbnail">

当我设置 template_dir 时,我觉得这也会改变,但它不会...... 我找不到任何可以更改 .tpl 文件的文档。

注意“模板/仪表板”,这就是我想在 .tpl 中删除的内容

【问题讨论】:

    标签: php templates smarty


    【解决方案1】:

    首先,您不应该将图像放在您拥有模板的同一文件夹中。图片应该放在公共文件夹中(通常是publicpublic_html),站点代码可以放在其他无法从网络访问的目录中。

    所以把你的文件移到另一个文件夹,因为你现在的结构总有一天会给你带来麻烦。

    但如果你仍然想使用这样的结构,你可以简单地将新变量分配给 Smarty 并使用你想要使用的路径:

    $smarty->assign('ip', $smarty->template_dir);
    

    现在您可以在模板中使用:

    <img src="{$ip}images/avatar/full/{$user->username}.jpg" class="profile-avatar img-thumbnail">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      • 1970-01-01
      • 2016-07-08
      相关资源
      最近更新 更多