我正在使用Ansible生成Behat配置文件。此配置文件是YAML文件。我正在使用这样的Jinja2模板:
default:
paths:
features: '../all/tests/features'
filters:
tags: "~@api&&~@drush"
extensions:
Behat\MinkExtension\Extension:
files_path: '{{ project_docroot }}/sites/all/tests/files'
files_path: '{{ project_docroot }}'
goutte: ~
selenium2: ~
base_url: '{{ base_url }}'
Drupal\DrupalExtension\Extension:
blackbox: ~
drush_driver: "drush"
drush:
root: "{{ project_docroot }}"
api_driver: "drupal"
drupal:
drupal_root: "{{ project_docroot }}"
region_map:
{{ project_behat_region_map }}
selectors:
{{ project_behat_selectors }}
以下定义的变量:
project_behat_region_map: |
content: "#content"
footer: "#footer"
header: "#header"
header bottom: "#header-bottom"
navigation: "#navigation"
highlighted: "#highlighted"
help: "#help"
bottom: "#bottom"
project_behat_selectors: |
message_selector: '.messages'
error_message_selector: '.messages.error'
success_message_selector: '.messages.status'
warning_message_selector: '.messages.warning'
正如您所看到的那样,变量值是缩进的,但是当粘贴到Jinja2模板中时会丢失缩进:
default:
paths:
features: '../all/tests/features'
filters:
tags: "~@api&&~@drush"
extensions:
Behat\MinkExtension\Extension:
files_path: '/var/www//bacteriemias/docroot/sites/all/tests/files'
files_path: '/var/www//bacteriemias/docroot'
goutte: ~
selenium2: ~
base_url: 'http://bacteriemias.me'
Drupal\DrupalExtension\Extension:
blackbox: ~
drush_driver: "drush"
drush:
root: "/var/www//bacteriemias/docroot"
api_driver: "drupal"
drupal:
drupal_root: "/var/www//bacteriemias/docroot"
region_map:
content: "#content"
footer: "#footer"
header: "#header"
header bottom: "#header-bottom"
navigation: "#navigation"
highlighted: "#highlighted"
help: "#help"
bottom: "#bottom"
selectors:
message_selector: '.messages'
error_message_selector: '.messages.error'
success_message_selector: '.messages.status'
warning_message_selector: '.messages.warning'
这不是有效的YAML。如何在Jinja2中打印带缩进的变量?
答案 0 :(得分:11)
事实证明,使用indent Jinja2 filter可以解决问题。
缩进(s,width = 4,indentfirst = False)
返回传递的副本 string,每行缩进4个空格。第一行不是 缩进。如果要更改空格数或缩进 第一行也可以将其他参数传递给过滤器:
{{mytext | indent(2,true)}} 缩进两个空格并缩进第一行。
所以,在我的情况下是:
default:
paths:
features: '../all/tests/features'
filters:
tags: "~@api&&~@drush"
extensions:
Behat\MinkExtension\Extension:
files_path: '{{ project_docroot }}/sites/all/tests/files'
files_path: '{{ project_docroot }}'
goutte: ~
selenium2: ~
base_url: '{{ base_url }}'
Drupal\DrupalExtension\Extension:
blackbox: ~
drush_driver: "drush"
drush:
root: "{{ project_docroot }}"
api_driver: "drupal"
drupal:
drupal_root: "{{ project_docroot }}"
region_map:
{{ project_behat_region_map | indent( width=8, indentfirst=True) }}
selectors:
{{ project_behat_selectors | indent( width=8, indentfirst=True) }}