【问题标题】:How to access value from URL in smarty template?如何从 smarty 模板中的 URL 访问值?
【发布时间】:2013-04-24 06:15:21
【问题描述】:

我是 smarty 的新手,所以在使用它时遇到了很多困难。 我有以下网址:

http://localhost/schooling_needs/sneeds1.0/web/control/modules/chapter_youtube_videos/chapter_youtube_videos.php?chapter_id=102

我想将 chapter_id 的值分配给 smarty 模板中的文本字段。为此,我编写了以下代码,但它没有得到正确的值。

<input type="text" name="chapter_id" id="chapter_id" value="{$chapter_id}">

文本字段没有获得任何值。请您告诉我应该如何将chapter_id 的值分配给上面的文本字段,请您帮助我? 提前致谢。

【问题讨论】:

    标签: php smarty


    【解决方案1】:

    您需要在代码中将其分配给您正在使用的 smarty 模板。例如:

    $smarty->assign('chapter_id', $_GET['chapter_id']);
    

    在您之前fetch/display 您的模板。 ^^

    由于 OP 的限制,使用内置 smarty 而不是单独分配给页面。:

    {$smarty.get.chapter_id}
    

    将直接从$_GET 数组中提供它。

    【讨论】:

    • 其实我想从 URL 中访问值。 URL 是动态生成的,因此我无法分配值。您能建议任何从 URL 访问 chapter_id 值的方法吗?
    • 嘿伙计,你的把戏对我有用。非常感谢您的帮助。
    • 没问题,乐于助人^^
    【解决方案2】:

    像这样使用$_REQUEST

    $chapter_id = $_REQUEST['chapter_id'];
    

    这里,$_REQUEST['chapter_id']; 从 url 获取章节 ID。

    【讨论】:

    • 太棒了,PHP 就是这样工作的。但它在 smarty 模板中没有帮助。
    【解决方案3】:

    您可以使用$smarty.get.parametername 访问模板中的$_GET 参数。所以你的代码应该是这样的:

    <input type="text" name="chapter_id" id="chapter_id" value="{$smarty.get.chapter_id}">
    

    您可以在Smarty documentation阅读更多内容

    【讨论】:

      猜你喜欢
      • 2013-09-15
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      • 2013-06-01
      相关资源
      最近更新 更多