{"id":1031,"date":"2019-12-14T16:00:07","date_gmt":"2019-12-14T09:00:07","guid":{"rendered":"https:\/\/lab.wptips.dev\/?p=1031"},"modified":"2020-06-14T20:23:07","modified_gmt":"2020-06-14T13:23:07","slug":"var-dump-for-timber","status":"publish","type":"post","link":"https:\/\/pixelstudio.id\/blog\/var-dump-for-timber\/","title":{"rendered":"How to Debug Timber Theme? (Use var_dump)"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\"><p><strong>Timber<\/strong> is a plugin to allow separation between PHP and HTML files.  <a href=\"https:\/\/pixelstudio.id\/blog\/what-is-timber-library\/\">Click here<\/a> for introduction.<\/p><\/blockquote>\n\n\n\n<p>Timber is not a commonly-used plugin. Aside from the official documentation and few Stack Overflows threads, it can be tough to find the information you need.<\/p>\n\n\n\n<p>One convenient way to figure out Timber by yourself is to dump the variable and methods. We will show you how in this article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Add Dump Filter<\/h2>\n\n\n\n<p>We will add 2 new filters: <code>dump<\/code> for listing all variables and <code>methods<\/code> for listing all methods.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>NOTE:  Please read our <a rel=\"noreferrer noopener\" aria-label=\"tutorial on adding filter (opens in a new tab)\" href=\"\/global-var-n-filters-in-timber\/\" target=\"_blank\">tutorial on adding new filters<\/a> to know where to put the snippet below.<\/p><\/blockquote>\n\n\n\n<pre title=\"code\/timber.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">class MyTimber extends TimberSite {\n  ...\n  \n  function add_to_twig( $twig ) {\n    \/\/ only if set to Debug mode\n    if( defined('WP_DEBUG') &amp;&amp; WP_DEBUG === true ) {\n      $twig->addFilter( new Twig_SimpleFilter( 'dump', [$this, 'filter_dump'] ) );\n      $twig->addFilter( new Twig_SimpleFilter( 'methods', [$this, 'filter_methods'] ) );\n    }\n    return $twig;\n  }\n\n  function filter_dump( $var ) {\n    var_dump( $var );\n  }\n\n  function filter_methods( $object ) {\n    var_dump( get_class_methods( $object ) );\n  }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. Enable DEBUG Mode<\/h2>\n\n\n\n<p>Open your <code>wp-config.php<\/code> file and near the bottom of it, change WP_DEBUG to true.<\/p>\n\n\n\n<pre title=\"wp-config.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">...\ndefine('WP_DEBUG', true);\n...<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. Try it Out<\/h2>\n\n\n\n<p>Let&#8217;s say you want to display <strong>Author info<\/strong>. You are not sure how, so you check what&#8217;s inside <code>post<\/code> variable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">{{ post | dump }}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"590\" height=\"280\" src=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2019\/12\/debug-post-dumped.jpg\" alt=\"\" class=\"wp-image-1088\" srcset=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2019\/12\/debug-post-dumped.jpg 590w, https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2019\/12\/debug-post-dumped-480x228.jpg 480w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><figcaption>Dumping Post&#8217;s variable in Timber<\/figcaption><\/figure>\n\n\n\n<p>After taking a quick look, there doesn&#8217;t seem to be any data about the author&#8217;s name. The closest thing is <code>post_author<\/code> which only shows Author ID.<\/p>\n\n\n\n<p>But don&#8217;t stop there, <strong>try checking the methods<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">{{ post | methods }}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"590\" height=\"237\" src=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2019\/12\/debug-post-methods.jpg\" alt=\"\" class=\"wp-image-1086\" srcset=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2019\/12\/debug-post-methods.jpg 590w, https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2019\/12\/debug-post-methods-480x193.jpg 480w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><figcaption>Dumping Post&#8217;s methods in Timber<\/figcaption><\/figure>\n\n\n\n<p>Nice! We found <code>author<\/code> method, let&#8217;s try to see <strong>what&#8217;s inside that<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">{{ post.author | dump }}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"590\" height=\"251\" src=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2019\/12\/debug-author-dump.jpg\" alt=\"\" class=\"wp-image-1083\" srcset=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2019\/12\/debug-author-dump.jpg 590w, https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2019\/12\/debug-author-dump-480x204.jpg 480w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><figcaption>Dumping Post Author&#8217;s variable in Timber<\/figcaption><\/figure>\n\n\n\n<p>There we go! We found the display name, description, and all other data about the author.<\/p>\n\n\n\n<p>Wait for a second, there&#8217;s no avatar data! Check the methods and you will see that it&#8217;s available as <code>{{ post.avatar }}<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-dots\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>This tip is especially useful if you try to integrate a 3rd party plugin like WooCommerce with Timber.<\/p>\n\n\n\n<p><strong>Always Google what you need first<\/strong>. If you can&#8217;t find the information, proceed to dissect the variable with this method. Common WordPress stuff like the example above can actually be found in the official docs.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>As always, let me know in the comment below if you have any question \ud83d\ude42<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>One convenient way to figure out Timber&#8217;s stuff by yourself is to dump the variable and methods. We will show you how in this article.<\/p>\n","protected":false},"author":1,"featured_media":1090,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[31,24],"class_list":["post-1031","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-timber","tag-php","tag-timber"],"blocksy_meta":{"page_structure_type":"default","page_enable_vertical_spacing":"yes","has_hero_section":"default","hero_section":"type-1","hero_alignment1":"left","hero_alignment2":"center","hero_height":"230px","page_title_bg_type":"color","9d41fbfd3f695a106f2ba1a19d0880d2":"","custom_hero_background":{"attachment_id":null},"parallax":{"desktop":false,"tablet":false,"mobile":false},"b6d08a49fa93bc7466f6c62912a4e645":"","single_meta_elements":{"author":true,"date":true,"categories":true,"comments":true,"updated":false,"tags":false},"has_meta_label":"yes","date_format_source":"custom","single_meta_date_format":"M j, Y","53d06589fbfd6f39c905abda16422126":"","page_excerpt_visibility":{"desktop":true,"tablet":true,"mobile":false},"pageTitleFont":{"family":"Default","variation":"Default","size":{"desktop":"32px","tablet":"30px","mobile":"25px"},"line-height":"1.3","letter-spacing":"CT_CSS_SKIP_RULE","text-transform":"CT_CSS_SKIP_RULE","text-decoration":"CT_CSS_SKIP_RULE"},"pageTitleFontColor":{"default":{"color":"var(--paletteColor4)"}},"pageMetaFont":{"family":"Default","variation":"n6","size":{"desktop":"12px","tablet":"12px","mobile":"12px"},"line-height":"1.3","letter-spacing":"CT_CSS_SKIP_RULE","text-transform":"uppercase","text-decoration":"CT_CSS_SKIP_RULE"},"pageMetaFontColor":{"default":{"color":"CT_CSS_SKIP_RULEDEFAULT"},"hover":{"color":"CT_CSS_SKIP_RULEDEFAULT"}},"pageExcerptFont":{"family":"Default","variation":"n5","size":"CT_CSS_SKIP_RULE","line-height":"CT_CSS_SKIP_RULE.65","letter-spacing":"CT_CSS_SKIP_RULE","text-transform":"CT_CSS_SKIP_RULE","text-decoration":"CT_CSS_SKIP_RULE"},"pageExcerptColor":{"default":{"color":"CT_CSS_SKIP_RULEDEFAULT"}},"pageTitleOverlay":{"default":{"color":"rgba(41, 51, 60, 0.2)"}},"pageTitleBackground":{"default":{"color":"#EDEFF2"}},"ee7ac8063825e1bab9079e639f1b5229":"","34ce6fe6895935d020f03dad2e1ba070":"","3e44d97d1de29e93e779af67fe4d624e":"","9956cc6a226f3f3eb2879decc50b4448":"","a0ac92286e201ec861b59ee8f17cdd98":"","1e91e6abad43f4cbcb98b92ba9d650c5":"","2a50e0acb53f0c8a76a2e8653f83b1af":"","c786371072c902ffe89b816688fb6d55":"","d77f352bc6ad5e1269b6dc05f7f65aee":"","e442697534e9a8b4278b5c32c4668bfe":"","a0c83c4441becd669fc6a096cd3a776c":"","125c376d7f5756840a0420d67e00f310":""},"_links":{"self":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/comments?post=1031"}],"version-history":[{"count":10,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1031\/revisions"}],"predecessor-version":[{"id":1583,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1031\/revisions\/1583"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/media\/1090"}],"wp:attachment":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/media?parent=1031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/categories?post=1031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/tags?post=1031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}