Drupalising a Flat Theme
How you build a Drupal site will greatly depend on your personal preference or the preference of your boss/client.
A favourite debate between other developers and myself is if it's better to:
How you build a Drupal site will greatly depend on your personal preference or the preference of your boss/client.
A favourite debate between other developers and myself is if it's better to:
views-view-unformatted--testimonials--block-2.html.twig
{% for row in rows %} {% set row_classes = cycle(['views-row odd', 'views-row even'], loop.index0) %} <div{{ row.attributes.addClass(row_classes) }}> {{- row.content -}} </div> {% endfor %}
views-view-unformatted--home-news.html.twig
Within {% for row in rows %} we can use {{ loop.index }} like: <article id="newsitem{{ loop.index }}">.
Full output might look like:
{% for row in rows %} <article id="newsitem{{ loop.index }}" class="newsitem col-md-4 col-lg-4 col-sm-6 col-xs-12"> {{- row.content -}} </article> {% endfor %}
In D7 the copyright line in a tpl.php file might look like this:
<p>© Copyright <?php echo date('Y'); ?> Company Name</p>
In D8/9 the TWIG template needs to be like this:
<p>© Copyright {{ currentYear|raw }} Company Name</p>
D7 to D8/9 Differences
D7: print render($page['help']);
D8/9: {{ page.help }}
D7:
<?php if($page['sidebar_first']){ ?> <div class="sidebar-first"> <?php print render($page['sidebar_first']); ?> </div> <?php } ?>
D8/9:
{% if page.sidebar_first %} <div class="sidebar-first"> {{ page.sidebar_first }} </div> {% endif %}
{% if content.field_inner_hero %} <div id="innerhero"> {{ content.field_inner_hero }} </div> {% endif %}
Better
{% if content.field_inner_hero[0] %} <div id="innerhero"> {{ content.field_inner_hero }} </div> {% endif %}
{{ title_prefix }} {% if title %} <h1{{ title_attributes.addClass('title').setAttribute('id', 'page-title') }}>{{ title }}</h1> {% endif %} {{ title_suffix }}
Another way
<div id="nodebody{{ node.id }}" {{ content_attributes.addClass('nodearticle') }}>
This is such a vital part of the build process for D8/9 that it's a shame it's not something you just tick in the CMS. Then again, I believe the idea is to have the additional settings/files on your local machine and not on your production server. That way the production server is always fully cached and your development environment always has caching turned off. Which does make a lot of sense.
In Drupal 8/9 you create a MYTHEME.info.yml file to specify the theme name, and base themes (if this is a child theme), regions, etc. One of the items is a reference to your 'libraries'.