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 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 Slide

  3. www.orbcomm.com
    URL

    View Slide

  4. trevordavis.net
    URL

    View Slide

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

    View 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 Slide

  7. Settings > General

    View Slide

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

    View 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 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 Slide

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

    View 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 Slide

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

    View Slide

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

    View Slide

  15. View Slide

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

    View Slide

  17. © 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 Slide

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

    STILL NEED TO LOOP
    18

    View Slide

  19. © 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 Slide

  20. © 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  27. Media Filters

    View Slide

  28. © 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 Slide

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

    View 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('image') %}
    ...
    {% endfor %}
    {% endfor %}
    30

    View Slide

  31. © 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 Slide

  32. © 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 Slide

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

    View Slide

  34. © 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 Slide

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

    General Inquiries
    Order Status
    Press & Media Inquiries
    Other

    35

    View Slide

  36. © 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 Slide

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


    Home

    !

    About

    !

    Blog

    !

    Contact


    37

    View Slide

  38. © 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 Slide

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

    View Slide

  40. © 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 Slide

  41. © 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 Slide

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

    View Slide

  43. © 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 Slide

  44. Media Filters

    View Slide

  45. © 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 Slide

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

    View Slide

  47. Media Section Settings

    View Slide

  48. © 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 Slide

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

    View Slide

  50. © 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 Slide

  51. © 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 Slide

  52. © 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 Slide

  53. © 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 Slide

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

    View Slide

  55. © 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 Slide

  56. © 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 Slide

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

    View Slide

  58. companyTimeline field

    View Slide

  59. © 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 Slide

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

    View Slide

  61. © 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 Slide

  62. © 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 Slide

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

    View Slide

  64. © 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 Slide

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






    ...


    {%- endcache -%}
    65

    View Slide

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

    View Slide

  67. © 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 Slide

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

    View Slide

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

    View Slide

  70. © 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 Slide

  71. © 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 Slide

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

    View Slide

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

    73

    View Slide

  74. © 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 Slide