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

Real World Craft Tips & Tricks

Real World Craft Tips & Tricks

Trevor Davis

June 17, 2014
Tweet

More Decks by Trevor Davis

Other Decks in Technology

Transcript

  1. by Trevor Davis
    at Craft Summit
    on June 17, 2014
    © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    REAL WORLD

    CRAFT TIPS & TRICKS

    View full-size slide

  2. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    @trevor_davis
    HI! I’M
    I work as a Senior Front End Developer at Viget.
    TREVOR DAVIS
    2

    View full-size slide

  3. www.orbcomm.com
    URL

    View full-size slide

  4. trevordavis.net
    URL

    View full-size slide

  5. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {siteUrl}
    5

    View full-size slide

  6. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    # craft/config/general.php
    !
    return array(
    '*' => array(
    'environmentVariables' => array(
    'siteUrl' => 'http://' . $_SERVER['SERVER_NAME']
    )
    )
    );
    6

    View full-size slide

  7. Settings > General

    View full-size slide

  8. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    SWITCH
    8

    View full-size slide

  9. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% if craft.request.segments[1] == 'category' %}
    {% set title = 'Category' %}
    {% elseif craft.request.segments[1] == 'tag' %}
    {% set title = 'Tag' %}
    {% else %}
    {% set title = 'Index' %}
    {% endif %}
    9

    View full-size slide

  10. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% switch craft.request.segments[1] %}
    {% case 'category' %}
    {% set title = 'Category' %}
    !
    {% case 'tag' %}
    {% set title = 'Tag' %}
    !
    {% default %}
    {% set title = 'Index' %}
    !
    {% endswitch %}
    10

    View full-size slide

  11. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    CYCLE
    11

    View full-size slide

  12. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% for entry in craft.entries({ section: 'news' }) %}

    {{ entry.title }}
    {{ entry.summary }}

    {% endfor %}
    12

    View full-size slide

  13. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    DUMP
    13

    View full-size slide

  14. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {{ dump(entry) }}
    14

    View full-size slide

  15. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    ELSE FOR LOOPS
    16

    View full-size slide

  16. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% for entry in craft.entries({ section: 'news' }) %}!
    !
    {{ entry.title }}!
    {{ entry.summary }}!
    !
    {% else %}!
    Sorry, there is no news for this category.!
    {% endfor %}
    17

    View full-size slide

  17. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    LIMIT: 1,

    STILL NEED TO LOOP
    18

    View full-size slide

  18. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% set announcement = craft.entries({ section: 'announcement', limit: 1}) %}
    !
    {% for entry in announcement %}
    {{ entry.title }}
    {% endfor %}
    19

    View full-size slide

  19. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% set announcement = craft.entries({ section: 'announcement', limit: 1}).first() %}
    !
    {{ announcement.title }}
    20

    View full-size slide

  20. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    NO LIMIT
    21

    View full-size slide

  21. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% set retailers = craft.entries({ section : 'retailer', limit: 1000 }) %}
    22

    View full-size slide

  22. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% set retailers = craft.entries({ section : 'retailer', limit: null }) %}
    23

    View full-size slide

  23. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    DEFAULT FILTER
    24

    View full-size slide

  24. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% set currentFilter = craft.request.segments[2] | default('All') %}
    25

    View full-size slide

  25. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    ACCESS CUSTOM
    FIELD SETTINGS
    26

    View full-size slide

  26. Media Filters

    View full-size slide

  27. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.

    {% for option in craft.fields.getFieldbyHandle('activity').settings.options %}
    {{ option.label }}
    {% endfor %}

    28

    View full-size slide

  28. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    MATRIX
    29

    View full-size slide

  29. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% for entry in craft.entries({ section: 'news', limit: 5 }) %}
    {% for block in entry.bodyBuilder.type('image') %}
    ...
    {% endfor %}
    {% endfor %}
    30

    View full-size slide

  30. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% for entry in craft.entries({ section: 'news', limit: 5 }) %}
    {% for block in entry.bodyBuilder.type('video, image').limit(1) %}
    ...
    {% endfor %}
    {% endfor %}
    31

    View full-size slide

  31. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% for milestones in entry.companyTimeline.order('date desc') %}
    ...
    {% endfor %}
    32

    View full-size slide

  32. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    LOOP ALL THE THINGS
    33

    View full-size slide

  33. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% for i in 1..5 %}

    Here is the title {{ i }}

    Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
    Aenean commodo ligula eget dolor. Aenean massa. Cum sociis
    natoque penatibus et magnis dis parturient montes.


    {% endfor %}
    34

    View full-size slide

  34. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.

    General Inquiries
    Order Status
    Press & Media Inquiries
    Other

    35

    View full-size slide

  35. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {%
    set subjects = {
    'General Inquiries' : 'general_inquiries',
    'Order Status' : 'order_status',
    ‘Press & Media Inquiries' : 'press_media',
    'Other' : 'other_contact',
    }
    %}
    !

    {% for label, value in subjects %}
    {{ label }}
    {% endfor %}

    36

    View full-size slide

  36. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.


    Home

    !

    About

    !

    Blog

    !

    Contact


    37

    View full-size slide

  37. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {%
    set nav = {
    'Home' : '/',
    'About' : '/about',
    'Blog' : '/blog',
    'Contact' : '/contact'
    }
    %}
    !

    {% for label, url in nav %}

    {{ label }}

    {% endfor %}

    38

    View full-size slide

  38. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    RELATEDTO
    39

    View full-size slide

  39. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% set tag = craft.tags({ name: tag, set: 'news' }).first() %}
    {% set entries = craft.entries({ section: 'news', limit: 5, relatedTo: tag }) %}
    40

    View full-size slide

  40. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% set allMedia = craft.entries({ section: 'media', limit: null }) %}
    {% set products = craft.entries({ section: 'product', relatedTo: allMedia }) %}
    41

    View full-size slide

  41. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    MERGE
    42

    View full-size slide

  42. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% set default = { section: 'media', limit: 12 } %}
    {% set options = { limit: 5, order: 'title asc' } %}
    {% set result = default | merge(options) %}
    !
    { section: 'media', limit: 5, order: 'title asc'}
    43

    View full-size slide

  43. Media Filters

    View full-size slide

  44. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% set filters = ['type', 'product', 'activity', 'element'] %}
    {% set params = { section: 'media', limit: 12 } %}
    !
    {#% Apply filter? %#}
    {% if craft.request.segments[2] is defined and craft.request.segments[1] in filters %}
    {% switch craft.request.segments[1] %}
    {% case 'product' %}
    {% set product = craft.entries({ slug: craft.request.segments[2], section:
    'product' }).first() %}
    {% set params = params | merge({ relatedTo: product }) %}
    !
    {% case 'type' %}
    {% set params = params | merge({ type: craft.request.segments[2] }) %}
    !
    ...
    {% endswitch %}
    {% endif %}
    !
    {% set entries = craft.entries(params) %}
    45

    View full-size slide

  45. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    CHANNEL VS.
    STRUCTURE
    46

    View full-size slide

  46. Media Section Settings

    View full-size slide

  47. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% set params = { section: 'media', limit: 12, order: 'lft desc' } %}
    !
    {% set entries = craft.entries(params) %}
    48

    View full-size slide

  48. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    VARIABLES &
    INCLUDES
    49

    View full-size slide

  49. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% set heading = 'Here is my heading' %}
    {% include 'shared/_sidebar' %}
    50

    View full-size slide

  50. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% include 'shared/_sidebar' with { 'heading' : 'Here is my heading' } %}
    51

    View full-size slide

  51. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% set otherVar = 'Oh hai' %}
    {% include 'shared/_sidebar' with { 'heading' : 'Here is my heading' } only %}
    52

    View full-size slide

  52. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% if entry.listingSection %}
    {% include '_shared/listing_' ~ entry.listingSection ignore missing %}
    {% endif %}
    53

    View full-size slide

  53. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    PARENT
    54

    View full-size slide

  54. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    # _layout.html
    !







    {% block content %}{% endblock %}

    !

    {% block sidebar %}

    About
    Blog
    Contact

    {% endblock %}



    55

    View full-size slide

  55. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    # page.html
    !
    {% extends '_layout' %}
    !
    {% block content %}
    Here is the content
    {% endblock %}
    !
    {% block sidebar %}
    {{ parent() }}
    !
    Here is some additional sidebar content
    {% endblock %}
    56

    View full-size slide

  56. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    LOOP GROUPING
    57

    View full-size slide

  57. companyTimeline field

    View full-size slide

  58. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% if entry.companyTimeline | length %}

    {% for year, milestones in entry.companyTimeline.order('date desc') | group('date.year') %}

    {{ year }}

    {% for milestone in milestones %}

    {{ milestone.milestoneTitle }}
    {{ milestone.description }}

    {% endfor %}


    {% endfor %}

    {% endif %}
    59

    View full-size slide

  59. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    UPDATING IN
    MULTIPLE
    ENVIRONMENTS
    60

    View full-size slide

  60. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    ‎ Auto-update locally
    ‎ Commit and deploy the files
    ‎ Visit the CP on additional environments
    ‎ DB update process will run
    UPDATING IN MULTIPLE ENVIRONMENTS
    61

    View full-size slide

  61. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    # craft/config/general.php
    !
    return array(
    '*' => array(
    'allowAutoUpdates' => false
    ),
    !
    '.dev' => array(
    'allowAutoUpdates' => true,
    'devMode' => true,
    )
    );
    62

    View full-size slide

  62. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    CACHING IN
    MULTIPLE
    ENVIRONMENTS
    63

    View full-size slide

  63. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    # craft/config/general.php
    !
    !
    return array(
    ...
    !
    '.dev' => array(
    'devMode' => true
    )
    );
    64

    View full-size slide

  64. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    # _layout.html
    !
    {%- cache unless craft.config.devMode -%}






    ...


    {%- endcache -%}
    65

    View full-size slide

  65. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    MACROS
    66

    View full-size slide

  66. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    # _helpers/index.html
    !
    {%- macro map_link(address) -%}
    http://maps.google.com/?q={{ address | url_encode }}
    {%- endmacro -%}
    67

    View full-size slide

  67. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% import "_helpers" as helpers %}
    !
    Map
    68

    View full-size slide

  68. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    Map
    69

    View full-size slide

  69. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    # _helpers/index.html
    !
    {%- macro input(options) -%}
    name="{{ options.name }}"
    class="{{ options.class | default('input') }}">
    {%- endmacro -%}
    70

    View full-size slide

  70. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    # _helpers/index.html
    !
    {%- macro input(options) -%}
    {%
    set defaults = {
    type: 'text',
    class: 'input'
    }
    %}
    {% set options = defaults | merge(options) %}
    !
    name="{{ options.name }}"
    class="{{ options.class }}">
    {%- endmacro -%}
    71

    View full-size slide

  71. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    {% import "_helpers" as helpers %}
    !
    {{ helpers.input({ name: 'somefield' }) }}
    72

    View full-size slide

  72. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.

    73

    View full-size slide

  73. © Viget Labs, LLC • This presentation is CONFIDENTIAL and should not be shared without permission.
    web: viget.com
    web: trevordavis.net
    twitter: @trevor_davis
    Thanks!
    74

    View full-size slide