-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b615e47
commit 77a39f3
Showing
7 changed files
with
258 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
|
||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<meta name="description" content="Drupal and travelling"> | ||
|
||
<title>Twig inside Views in Drupal 8 - Miro Michalicka</title> | ||
|
||
<link rel="canonical" href="http://yourdomain.com/2015/12/07/views-twig/"> | ||
|
||
<!-- Bootstrap Core CSS --> | ||
<link rel="stylesheet" href="/css/bootstrap.min.css"> | ||
|
||
<!-- Custom CSS --> | ||
<link rel="stylesheet" href="/css/clean-blog.css"> | ||
|
||
<!-- Pygments Github CSS --> | ||
<link rel="stylesheet" href="/css/syntax.css"> | ||
|
||
<!-- Custom Fonts --> | ||
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"> | ||
<link href='//fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'> | ||
<link href='//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'> | ||
|
||
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --> | ||
<!-- WARNING: Respond.js doesn't work if you view the page via file:// --> | ||
<!--[if lt IE 9]> | ||
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> | ||
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script> | ||
<![endif]--> | ||
|
||
</head> | ||
|
||
|
||
<body> | ||
|
||
<!-- Navigation --> | ||
<nav class="navbar navbar-default navbar-custom navbar-fixed-top"> | ||
<div class="container-fluid"> | ||
<!-- Brand and toggle get grouped for better mobile display --> | ||
<div class="navbar-header page-scroll"> | ||
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> | ||
<span class="sr-only">Toggle navigation</span> | ||
<span class="icon-bar"></span> | ||
<span class="icon-bar"></span> | ||
<span class="icon-bar"></span> | ||
</button> | ||
<a class="navbar-brand" href="/">Miro Michalicka</a> | ||
</div> | ||
</div> | ||
<!-- /.container --> | ||
</nav> | ||
|
||
|
||
<!-- Post Header --> | ||
<header class="intro-header" style="background-image: url('/img/post-bg-07.png')"> | ||
<div class="container"> | ||
<div class="row"> | ||
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1"> | ||
<div class="post-heading"> | ||
<h1>Twig inside Views in Drupal 8</h1> | ||
|
||
<h2 class="subheading">frontend for sitebuilders</h2> | ||
|
||
<span class="meta">Posted on December 7, 2015</span> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</header> | ||
|
||
<!-- Post Content --> | ||
<article> | ||
<div class="container"> | ||
<div class="row"> | ||
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1"> | ||
|
||
<p>Drupal 8 brings us except Symfony in backend also Twig for frontenders and Views for sitebuilders. Have you every thought about mixing this two parts of Drupal? In most of cases it is better if frontender knows at least basics of Drupal sitebuilding and Views are one of the most important modules for this.</p> | ||
|
||
<h3 id="twig">Twig</h3> | ||
<p>Twig is templating engine which replaced PHPTemplates in Drupal 8. It uses some own functions, filters and syntax which aren’t related to PHP or Javascript and more importantly, it protects you from complexity of Drupal data model. No more thinking if variable is array or object - you access its children the same way. These functions and filters, which modify way how are input variables displayed. Drupal also extends Twig abilities by adding some own functions, mostly related to manipulation with html attributes. More about Twig should be found on <a href="http://twig.sensiolabs.org/">this</a> page and in Drupal <a href="https://www.drupal.org/list-changes">change records</a>.</p> | ||
|
||
<h3 id="views">Views</h3> | ||
<p>Views is module, which knows probably everyone in Drupal world and honestly I cannot imagine building even simple presentation website without it. It offers you ability to build complex queries and display their results without having any knowledge about databases just through Drupal UI.</p> | ||
|
||
<h3 id="using-twig-functions-filters-in-views">Using Twig functions filters in Views</h3> | ||
<p><strong>Usecase:</strong> You want to display some text all uppercase and you don’t want to do it in CSS.</p> | ||
|
||
<p><strong>Drupal 7 solution:</strong> You can create field template with correct name, so it will have an effect only on desired field and inside this field template you will use <code>strtoupper</code> PHP function.</p> | ||
|
||
<p><strong>Drupal 8 solution:</strong> You will rewrite field with its own value and use <code>upper</code> Twig filter. It will look this way: | ||
<img src="/img/twig1.png" alt="upper Twig filter" /></p> | ||
|
||
<h3 id="more-advanced-twig-expressions">More advanced Twig expressions</h3> | ||
<p><strong>Usecase:</strong> Show content of field conditionally or conditionally show content one of two fields.</p> | ||
|
||
<p><strong>Drupal 7 solution:</strong> Preprocessing or some naughty things in templates.</p> | ||
|
||
<p><strong>Drupal 8 solution:</strong> Use Twig expressions :) | ||
<img src="/img/twig2.png" alt="upper Twig filter" /></p> | ||
|
||
<figure class="highlight"><pre><code class="language-html" data-lang="html"><span class="lineno">1</span> {% if field_display_text2=='True' %} | ||
<span class="lineno">2</span> {{ field_text2 }} | ||
<span class="lineno">3</span> {% else %} | ||
<span class="lineno">4</span> {{ field_text1 }} | ||
<span class="lineno">5</span> {% endif %}</code></pre></figure> | ||
|
||
<h3 id="conclusion">Conclusion</h3> | ||
<p>Twig makes Frontender’s life easier. I’m not saying that we should do everything possible only through UI, as I see there many risks related to harder debugging. Practical use on projects will show us the best way. </p> | ||
|
||
|
||
<hr> | ||
|
||
<ul class="pager"> | ||
|
||
<li class="previous"> | ||
<a href="/2015/11/06/configuration-d8/" data-toggle="tooltip" data-placement="top" title="Configuration management in Drupal 8">← Previous Post</a> | ||
</li> | ||
|
||
|
||
</ul> | ||
|
||
</div> | ||
</div> | ||
</div> | ||
</article> | ||
|
||
<hr> | ||
|
||
|
||
<!-- Footer --> | ||
<footer> | ||
<div class="container"> | ||
<div class="row"> | ||
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1"> | ||
<ul class="list-inline text-center"> | ||
<li> | ||
<a href="/feed.xml"> | ||
<span class="fa-stack fa-lg"> | ||
<i class="fa fa-circle fa-stack-2x"></i> | ||
<i class="fa fa-rss fa-stack-1x fa-inverse"></i> | ||
</span> | ||
</a> | ||
</li> | ||
|
||
<li> | ||
<a href="https://twitter.com/miromichalicka"> | ||
<span class="fa-stack fa-lg"> | ||
<i class="fa fa-circle fa-stack-2x"></i> | ||
<i class="fa fa-twitter fa-stack-1x fa-inverse"></i> | ||
</span> | ||
</a> | ||
</li> | ||
|
||
|
||
|
||
<li> | ||
<a href="https://github.com/miromichalicka"> | ||
<span class="fa-stack fa-lg"> | ||
<i class="fa fa-circle fa-stack-2x"></i> | ||
<i class="fa fa-github fa-stack-1x fa-inverse"></i> | ||
</span> | ||
</a> | ||
</li> | ||
|
||
<li> | ||
<a href="https://sk.linkedin.com/in/michalicka"> | ||
<span class="fa-stack fa-lg"> | ||
<i class="fa fa-circle fa-stack-2x"></i> | ||
<i class="fa fa-linkedin fa-stack-1x fa-inverse"></i> | ||
</span> | ||
</a> | ||
</li> | ||
</ul> | ||
</div> | ||
</div> | ||
</div> | ||
</footer> | ||
|
||
<!-- jQuery --> | ||
<script src="/js/jquery.min.js "></script> | ||
|
||
<!-- Bootstrap Core JavaScript --> | ||
<script src="/js/bootstrap.min.js "></script> | ||
|
||
<!-- Custom Theme JavaScript --> | ||
<script src="/js/clean-blog.min.js "></script> | ||
|
||
|
||
</body> | ||
|
||
</html> |
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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