493333王中王开奖结果-王中王开奖一马中特

493333王中王开奖结果让不同类型的网络可以可靠地互相联接,王中王开奖一马中特拥有规模庞大的原创游戏,在这里您将享受的不一样快乐。

网络频道

当前位置:493333王中王开奖结果 > 网络频道 > 【Drupal 8】中调试Twig模板中的变量教程王中王开

【Drupal 8】中调试Twig模板中的变量教程王中王开

来源:http://www.soniaLorenzana.com 作者:493333王中王开奖结果 时间:2019-09-30 17:05

常言:工欲善其事,必先利其器。Drupal 主题开发中技巧也不少,学会这些技巧会起到事半功倍的效果。今天给大家推介的是如何调试打印出Twig模板中的变量,不妨一看!Oops,Drupal 8!

Adding CSS Classes to Blocks in Drupal 8

在drupal 8 twig模板中,你可以在注释中找到大部分、常用的模板变量。但是,当一些模块或者主题引入新变量的时候,该怎么办呢?

原文链接:http://atendesigngroup.com/blog/adding-css-classes-blocks-drupal-8

Twig模板提供了一个函数——‘dump’,方便你去发现、检查这些变量。

John FerrisApril 20, 2016

使用‘dump’函数有个小前提,你需要启用twig的debug模式。关于如何启用twig的debug模式 https://www.drupal.org/node/1903374。

王中王开奖一马中特 1

检查单个变量

This is an update to a previous post I wrote onadding classes to blocks in Drupal 7

{{ dump(title) }}

Interested in

简单吧!

Drupal 8 Theming?

那我想看所有的变量,怎么整?

Attend a free 90 minute workshop online or in person in Denver to learn more about theming in Drupal 8. Specific dates TBD.

找出所有变量

SIGN UP AND BE THE FIRST TO KNOW:

{{ dump() }}

AsI've stated before, I'm a big fan of Modular CSS which requires the ability to easily manage classes on your markup. This was often a struggle in previous versions of Drupal. However, Drupal 8 makes this significantly easier to manage thanks to a number of improvements to the front-end developer experience (DX). In this post we'll look at how two of these DX improvements, theTwigtemplate language andhook_theme_suggestions_HOOK_alter, and how they make adding classes to blocks much easier to manage.

找出变量可用的key

Twig allows us to easily open up a template file and add our classes where we need them. There are two main approaches to adding classes to a template file. The first is simple: open the file, add the class directly to the tag, save the file and move on with your life.

{{ dump(_context|keys) }}

block.html.twig

Twig中的全局可用变量

{{ title_prefix }}

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.

{% if label %}

友情提示

{{ label }}

如果你想查看所有的变量,但是‘dump’函数因为递归等原因把内存耗尽了,你可以通过以下方式来处理:

{% endif %}

<ol>
    {% for key, value in _context %}
    <li>{{ key }}</li>
    {% endfor %}
</ol>

{{ title_suffix }}

然后用一个约束条件去检查这些变量,如 { if loop.index = 2 %} 这样就可以找到你所需要的内容啦!

{% block content %}

{{ content }}

{% endblock %}

This works in a lot of cases, but may not be flexible enough. The second approach utilizes the newattributes object– the successor to Drupal 7's attributes array. The attribute object encapsulates all the attributes for a given tag. It also includes a number of methods which enable you to add, remove and alter those attributes before printing. For now we'll just focus on theattributes.addClass()method. You can learn more about available methods in the officialDrupal 8 documentation.

block.html.twig

{%

set classes = [

'block',

'block--fancy'

]

%}

{%

set title_classes = [

'block__title',

'block__title--fancy'

]

%}

{{ title_prefix }}

本文由493333王中王开奖结果发布于网络频道,转载请注明出处:【Drupal 8】中调试Twig模板中的变量教程王中王开

关键词: