{"id":767,"date":"2019-11-21T21:22:04","date_gmt":"2019-11-21T14:22:04","guid":{"rendered":"https:\/\/lab.wptips.dev\/?p=767"},"modified":"2020-06-14T20:29:57","modified_gmt":"2020-06-14T13:29:57","slug":"what-is-timber-library","status":"publish","type":"post","link":"https:\/\/pixelstudio.id\/blog\/what-is-timber-library\/","title":{"rendered":"What is Timber Library?"},"content":{"rendered":"\n<p>Timber is a plugin to <strong>separate PHP and HTML code<\/strong>. It makes your code cleaner and more maintainable. A great tool if you want to create a theme from scratch.<\/p>\n\n\n\n<p>Let&#8217;s take a look at Timber in action. Here we have a <strong>standard posts loop<\/strong>:<\/p>\n\n\n\n<pre title=\"index.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>\n  \n&lt;article>\n  &lt;h2>\n    &lt;a href=\"&lt;?php the_permalink(); ?>\">\n      &lt;?php the_title(); ?>\n    &lt;\/a>\n  &lt;h2>\n  &lt;p>\n    &lt;?php the_excerpt(); ?>\n  &lt;\/p>\n  &lt;time>\n    Written by &lt;?php the_author_posts_link(); ?> on &lt;?php the_time('F jS, Y'); ?>\n  &lt;\/time>\n&lt;\/article>\n\n&lt;?php endwhile; else: ?>\n  &lt;h2> Sorry, no post found &lt;\/h2>\n&lt;?php endif; ?><\/code><\/pre>\n\n\n\n<p>With Timber, we split that into two: (1) <strong>PHP file<\/strong> for data processing and (2) <strong>Twig file<\/strong> for HTML templating:<\/p>\n\n\n\n<pre title=\"index.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n$context = Timber::context();\n$context['posts'] = Timber::get_posts();\n\nTimber::render( 'index.twig', $context );<\/code><\/pre>\n\n\n\n<pre title=\"views\/index.twig\" class=\"wp-block-code language-twig\"><code lang=\"twig\" class=\"language-twig\">...\n\n{% for p in posts %}\n&lt;article>\n  &lt;h2>\n    &lt;a href=\"{{ p.link }}\">\n      {{ p.title }}\n    &lt;\/a>\n  &lt;h2>\n  &lt;p>\n    {{ p.post_excerpt }}\n  &lt;\/p>\n  &lt;time>\n    Written by {{ p.author.display_name }} on {{ p.post_date | date }}\n  &lt;\/time>\n&lt;\/article>\n\n{% else %}\n  &lt;h2> Sorry, no post found &lt;\/h2>\n{% endfor %}\n\n...<\/code><\/pre>\n\n\n\n<p>All values that is passed in when calling <code>Timber::render()<\/code> will be available to use in the Twig file, in the case above: <code>posts<\/code> data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Twig Syntax<\/h2>\n\n\n\n<p>Below is a brief introduction to Twig language. Visit the <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/twig.symfony.com\/\" target=\"_blank\">official docs<\/a> for more info.<\/p>\n\n\n\n<p><strong>ECHO VARIABLE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"twig\" class=\"language-twig\">{{ var_name }}<\/code><\/pre>\n\n\n\n<p><strong>COMMENT<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"twig\" class=\"language-twig\">{# Use curly brace with hashtag #}<\/code><\/pre>\n\n\n\n<p><strong>FOR LOOP<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"twig\" class=\"language-twig\">{% for p in posts %}\n  ...\n{% endfor %}<\/code><\/pre>\n\n\n\n<p><strong>CONDITIONAL<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"twig\" class=\"language-twig\">{% if var == \"...\" %}\n  ...\n{% elseif var >= 10 %}\n  ...\n{% else %}\n  ...\n{% endif %}<\/code><\/pre>\n\n\n\n<p><strong>FILTER<\/strong> &#8211; Run a function by passing in the variable<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"twig\" class=\"language-twig\">{{ p.post_date | date }}  {# Format the date #}\n\n{{ p.my_custom_field | shortcodes }}  {# Run the shortcode #}\n\n{{ my_array | length }}  {# count the array #}\n\n...<\/code><\/pre>\n\n\n\n<p><strong>EXTENDS &amp; BLOCK<\/strong> &#8211; Extend is to inherit another template while Block is to override certain part.<\/p>\n\n\n\n<pre title=\"views\/layout.twig\" class=\"wp-block-code\"><code lang=\"twig\" class=\"language-twig\">&lt;nav class=\"top-nav\"> ... &lt;\/nav>\n\n{% block content %}\n{% endblock %}\n\n&lt;footer class=\"main-footer\"> ... &lt;\/footer><\/code><\/pre>\n\n\n\n<pre title=\"views\/page.twig\" class=\"wp-block-code\"><code lang=\"twig\" class=\"language-twig\">{% extends \"layout.twig\" %}\n\n{# Override the block content in layout.twig #}\n{% block content %}\n\n&lt;article>\n  &lt;h1> {{ post.title }} &lt;\/h1>\n  {{ post.content }}\n&lt;\/article>\n\n{% endblock %}<\/code><\/pre>\n\n\n\n<p>You will learn a lot more about Timber by taking a look at <a rel=\"noreferrer noopener\" aria-label=\"our starter theme (opens in a new tab)\" href=\"https:\/\/github.com\/hrsetyono\/timber-basic-theme\" target=\"_blank\">our basic theme<\/a>. That theme has no styling and lack features. It is only intended for learning purposes.<\/p>\n\n\n\n<p>If you are looking for fully-featured theme, try the <a href=\"https:\/\/github.com\/timber\/starter-theme\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"official starter theme (opens in a new tab)\">official starter theme<\/a>.<\/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>Timber makes developing a custom theme much easier and manageable. It applies the core idea of MVC framework which is to separate logic and interface.<\/p>\n\n\n\n<p>If you are interested to learn more about Timber, check out  the following:<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/github.com\/hrsetyono\/timber-basic-theme\">Basic Theme<\/a> &#8211; Plain Timber theme intended for teaching you the rope about Timber.<\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/github.com\/timber\/starter-theme\" target=\"_blank\">Starter Theme<\/a> &#8211; Fully-featured theme built with Timber.<\/li><li><a href=\"https:\/\/www.upstatement.com\/timber\/\">Timber Official Docs<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\"Twig Language Docs (opens in a new tab)\" href=\"https:\/\/twig.symfony.com\/\" target=\"_blank\">Twig Language Docs<\/a><\/li><\/ul>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Feel free to ask question regarding Timber in the comment below \ud83d\ude42<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Timber is a plugin to separate PHP and HTML code. It makes your code cleaner and more maintainable. A great tool if you want to create a theme from scratch. Let&#8217;s take a look at Timber in action. Here we have a standard posts loop: With Timber, we split that into two: (1) PHP file for data processing and (2) Twig file for HTML templating: All values that is passed in when calling Timber::render() will be available to use in the Twig file, in the case above: posts data. Twig Syntax Below is a brief introduction to Twig language. Visit\u2026<\/p>\n","protected":false},"author":1,"featured_media":786,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[31,24,37],"class_list":["post-767","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-timber","tag-php","tag-timber","tag-twig"],"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","bed0a20ecf110c23936e9e0c3ccf330c":"","custom_hero_background":{"attachment_id":null},"parallax":{"desktop":false,"tablet":false,"mobile":false},"single_meta_elements":{"author":true,"date":true,"categories":true,"comments":true,"updated":false,"tags":false},"has_meta_label":"yes","single_meta_date_format":"M j, Y","page_excerpt_visibility":{"desktop":true,"tablet":true,"mobile":false},"pageTitleFont":{"family":"Default","variation":"n7","size":{"desktop":"32px","tablet":"30px","mobile":"25px"},"line-height":"1.3","letter-spacing":"0em","text-transform":"none","text-decoration":"none"},"pageTitleFontColor":{"default":{"color":"var(--paletteColor4)"}},"pageMetaFont":{"family":"Default","variation":"n6","size":{"desktop":"12px","tablet":"12px","mobile":"12px"},"line-height":"1.3","letter-spacing":"0em","text-transform":"uppercase","text-decoration":"none"},"pageMetaFontColor":{"default":{"color":"CT_CSS_SKIP_RULEDEFAULT"},"hover":{"color":"CT_CSS_SKIP_RULEDEFAULT"}},"pageExcerptFont":{"family":"Default","variation":"n5","size":"17px","line-height":"1.65","letter-spacing":"0em","text-transform":"none","text-decoration":"none"},"pageExcerptColor":{"default":{"color":"CT_CSS_SKIP_RULEDEFAULT"}},"pageTitleOverlay":{"default":{"color":"rgba(41, 51, 60, 0.2)"}},"pageTitleBackground":{"default":{"color":"#EDEFF2"}},"c2a013c61b8ebba0d3592f393a4c6500":"","2c78be59e981107ec9bf61bf729e3c4e":"","date_format_source":"custom","51754def25c823443561e3c1f4d9c0ea":"","da942bcd3cfc6f2f0d2a1c286ce8224d":"","66284c28cecca4a1b7bfa9d3c3ba5c80":"","ffe508b46b58cd25d9992fbef2ada163":"","9671bb3644fabc3ab661a163096f466d":"","7e3309aa52a6e25b45a72e3b6d6fa3dd":"","50934647e86ecf567fd64cc401150cd1":""},"_links":{"self":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/767","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=767"}],"version-history":[{"count":9,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/767\/revisions"}],"predecessor-version":[{"id":1026,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/767\/revisions\/1026"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/media\/786"}],"wp:attachment":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/media?parent=767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/categories?post=767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/tags?post=767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}