Compare name variable against a string value in Drupal 8 twig template - drupal

I'm working with a taxonomy term template page in Drupal 8 and I need to be able to show specific content based solely on the taxonomy term that is populating the content of the page. I'm using the taxonomy-term.twig.html template as a starting point, but I'm not having any luck comparing a string value against the {{ name }} variable. Here's the code I have so far:
<section{{ attributes.addClass(classes)}}>
{% if 'general' in name %}
<img class="promo-image" src="http://placehold.it/150x150" alt="promo">
<h3 class="promo-title">Promo title text</h3>
<p class="promo-copy">lorem ipsum dolor set amet</p>
<div class="links">
Learn more
</div>
{% endif %}
</section>
If I output the name variable like normal {{ name }} it prints the tag name onto the page, but I can't figure out how to compare the value against anything. I've tried a straight if equals route as well, but it seems like the name variable is an array.

Since the name variable is an array, and I only needed the first value I added:
{%
set tag = name['#items'].0.value
%}
Then changed the if statement to:
{% if tag == 'text' %}
<section{{ attributes.addClass(classes)}}>
<img class="promo-image" src="http://placehold.it/150x150" alt="promo">
<h3 class="promo-title">Promo title text</h3>
<p class="promo-copy">lorem ipsum dolor set amet</p>
<div class="links">
Learn more
</div>
</section>
{% endif %}
Now the template is working the way I intended.

Related

Access view fields inside page--%.html.twig

I have a view named "Blog archive". The view displaye title, blurb and image fields. It's a page view and the twig template for the view is named as "page--blog--archive.html.twig". The view content is as follows:
{% if page.content %}
{{ page.content }}
{% endif %}
Thsi prints out the view fields correctly but without any style classes. I want to add the below snippet into the twig template and iterate through view rows:
<div class="grid-4 omega ft-text">
<h3 class="blog-title">{{ fields.title.content }} </h3>
<br/>
<span style="font-family: sans-serif; font-size: 10pt;">
<p>{{ fields.field_blurb_new.content }}</p>
</span>
<h4>{{fields.field_blog_readmore_link.content }}</h4>
</div>
any help on how to achieve this?

Cannot check title's value in twig, Drupal 8

I have the following in a custom text field in a view:
<h5 class="rates_title">{{title}}</h5>
{%if field_car%}
<p class="car">{{field_car}}
{%if title != "Home" %}
<span class="small_caps">car</span>
{%endif%}
</p>
{%else%}
<p class="view-details">View Details</p>
{%endif%}
Everything works except the {%if title != "Home" %} part. Every item gets the span tag, even Home, which I do not want. Yes, the title is Home, and I can confirm that in the <h5> tag.
title at that point is an array and needs to be rendered but then it will contain html tags so you need to search in that rendered string for Home like this
{% if 'Home' in title|render %}
<span class="small_caps">car</span>
{% endif %}

Symfony 3 - Can't get form to be displayed on the view

I'm trying to pass a form along to a view but it wouldn't display and I get an error 500 (page loading is stopped and then, when I try to reach another page, it freezes)
I tried several tutorials including the official symfony documentation and I always get to that same point.
So please help me... (below is my code)
my controller
<?php
namespace siteBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use siteBundle\Form\ContactType;
use siteBundle\Entity\Contact;
class SiteController extends Controller
{
public function navigationAction(string $page,Request $request)
{
$option=array();
if ($page=='contact')
{
$donnees=new Contact();
$form=$this->createForm(ContactType::class,$donnees);
$option['contact']=$form->createView();
}
return $this->render('siteBundle:Pages:'.$page.'.html.twig',$option);
}
}
?>
my view
{% extends 'general.html.twig' %}
{% block description %}blabla{% endblock %}
{% block title %}blabla{% endblock %}
{% block body %}
<div class="container">
<h1>Prendre Contact </h1>
<p class="text-justify">
<br>blabla
</p>
<br>
<div class="row text-center">
<div class="col-md-6">
<p>
<img src="{{ asset('bundles/site/img/telephone.png') }}" alt="téléphone"><br>
<p>01.23.45.67.89</p>
</p>
</div>
<div class="col-md-6">
<p>
<img src="{{ asset('bundles/site/img/email.png') }}" alt="email"><br>
<p>a#b.fr</p>
</p>
</div>
</div>
<br>
<div class="row text-center">
<p>
<img class="text-center" src="{{ asset('bundles/site/img/formulaire.png') }}" alt="Formulaire"><br>
<p>Complétez le formulaire ci-dessous</p>
</p>
</div>
{% if contact is defined %}
<div>
{{ form_start(contact) }}
{{ form_widget(contact) }}
{{ form_end(contact) }}
</div>
{% endif %}
</div> <!-- container -->
{% endblock %}
thanks in advance for your help
Regards
Make sure you use the dev env to display the stack trace of the Exception.
You can use /app_dev.php for that.
my opinion, the error in:
return $this->render('siteBundle:Pages:'.$page.'.html.twig',$option);
use:
return $this->render('SiteBundle:pages:'.$page.'.html.twig',$option);
Check your logs! /var/logs/prod.log or error.log, and you find an error message
In you route change the key word controller by _controller
Maybe this would help :
if i get rid of the array $option on my render line (see code below), there is absolutely no problem and the page loads fine.
return $this->render('siteBundle:Pages:'.$page.'.html.twig');
something is happening when passing the variable to the template...

Timber use different template for custom post type

I am completely new to PHP, Wordpress and Timber. I have a custom post type called Projects, it uses the posts archive template and I am going crazy to create a specific Projects archive template so I can have a different layout for it.
This is what my index.twig looks like
{% extends "layouts/base.twig" %}
{% block content %}
<div class="uk-child-width-1-3#m" uk-grid uk-scrollspy="cls: uk-animation-fade; target: > div > .uk-card; delay: 500; repeat: false">
{% for post in posts %}
{% include "tease-post.twig" %}
{% endfor %}
</div>
{% endblock %}
and this is the tease-post.twig
{% block content %}
<div>
<div class="uk-card uk-card-default">
<div class="uk-card-media-top">
<img src="{{post.thumbnail.src('full')}}" alt="">
</div>
<div class="uk-card-body">
<h3 class="uk-card-title">{{post.title}}</h3>
<p>{{post.get_preview(25,false,false,true)}}</p>
</div>
<div class="uk-card-footer">
Read more
</div>
</div>
</div>
{% endblock %}
Any idea how it works? Can't find any related documentation..
There are a few diff't methods depending on what you're looking to achieve. It would seem the simplest is...
{% extends "layouts/base.twig" %}
{% block content %}
<div class="uk-child-width-1-3#m" uk-grid uk-scrollspy="cls: uk-animation-fade; target: > div > .uk-card; delay: 500; repeat: false">
{% for post in posts %}
{% include "tease-'~ post.type ~'.twig" %}
{% endfor %}
</div>
{% endblock %}
You could then create a file called tease-project.twig (assuming that `project is the name of your custom post type's slug) that might look something like this...
{# tease-project.twig #}
<h2>My cool project is... {{ post.title }}</h2>
If you're looking to do something special with a CPT's specific archive page...
Check your archive.php file, if you're using the starter theme it should load a file called archive-projects.twig (assuming that projects is the name of the custom post type).
The logic is fully customizable so you can load any .twig files you want depending on the circumstance

TB fluid div alignment issue from hyde generated code

I have the following responsive blog archives layout, which is suffering from alignment issues but I'm not sure which element to target to remedy the issue.
In the linked jsFiddle example, adjusting the width does produce a responsive layout, but the listing of blog posts does not flow in proper alignment.
The below code from the hyde static site generator seems to put redundant <p> tags in, which I'm not sure if it needs to be remedied there or if some CSS adjustments will suffice.
{% extends "base.j2" %}
{% from "macros.j2" import render_excerpt with context %}
{% block main %}
{% block page_title %}
<h1 class="title"></h1>
<div class="page-header">
<h1>{{ resource.meta.title }}</h1>
</div>
{% endblock %}
<div class="row-fluid">
{% for res in resource.node.walk_resources_sorted_by_time() %}
{% refer to res.url as post %}
<div class="span4">
<h2>{{ res.meta.title }}</h2>
{{ post.image|markdown|typogrify }}
{{ res.meta.excerpt|markdown|typogrify }}
<p><a class="btn" href="{{ content_url(res.url) }}">View details »</a></p>
</div><!--/span-->
{% endfor %}
</div><!--/row-->
{% endblock %}
If you want to build a grid is best that every 3 span4 you generate a new row-fluid below the other. For example:
<div class="row-fluid">
<div class="span12"> //These two row are our flexible container
/ GENERATE WITH YOUR CODE THIS STUCTURE UNDER EVERY 3 POST /
<div class="row-fluid">
<div class="span4"></div>
<div class="span4"></div>
<div class="span4"></div>
</div>
I use the php for doing it, but I think that is a small error of logical structuration of your html in the cycle!

Resources