首页 > 解决方案 > Smarty:如何在模板和链接中创建目录?

问题描述

我想在 Smarty 的默认模板目录中添加一个名为“locations”的新文件夹/目录。

为什么?

这样我就可以导航到我的新 页面 mywebsite.com/locations/new-post-here

但我根本无法让这些地点工作。

我已经在我的“模板”文件夹中创建了文件夹“位置”。这就是我为 Locations 创建新类时所拥有的。

<?php
    require_once("libs/Smarty.class.php");
    $smarty = new Smarty();

    $smarty->assign("title", "Locations");
    $smarty->display("templates/locations.html");
?>

我得到的错误

内部服务器错误,500。

我还尝试了什么?

<?php
    require_once("libs/Smarty.class.php");
    $smarty = new Smarty();

    $smarty->setTemplateDir( dirname( __FILE__ ) . '/../templates/locations/' );

    $smarty->assign("title", "Locations");
    $smarty->display("/locations/new-post-here.html");
?>

不工作!

标签: phptemplatessmarty

解决方案


目前尚不清楚文件在项目目录中的存储位置。无论如何,如果我们假设模板的默认文件夹是模板,那么您唯一要做的就是

$smarty->display("${location}/{$templateFile}");

您不必在显示路径前使用 /

<?php
    require_once("libs/Smarty.class.php");
    $smarty = new Smarty();

    $smarty->setTemplateDir( dirname( __FILE__ ) . '/../templates/locations/' );

    $smarty->assign("title", "Locations");
    $smarty->display("locations/new-post-here.html");
?>

但我不确定你的脚本在哪里


推荐阅读