Twig 模板引擎:获取当前 url [英] Twig templates engine: get current url

查看:35
本文介绍了Twig 模板引擎:获取当前 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 Twig 模板中获取当前 URL?

我在 PHP 中使用 Twig,没有任何其他框架.

解决方案

查找当前 URL

当前 URL 由您的 Web 服务器提供并写入 $_SERVER 超级全局.运行这个小脚本,

';print_r($_SERVER);,通过你的服务器和 root 找到你正在寻找的值.

关于这个主题的相关问题:

PHP 手册描述了可用的$_SERVER 此处的值.

在 TWIG 中获取 URL

获得 URL 后,您需要在 Twig 模板实例上调用 render(...) 时将其作为模板变量传递.例如,您可以对此进行编码.

$current_url =//找出当前 url 是什么//将当前 URL 作为变量传递给模板echo $template->render(array('current_url' => $current_url));

要在模板中使用变量,请使用 {{ variable_name }} 语法.

How can I get the current URL from a Twig template?

I am using Twig with PHP, without any other framework.

解决方案

Finding the current URL

The current URL is supplied by your web server and written to the $_SERVER super-global. Run this small script, <?php echo '<pre>'; print_r($_SERVER);, through your server and root around to find the value(s) you are looking for.

Related questions on this subject:

The PHP manual describes the nature of the available $_SERVER values here.

Getting the URL in TWIG

After you have the URL, you need to pass it as a template variable when calling render(...) on the Twig template instance. For example, you might code this.

$current_url = // figure out what the current url is

// pass the current URL as a variable to the template
echo $template->render(array('current_url' => $current_url));

To use the variable in the template, you use the {{ variable_name }} syntax.

这篇关于Twig 模板引擎:获取当前 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆