Twig templates engine: get current url(树枝模板引擎:获取当前网址)
问题描述
如何从 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 确定当前 url
- 如何在 PHP 中获取当前页面的 URL一个>
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.
- 开发者的基本 Twig 文档
- 使用变量的 Twig 设计器文档.
这篇关于树枝模板引擎:获取当前网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:树枝模板引擎:获取当前网址


基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01