swig to hbs for search results - handlebars.js

Im relatively new to handlebars but am using it for a current project.
I need to show search results which I would usually show like this in swig:
{% for loan in data.loans %}
{% include 'loan.swig' %}
{% endfor %}
but that obviously won't work in hbs.
Can someone point me in the right direction as to what I should be putting?
Much appreciated.

Related

Sensiolabs Insight : Twig templates should not contain business logic

I am actually running Sensiolabs Insight analysis on my Symfony 2.8 project.
I have a major issue with some of my Twig templates:
Twig templates should not contain business logic
The associated message is always the same :
Template too complex, depth of 10 is reached but only 5 is allowed.
For example this happens with the following template :
{% extends "FBNGuideBundle::layout.html.twig" %}
{% block title %}
{{ 'fbn.guide.page_title.bookmarks'|trans }}
{% endblock %}
{% block body %}
<div id="bookmarks" data-bookmark-ids="{{bookmarkIds|json_encode()}}">
{% if (restaurants|length > 0) %}
<div class="restaurants">
<h3>MES RESTOS</h3>
{% for bookmark in restaurants %}
<div class="bookmark" id="{{'bookmark-' ~ bookmark.id}}">
{{ bookmark.restaurant.name }}
<br>
<br>
<button>SUPPRIMER DES FAVORIS</button>
<br>
<hr>
</div>
{% endfor %}
</div>
{% endif %}
</div>
{% endblock %}
I tried to include in a separated file the code contained inside <div id="bookmarks"></div> and the depth has been reduced, but it is not a solution. I suppose that the problem is the access to some properties through several objects using getters (i.e bookmark.restaurant.slug).
I have a free plan so I am not able to access the documentation related to this warning. Anyone knows how to solve the problem ?
Thanks.
When you have too much logic in the view, you can put it in a custom Twig Extension. An advantage is that you don't need to duplicate the html if you are reusing that part in another page and of course, the code is more clear :)
In your case, you can write a new Twig Extension that renders all the bookmarks.
If you didn't build somethng similar till now, you can read about it here http://symfony.com/doc/current/templating/twig_extension.html

Drupal 8 conditional syntax

I'm looking for the proper syntax with a Drupal 8 conditional display of a link. I've been reading the twig documentation, and it's a significant jump from the PHP templating days so it's slow going. I need to conditionally display a link within the footer of a view.
In the past, this would be straightforward, i.e:
<?php global $user;?>
<?php if (user_access('administer nodes')):?>
<div class="foo">Shorthand link for admins</div>
<?php endif;?>
Getting this to work in Twig has been difficult as I'm unfamiliar with the syntax. Do I need to declare global user in some capacity? From this link here, Symfony 2: How do I check if a user is not logged in inside a template?, it seems that all I'd need to do is:
{% if is_granted('ROLE_ADMIN') -%}
<div class="foo">Shorthand link for admins</div>
{% endif %}
But I get an error when trying to submit that. Is ROLE_ADMIN not defined? How do I get the Symphony? (correct?) roles as defined within the D8 installation? I'm not sure what I'm doing wrong. Any assistance is appreciated.
If you need to check a specific permission try
{% if user.hasPermission('administer nodes') %}
<div class="foo">Shorthand link for admins</div>
{% endif %}
If you try to check the current user is administer in TWIG file , you can use
{% if is_admin %}
<div class="foo">Shorthand link for admins</div>
{% endif %}

Twig and atomic pattern — Clean twig rendering

I'm trying to follow the atomic design pattern with twig.
When rendering a simple atom, I need to do something like:
{% include '#MyBundle/Resources/views/atoms/button/button.html.twig' with { href: '/section1', text: 'Example text' } only %}
This approach starts getting messy when the atom or component has more variables, or the directory structure is a bit more complex.
I'd be awesome to be able to do something like:
{% button('/section1','Example text') %}
I know that this can be achieved with a twig function, but I'm worried this pattern can get tricky with a larger code base.
Any experience around this? Cheers!
You can use macro structure. Read documentation: http://twig.sensiolabs.org/doc/tags/macro.html
{% macro button(href, text) %}
{% here you can place your template %}
{% endmacro %}
Then you will need only import your twig file with macro once. After that you can use construction like {% button('/section1','Example text') %}.

Drupal 8 include part template

I'm trying to use Drupal 8, with an own theme, due big structure differences for my requirements I have a page--front.twig.html and a page.twig.html, I would like to create template parts as used in phrozn oder in a normal Symfony2 project, for example a footer.html.twig and a header.html.twig. These templates are saved under a subdirectory /parts/
But wenn I call this templates as normal I just receive a string with the name of the template.
For example:
{# in page.html.twig or page--front.html.twig #}
{% include 'parts/footer.html.twig' %}
Returns the file name as string:
parts/footer.html.twig
It's possible to do that with Drupal 8?
You can include any part of your template files like this
{% include directory ~ '/parts/footer.html.twig' %}
or this
{% include '#mytheme/parts/footer.html.twig' %}
I strongly recommend you to create a reusable layout for pages that will give you greater flexibility when dealing with more pages and variants.
{# filename: page-layout.html.twig #}
{% block content%}
{{ page.content }}
{% endblock%}
{% block footer%}
{% include '#mytheme/parts/footer.html.twig' %}
{% endblock%}
So you can do something like this in another page
{# filename: page--front.html.twig #}
{% block footer%}
<div> I want to handle a different footer in here</div>
{% endblock%}
Finally, I found really helpful to dig into suggestions array and see what Drupal is trying to use.
Cheers.
it's possible using the name of the template in the path
{% include '#mytheme/parts/footer.html.twig' %}
thanks to https://drupal.stackexchange.com/questions/141066/drupal-8-include-part-template
Now that https://www.drupal.org/node/2291449 has been committed you can also do:
{% include 'footer.html.twig' %}

FOSUserBundle changing text on page depending on form

I'm very new to symfony2 and twig templating in general.
My question is related to the FOSUserBundle.
I have a base template which has 2 columns, {% block left %} and {% block right %}
I have created a new file: Base5/UserBundle/Resources/views/layout.html.twig
which extends my 2 column layout
{% extends '::base_2col.html.twig' %}
{% block left %}
I want this text to change depending on wether a login form, register form, profile page etc is displayed
{% endblock %}
{% block right %}
{{ block('fos_user_content') }}
{% endblock %}
as the actual form is rendered using {{ block('fos_user_content') }}, how can i change the text in the right panel depending on what form is displayed, ideally i want to use a different include, containing the various descriptions into texts for the diffirent form.
Any pointers would be very much appreciated.
Found out, just a question of duplicating the templates from the fosuserbundle views directory

Resources