Loading...
Loading...
Compare original and translation side by side
assets/theme-template/THEMENAMETHEMELABEL.info.yml.libraries.yml.theme.breakpoints.ymltemplates/css/js/references/theming.mdassets/theme-template/THEMENAMETHEMELABEL.info.yml.libraries.yml.theme.breakpoints.ymltemplates/css/js/references/theming.md{{ variable }}{% if/for/set %}{# comment #}{{ var|filter }}{% extends %}{% block %}{{ 'Text'|t }}{{ attach_library('theme/library') }}{{ url('route.name') }}{{ path('route.name') }}{{ file_url('public://image.jpg') }}{{ content|without('field') }}page.html.twignode.html.twigblock.html.twigfield.html.twigviews-view.html.twig{{ variable }}{% if/for/set %}{# comment #}{{ var|filter }}{% extends %}{% block %}{{ 'Text'|t }}{{ attach_library('theme/library') }}{{ url('route.name') }}{{ path('route.name') }}{{ file_url('public://image.jpg') }}{{ content|without('field') }}page.html.twignode.html.twigblock.html.twigfield.html.twigviews-view.html.twigfunction THEMENAME_preprocess_HOOK(&$variables) {
// Add or modify variables
// Access entities, services, configuration
// Prepare data for templates
}hook_preprocess_page()hook_preprocess_node()hook_preprocess_block()hook_preprocess_field()hook_preprocess_html()function THEMENAME_preprocess_HOOK(&$variables) {
// 添加或修改变量
// 访问实体、服务、配置
// 为模板准备数据
}hook_preprocess_page()hook_preprocess_node()hook_preprocess_block()hook_preprocess_field()hook_preprocess_html()page--front.html.twig # Homepage
page--node--{nid}.html.twig # Specific node
page--node--{type}.html.twig # Content type
node--{type}--{viewmode}.html.twig # Type + view mode
block--{plugin-id}.html.twig # Specific block
field--{entity}--{field}.html.twig # Specific fieldfunction THEMENAME_theme_suggestions_HOOK_alter(array &$suggestions, array $variables) {
// Add custom suggestions
$suggestions[] = 'custom__suggestion';
}page--front.html.twig # 首页
page--node--{nid}.html.twig # 指定ID的内容
page--node--{type}.html.twig # 指定内容类型
node--{type}--{viewmode}.html.twig # 内容类型+展示模式
block--{plugin-id}.html.twig # 指定区块
field--{entity}--{field}.html.twig # 指定字段function THEMENAME_theme_suggestions_HOOK_alter(array &$suggestions, array $variables) {
// 添加自定义建议
$suggestions[] = 'custom__suggestion';
}undefinedundefined
**Loading libraries:**
- Global: Define in `.info.yml`
- Conditional: Use `attach_library()` in templates
- Preprocessed: Attach in preprocess functions
**Best practices:**
- Separate CSS into layers (base, layout, component, theme)
- Use CSS aggregation in production
- Minimize JavaScript dependencies
- Leverage Drupal's asset library system
**加载库的方式:**
- 全局加载:在 `.info.yml` 中定义
- 条件加载:在模板中使用 `attach_library()`
- 预处理加载:在预处理函数中挂载
**最佳实践:**
- 将CSS分层(基础层、布局层、组件层、主题层)
- 生产环境启用CSS聚合
- 最小化JavaScript依赖
- 充分利用Drupal的资源库系统undefinedundefined
**Mobile-first approach:**
1. Design for smallest screens first
2. Enhance for larger viewports
3. Use responsive images
4. Test across devices
5. Follow accessibility standards (WCAG)
**移动端优先方案:**
1. 优先为最小屏幕设计
2. 针对更大视口做增强
3. 使用响应式图片
4. 跨设备测试
5. 遵循可访问性标准(WCAG)cp -r assets/theme-template/ /path/to/drupal/themes/custom/mytheme/
cd /path/to/drupal/themes/custom/mytheme/
# Rename files
mv THEMENAME.info.yml mytheme.info.yml
mv THEMENAME.libraries.yml mytheme.libraries.yml
mv THEMENAME.theme mytheme.thememytheme.info.ymlmytheme.libraries.ymlTHEMENAMETHEMELABELddev drush cr
# Enable via UI at /admin/appearance or:
ddev drush config:set system.theme default mytheme -ysites/default/default.services.ymlsites/default/services.ymltwig.config.debug: truetwig.config.auto_reload: truetwig.config.cache: falseddev drush crtemplates/css/ddev drush crcp -r assets/theme-template/ /path/to/drupal/themes/custom/mytheme/
cd /path/to/drupal/themes/custom/mytheme/
# 重命名文件
mv THEMENAME.info.yml mytheme.info.yml
mv THEMENAME.libraries.yml mytheme.libraries.yml
mv THEMENAME.theme mytheme.thememytheme.info.ymlmytheme.libraries.ymlTHEMENAMETHEMELABELddev drush cr
# 可通过 /admin/appearance UI启用,或执行以下命令:
ddev drush config:set system.theme default mytheme -ysites/default/default.services.ymlsites/default/services.ymltwig.config.debug: truetwig.config.auto_reload: truetwig.config.cache: falseddev drush crtemplates/css/ddev drush cr.themeddev drush cr.themeddev drush cr<!-- FILE NAME SUGGESTIONS:
* page--node--123.html.twig
* page--node--article.html.twig
* page--node.html.twig
x page.html.twig
-->x<!-- FILE NAME SUGGESTIONS:
* page--node--123.html.twig
* page--node--article.html.twig
* page--node.html.twig
x page.html.twig
-->xfunction mytheme_preprocess_page(&$variables) {
// Add site slogan
$variables['site_slogan'] = \Drupal::config('system.site')->get('slogan');
// Add current user
$variables['is_logged_in'] = \Drupal::currentUser()->isAuthenticated();
// Add custom class
$variables['attributes']['class'][] = 'custom-page-class';
}function mytheme_preprocess_page(&$variables) {
// 添加站点标语
$variables['site_slogan'] = \Drupal::config('system.site')->get('slogan');
// 添加当前用户登录状态
$variables['is_logged_in'] = \Drupal::currentUser()->isAuthenticated();
// 添加自定义类
$variables['attributes']['class'][] = 'custom-page-class';
}{# templates/page--article.html.twig #}
{% extends "page.html.twig" %}
{% block content %}
<div class="article-wrapper">
{{ parent() }}
</div>
{% endblock %}{# templates/page--article.html.twig #}
{% extends "page.html.twig" %}
{% block content %}
<div class="article-wrapper">
{{ parent() }}
</div>
{% endblock %}function mytheme_preprocess_node(&$variables) {
if ($variables['node']->bundle() == 'article') {
$variables['#attached']['library'][] = 'mytheme/article-styles';
}
}function mytheme_preprocess_node(&$variables) {
if ($variables['node']->bundle() == 'article') {
$variables['#attached']['library'][] = 'mytheme/article-styles';
}
}{# Get field value #}
{{ node.field_custom.value }}
{# Check if field has value #}
{% if node.field_image|render %}
{{ content.field_image }}
{% endif %}
{# Loop through multi-value field #}
{% for item in node.field_tags %}
{{ item.entity.label }}
{% endfor %}{# 获取字段值 #}
{{ node.field_custom.value }}
{# 判断字段是否有值 #}
{% if node.field_image|render %}
{{ content.field_image }}
{% endif %}
{# 遍历多值字段 #}
{% for item in node.field_tags %}
{{ item.entity.label }}
{% endfor %}ddev drush crddev drush cr.libraries.yml.libraries.yml{{ dump() }}{{ dump() }}sites/default/services.ymltwig.config.debug: trueddev drush crsites/default/services.ymltwig.config.debug: trueddev drush crreferences/theming.mdreferences/theming.mdassets/theme-template/.info.yml.libraries.yml.themeassets/theme-template/.info.yml.libraries.yml.themeundefinedundefinedundefinedundefined