常言:工欲善其事,必先利其器。Drupal 主题开发中技巧也不少,学会这些技巧会起到事半功倍的效果。今天给大家推介的是如何调试打印出Twig模板中的变量,不妨一看!Oops,Drupal 8!
在drupal 8 twig模板中,你可以在注释中找到大部分、常用的模板变量。但是,当一些模块或者主题引入新变量的时候,该怎么办呢?
Twig模板提供了一个函数——‘dump’,方便你去发现、检查这些变量。
使用‘dump’函数有个小前提,你需要启用twig的debug模式。关于如何启用twig的debug模式 https://www.dr***upal.org/node/1903374。
检查单个变量
{{ dump(title) }}
简单吧!
那我想看所有的变量,怎么整?
找出所有变量
{{ dump() }}
找出变量可用的key
{{ dump(_context|keys) }}
Twig中的全局可用变量
There are additional global variables available in all Twig templates:
_self references the current template and contains advanced information about a template, i.e. the compiled template class name and information about the Twig environment.
_context references the current context and contains all variables passed to the template such as variables sent from theme(), prepared by preprocess, or set in the template. Adding {{ dump() }} without specifying a variable is equivalent to {{ dump(_context) }}.
_charset references the current charset.
友情提示
如果你想查看所有的变量,但是‘dump’函数因为递归等原因把内存耗尽了,你可以通过以下方式来处理:
天才足球经理
天才足球经理是一款经典的足球模拟策略类手游。精致的人物建模和
魔法学园手游
魔法学园是一款二次元题材的少女养成手游,玩家在游戏中需要经营
樱花校园模拟器4399中文版
樱花校园模拟器4399中文版,十分奇趣好玩的3d校园冒险模拟
我的暑假秘密回忆繁星新春版
我的暑假秘密回忆繁星新春版是一款少女模拟养成类休闲游戏。这款
樱花校园模拟器国际版
樱花校园模拟器国际版,一个超级自由超级好玩的自由恋爱模拟的游