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

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

问题描述

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

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

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

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

推荐答案

查找当前网址

当前 URL 由您的 Web 服务器提供并写入 $_SERVER 超级全局.运行这个小脚本,<?php echo '<pre>';print_r($_SERVER);,通过你的服务器和 root 来找到你正在寻找的值.

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:

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

在 TWIG 中获取 URL

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

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));

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

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

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

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