Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Drupal テーマ開発入門講座 第4回 テンプレートのカスタマイズ

Drupal テーマ開発入門講座 第4回 テンプレートのカスタマイズ

More Decks by アクイアジャパン Acquia Japan

Other Decks in Programming

Transcript

  1. 1. Twigͷ֓ཁ 2. جຊతͳTwigߏจ 3. ؔ਺ 4. ϑΟϧλ 5. ςϯϓϨʔτͷσόοά

    6. ϓϦϓϩηεؔ਺Λ࢖༻ͨ͠ม਺ͷ௥Ճ 7. ςϯϓϨʔτͷܧঝ 8. ࿅श໰୊ ΞδΣϯμ
  2. ࢀߟ) ςϯϓϨʔτΤϯδϯͱ͸ – ςϯϓϨʔτͱݺ͹ΕΔ਽ܗͱɺ͋ΔσʔλϞσϧͰදݱ͞ΕΔೖྗσʔλΛ߹੒͠ɺ੒ՌυΩϡϝϯτΛ ग़ྗ͢Διϑτ΢ΣΞ - wikipedia
 – PHPք۾ͰΑ͘ར༻͞ΕΔςϯϓϨʔτΤϯδϯ –

    Twig (Symfonyඪ४ ) – Blade (Laravelඪ४ ) – Smarty
 – ͳͥςϯϓϨʔτΤϯδϯΛ࢖༻͢Δͷʁ – ViewͱϩδοΫͷ෼཭ – XSS߈ܸͷճආʢࣗಈΤεέʔϓॲཧʣ - શͯͷHTMLΛTwigςϯϓϨʔτͰग़ྗ͢Δ͜ͱ͕๬·͍͠ Ͱ͢ɻWriting secure code for Drupal ࢀর
 – Drupal 8Ҏલ(ver 4.7 ~ 7.x)Ͱ͸ʮPHPTemplateʯ͕ςϯϓϨʔτΤϯδϯͱͯ͠࠾༻͞Ε͍ͯ·ͨ͠ɻ
  3. Twigͷ2ͭͷߏจ {{ ɹ}} …ʮԿ͔Λݴ͏ʯߏจ {% %} …ʮԿ͔Λ͢Δʯߏจ Say Something Do

    Something https://symfonycasts.com/screencast/twig/basics#the-2-syntaxes-of-twig- code-code-and-code-code
  4. ྫɿfooม਺Λఆٛ͢Δ ྫɿ഑ྻΛఆٛ͢Δ ม਺ͷఆٛ {% set foo = 'foo' %} {%

    set classes = [ 'js-form-managed-file', 'form-managed-file', ] %}
  5. ੍ޚߏ଄ ྫɿifจ ྫɿforจ {% if page.footer %} <footer role="contentinfo"> {{

    page.footer }} </footer> {% endif %} <span{{ attributes }}> {%- for item in items -%} {{ item.content }} {%- endfor -%} </span>
  6. Twigͷ૊ΈࠐΈؔ਺ͷྫ Twigຊମʹศརͳؔ਺͕༻ҙ͞Ε͍ͯ·͢ɻ ྫɿrandomؔ਺ ྫɿdumpؔ਺ {{ random('ABC') }} {# AɺBɺCͷ͍ͣΕ͔͕ग़ྗ #}

    {{ dump(user) }} {# userม਺ʹؔ͢Δ৘ใΛμϯϓ #} https://twig.symfony.com/doc/2.x/functions/index.html
  7. Drupalݻ༗ͷؔ਺ͷྫ DrupalίΞͰ༻ҙ͞Ε͍ͯΔศརͳؔ਺ͷҰྫͰ͢ɻ ྫɿattach_library($library ) ྫɿcreate_attribute($attributes ) ྫɿpath($name, $parameters, $options) {{

    attach_library('classy/node') }} <div{{ create_attribute({'class': ['apple', 'orange']}) }}>Fruit</div> {# <div class="apple orange">Fruit</div> ͱग़ྗ͞ΕΔ #} <a href="{{ path('user.register') }}">Register</a> {# <a href="/user/register">Register</a> ͱग़ྗ͞ΕΔ #} https://www.drupal.org/docs/theming-drupal/twig-in-drupal/functions-in-twig-templates
  8. TwigຊମʹศརͳϑΟϧλ͕༻ҙ͞Ε͍ͯ·͢ɻ ྫɿupperϑΟϧλ ྫɿlastϑΟϧλ ྫɿdefaultϑΟϧλ Twigͷ૊ΈࠐΈϑΟϧλͷྫ {{ 'welcome'|upper }} {# WELCOME

    ͱେจࣈͰग़ྗ͞Ε·͢ #} {{ [1, 2, 3, 4]|last }} {# 4 ͱग़ྗ͞Ε·͢ #} {{ var|default('var͸ະఆٛͰ͢') }} {# var͕ະఆٛ·ͨ͸ۭനͷ৔߹ɺׅހ಺ͷจࣈྻ͕ग़ྗ #} https://twig.symfony.com/doc/2.x/filters/index.html
  9. ςϯϓϨʔτͰ࢖༻Մೳͳม਺ͷදࣔ ᶃdumpؔ਺Λ࢖༻͢Δ 
 
 
 {{ dump(url) }} {# in

    node.html.twig #} dumpؔ਺Ͱࢦఆ͠ ͨม਺ͷ஋͕ग़ྗ͞ ΕΔ
  10. – ϓϦϓϩηεؔ਺(Preprocess functions)ͱ͸ɺςʔϚͱϞδϡʔϧ͕ TwigςϯϓϨʔτϑΝΠϧʹ౉͞ΕΔม਺ΛมߋͰ͖ΔΑ͏ʹ͢ΔPHP ؔ਺Ͱ͢ɻ – *.themeϑΝΠϧʹ࣍ͷؔ਺Λهड़͍͖ͯ͠·͢ɻ – THEME_preprocess(&$variables, $hook)

    
 ςϯϓϨʔτͱ࣮ͯ͠૷͞ΕͨશͯͷςʔϚϑοΫͰݺͼग़͞ΕΔ – THEME_preprocess_HOOK(&$variables) 
 HOOKͰࢦఆͨ͠ςʔϚϑοΫͰݺͼग़͞ΕΔ ϓϦϓϩηεؔ਺ͱ͸
  11. ϑοΫ໊ͷ໋໊نଇ – HOOKʹ෇͚Δ໊લ͸TwigςϯϓϨʔτͷsuggestionsʹै͍·͢ɻϋΠϑϯ -- ΛΞϯμʔείΞ __ ʹஔ͖׵͑·͢ɻ
 
 
 


    
 
 
 – ྫɿnode--article.html.twig ͷม਺Λॲཧ͢Δ৔߹ – THEME_preprocess_node__42() 
 ࠷΋۩ମతͳόʔδϣϯɻϊʔυID͕42ͷϊʔυʹରͯ͠ͷΈݺͼग़͞ΕΔɻ – THEME_preprocess_node__article() 
 શͯͷهࣄίϯςϯπλΠϓʹରͯ͠ݺͼग़͞ΕΔɻ – THEME_preprocess_node() 
 શͯͷϊʔυʹରͯ͠ݺͼग़͞ΕΔɻ
  12. ςϯϓϨʔτΛ֦ு͢Δ – TwigͷextendsλάΛ࢖༻ͯ͠ɺςϯϓϨʔτؒʹ਌ࢠؔ܎Λ࣋ͨͤͯɺςϯϓϨʔτͷίʔυͷ ॏෳΛݮΒ͢͜ͱ͕Ͱ͖·͢ɻ – ਌ςϯϓϨʔτͰ͸ block λάΛ࢖༻ͯ͠ࢠςϯϓϨʔτͰ্ॻ͖Ͱ͖ΔྖҬΛఆٛ͠·͢ɻ – ࢠςϯϓϨʔτͰ͸

    extends λάͰ਌ςϯϓϨʔτΛࢦఆ͠ɺ਌ςϯϓϨʔτͰ༻ҙ͞Εͨϒϩο ΫྖҬΛ࢖༻্ͯ͠ॻ͖͠·͢ɻ {# node.html.twig #} {# ਌ςϯϓϨʔτ #} <div{{ attributes }}> {% block content %} <p>Hello</p> {% endblock %} </div> {# node--article.html.twig #} {# ࢠςϯϓϨʔτ #} {% extends "node.html.twig" %} {% block content %} <p>Overide!</p> {% endblock %} blockλάͰ ্ॻ͖Ͱ͖Δ ྖҬΛఆٛ extendsλάͰ਌ͱͳΔ ςϯϓϨʔτΛࢦఆ ਌ςϯϓϨʔτͰ༻ҙ ͞ΕͨྖҬΛblockλά Ͱࢦఆ্ͯ͠ॻ͖͢Δ https://twig.symfony.com/doc/2.x/tags/extends.html https://www.drupal.org/docs/theming-drupal/twig-in-drupal/extending-templates
  13. ଞͷςϯϓϨʔτΛؚΊΔ – TwigͷincludeλάΛ࢖༻ͯ͠ɺςϯϓϨʔτʹଞͷςϯϓϨʔτΛؚΊ Δ͜ͱ͕Ͱ͖·͢ɻ {# field-ui-table.html.twig #} <div id="field-display-overview-wrapper"> {%

    include 'table.html.twig' %} </div> includeλάͰૠೖ͍ͨ͠ςϯ ϓϨʔτϑΝΠϧ໊Λࢦఆ https://twig.symfony.com/doc/2.x/tags/include.html https://www.drupal.org/docs/8/theming-drupal-8/including-part-template
  14. 1. Twigͷ֓ཁ 2. جຊతͳTwigߏจ 3. ؔ਺ 4. ϑΟϧλ 5. ςϯϓϨʔτͷσόοά

    6. ϓϦϓϩηεؔ਺Λ࢖༻ͨ͠ม਺ͷ௥Ճ 7. ςϯϓϨʔτͷܧঝ 8. ࿅श໰୊ ΞδΣϯμ