Last active
May 31, 2021 18:50
-
-
Save pommiegranit/d57d16ef5adc6005565d to your computer and use it in GitHub Desktop.
Timber + Twig Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% import 'twig/macros.twig' as macros %} | |
{{ function( 'get_header' ) }} | |
<div id="primary" class="content-area"> | |
<div id="main" class="site-main" role="main"> | |
{% block content %}{% endblock %} | |
</div><!-- #main --> | |
</div><!-- #primary --> | |
{{ function( 'get_sidebar' ) }} | |
{{ function( 'get_footer' ) }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The main template file. | |
*/ | |
get_header(); ?> | |
<div id="primary" class="content-area"> | |
<main id="main" class="site-main" role="main"> | |
<?php if ( have_posts() ) : ?> | |
<?php /* Start the Loop */ ?> | |
<?php while ( have_posts() ) : the_post(); ?> | |
<?php | |
/* Include the Post-Format-specific template for the content. | |
* If you want to override this in a child theme, then include a file | |
* called content-___.php (where ___ is the Post Format name) and that will be used instead. | |
*/ | |
get_template_part( 'content', get_post_format() ); | |
?> | |
<?php endwhile; ?> | |
<?php bosco_paging_nav(); ?> | |
<?php else : ?> | |
<?php get_template_part( 'content', 'none' ); ?> | |
<?php endif; ?> | |
</main><!-- #main --> | |
</div><!-- #primary --> | |
<?php get_sidebar(); ?> | |
<?php get_footer(); ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The main template file. | |
*/ | |
// set up page data | |
$data = Timber::get_context(); | |
$data['posts'] = Timber::get_posts(); | |
$data['page'] = 'home'; | |
// render using Twig template index.twig | |
Timber::render('twig/index.twig', $data ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<article id="post-{{ post.ID }}>" {{ function('post_class') }}> | |
<header class="entry-header"> | |
{% if page == 'single' %} | |
<h1 class="entry-title">{{ post.post_title }}</h1> | |
{% else %} | |
<h1 class="entry-title"><a href="{{ post.permalink }}" rel="bookmark">{{ post.post_title }}</a></h1> | |
{% endif %} | |
</header><!-- .entry-header --> | |
{% if post.thumbnail %} | |
<div class="entry-thumbnail"> | |
<img src="{{ post.thumbnail.src }}" > | |
</div><!-- .entry-thumbnail --> | |
{% endif %} | |
{% if page in [ 'search' , 'index' ] %} | |
<div class="entry-summary"> | |
{{ post.post_excerpt }} | |
</div><!-- .entry-summary --> | |
{% else %} | |
<div class="entry-content"> | |
{{ post.content }} | |
</div><!-- .entry-content --> | |
{% endif %} | |
<footer class="entry-meta"> | |
{% if post.post_type == 'post' %} | |
{{ macros.post_meta( post ) }} | |
{% endif %} | |
{% if page == 'single' %} | |
{{ macros.post_terms( post ) }} | |
{% endif %} | |
</footer><!-- .entry-meta --> | |
</article><!-- #post-## --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'twig/base.twig' %} | |
{% block content %} | |
<!-- start the loop --> | |
{% for post in posts %} | |
{{ include( 'twig/content.twig', ignore_missing = true ) }} | |
{% else %} | |
{{ include( 'twig/content-none.twig' ) }} | |
{% endfor %} | |
{% if posts %}{{ function( 'bosco_paging_nav' ) }}{% endif %} | |
{% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% macro post_meta( post ) %} | |
<span class="posted-on">Posted on <time class="entry-date published">{{ post.post_date|date('F j, Y') }}</time></a></span> | |
<span class="byline"> by <span class="author vcard"><a class="url fn n" href="{{ post.author.link }}">{{ post.author.name }}</a></span></span> | |
{% if post.can_edit %} | |
<span class="edit-link"><a class="post-edit-link" href="{{ post.edit_link }}">Edit</a></span> | |
{% endif %} | |
{% endmacro %} | |
{% macro post_terms( post ) %} | |
<div class="categories-tags"> | |
{% if post.terms('category') %} | |
<span class="cat-links"> | |
Posted in | |
{% for term in post.terms('category') %} | |
<a href="{{ term.link }}" title="View all posts in {{ term.name }}" rel="category tag">{{ term.name }}</a>{% if loop.last == false %},{% endif %} | |
{% endfor %} | |
</span> | |
{% endif %} | |
{% if post.terms('tags') %} | |
<span class="tags-links"> | |
Tagged | |
{% for term in post.terms('tags') %} | |
<a href="{{ term.link }}" title="View all posts tagged {{ term.name }}" rel="category tag">{{ term.name }}</a>{% if loop.last == false %},{% endif %} | |
{% endfor %} | |
</span> | |
{% endif %} | |
</div> | |
{% endmacro %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>{{ title }}</title> | |
</head> | |
<body> | |
<article> | |
<header> | |
<h1>{{ title }}</h1> | |
</header> | |
<div id="content" role="main"> | |
{{ content|raw }} | |
</div> | |
<footer> | |
<ul> | |
<li>By {{ author.name }}</li> | |
<li>On {{ date|date("F j, Y") }}</li> | |
<li>In | |
{% for term in terms.category %} | |
<a class="category-link" href="{{ link }}">{{ term.name }}</a> | |
{% endfor %} | |
</footer> | |
</article> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$json = '{ | |
"ID": 1, | |
"title": "Hello World!", | |
"author": | |
{ | |
"name": "Chris Knowles", | |
}, | |
"content": "<p>The ubiquitous post.</p><p>Need I say more?</p>", | |
"date": "2013-03-15T15:15:12+00:00", | |
"terms": | |
{ | |
"category": | |
[ | |
{ | |
"name": "Cat One", | |
"link": "http://www.test.dev/category/cat-one/" | |
}, | |
{ | |
"name": "Cat Two", | |
"link": "http://www.test.dev/category/cat-two/" | |
}, | |
{ | |
"name": "Cat Three", | |
"link": "http://www.test.dev/category/cat-three/" | |
} | |
] | |
} | |
}'; | |
// create the data from a JSON object | |
$data = json_decode($json, true); | |
// load Twig | |
require_once 'lib/Twig/Autoloader.php'; | |
Twig_Autoloader::register(); | |
$loader = new Twig_Loader_Filesystem('templates'); | |
$twig = new Twig_Environment($loader, array( | |
'cache' => 'cache', | |
)); | |
// render the template | |
echo $twig->render('index.html', $obj); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The main template file. | |
*/ | |
// set up page data | |
if ( is_singular() ) : | |
$data = Timber::get_context(); | |
$data['post'] = new TimberPost(); | |
else : | |
$data = Timber::get_context(); | |
$data['posts'] = Timber::get_posts(); | |
endif; | |
if ( is_single() ) : | |
$data['page'] = 'single'; | |
$template = 'single'; | |
elseif ( is_page() ) : | |
$data['page'] = 'page'; | |
$template = 'page'; | |
elseif ( is_home() ) : | |
$data['page'] = 'home'; | |
$template = 'index'; | |
elseif ( is_category() ) : | |
$data['archive_title'] = get_cat_name( get_query_var('cat') ); | |
$data['archive_description'] = term_description(); | |
$data['page'] = 'archive'; | |
$template = 'archive'; | |
elseif ( is_tag() ) : | |
$data['archive_title'] = get_term_name( get_query_var('tag_id') ); | |
$data['archive_description'] = term_description(); | |
$data['page'] = 'archive'; | |
$template = 'archive'; | |
elseif ( is_author() ) : | |
$data['archive_title'] = get_the_author(); | |
$data['page'] = 'archive'; | |
$template = 'archive'; | |
endif; | |
// render using Twig template index.twig | |
Timber::render('twig/' . $template . '.twig', $data ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment