Display twig code written in a twig file - php

I am writting a tutorial and need to display some twig code... Problem: I'm writting that twig code on a twig view, so I need to find some way to write it raw, without interpretation.
Currently, I am using a twig extension to do the job:
$path = $this->kernel->locateResource($path, null, true);
return file_get_contents($path);
But I'm wondering if there is better solutions.

Found it. Twig has a built-in tag to to that job : verbatim.
{% verbatim %}
<ul>
{% for item in seq %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endverbatim %}

Related

Twig posting empty modules on page

I'm working with Twig in Craft and am trying to include a module I've made, I have added all the content into the modules in the CMS and saved it, they show on the page correctly, but a tonne of empty paragraph tags show also, does anyone know why this is?
Steps I took:
I have a twig file called "Abilities.twig", inside abilities I have the below code:
<p>{{ module.Abilities }}</p>
In Index.twig I have the below:
{% for module in entry.modals %}
{% include '_modals/Abilities' %}
{% endfor %}
An example below of how they show on the page
<p>This is the first example of an ability</p>
<p>This is the second example of an ability</p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
In the CMS the only modules that are shown are the top 2 examples, there are no empty modules saved.
If anyone can help would appreciate it
If you don't want to have the "extra" <p>'s you need to verify the content is not empty
{% if module.Abilities | trim != '' %}
<p>{{ module.Abilities }}</p>
{% endif %}
You can add an if statement in you loop
{% for module in entry.modals if module != null %}
{% include '_modals/Abilities' %}
{% endfor %}

Applying twig filters in Drupal 8 Views Template

I'm building a themed view in Drupal 8 using twig. I'd like to use twig filters on the row.content variable in the views-view-unformatted.html.twig template.
It seems that row.content is an array so twig's string manipulation doesn't work. However, it prints onto the page as a string (view is just a list of taxonomy terms).
What I'd like to do is slugify the output so taxonomy terms with spaces can have valid hrefs. See the replace filter in the code below.
<ul class="nav nav-tabs" role="tablist">
{% for row in rows %}
<li role="presentation" class="{{loop.first ? 'active' : ''}}">
{{row.content}}
</li>
{% endfor %}
</ul>
This will just output <a href="#">. Does anyone know how to access the raw text value that is output during twigs interpolation?
Thanks!
I ran into this myself it was difficult because kint and dump crashes on views.
there is a quick workaround to get to the bits though, put this under {% for row in rows %} in your twig views style template.
<ol>
{% for key, value in row.content %}
<li>{{ key }}</li>
{% endfor %}
</ol>
load the page with that & gives you the keys to see, I checked them each with the following dump command, just added underneath to test.
{{ dump(row.content['#row']) }}
The above dump showed all the goods in #row, where I dug in and found the field I wanted inside _entity (may be different for you), then I wanted to replace spaces with dash and force lowercase.
Everything past row.content['#row'] is likely different for you, you'll need to dig in the array a bit with the dump command mentioned above.
Below is the line that got me what I wanted.
{{ row.content['#row']._entity.title[0].value|replace(' ', '-')|lower }}
Below is twig template example.
For filename change viewname and block-3 to your setup.
views-view-unformatted--viewname--block-3.html.twig
{% for row in rows %}
{%
set row_classes = [
default_row_class ? 'views-row',
'something',
'kint-cant',
]
%}
{# My field value unformatted #}
<!-- {{ row.content['#row']._entity.title[0].value }} -->
<section{{ row.attributes.addClass(row_classes) }} id="{{ row.content['#row']._entity.title[0].value|replace(' ', '-')|lower }}">
{{ row.content }}
</section>
{% endfor %}
Im sure there are plenty of other ways to do this, but it worked for me as a quick solution to print out views fields in the style template, which is really useful.

symfony2 - how to include a twig from a twig

Learning sympony2 and I have hit a wall and have tried numerous solution but nothing seems to work, I keep getting the
Unable to find template "ScoreBoardViewerBundle.Viewer.scoreboard_keeper.html.twig" in
ScoreBoardViewerBundle:Viewer:view.html.twig at line 15.
Here is my twig snipped:
{% if score_keeper=='sk' %}
{% include 'ScoreBoardViewerBundle.Viewer.scoreboard_keeper.html.twig' with {'score_keeper' : score_keeper} %}
{% else %}
{% include 'ScoreBoardViewerBundle.Viewer.scoreboard.html.twig' %}
{% endif %}
Initially I had only the file name, like I seen on examples from this twig site: Twig site but that produced the same error. The twig files are found in the same directory.
What am I doing wrong? Also I get the same issue regardless which file I try to include. I did have some typos but they should be fixed.
Here is the Controller I was using and it was able to open both views correctly, I just noted some twig in each file was redundant so hence the changes
public function viewAction($score_keeper)
{
//returns scoreboard view for score keeper
// if($score_keeper=="sk"){
// return $this->render('ScoreBoardViewerBundle:Viewer:keeper.html.twig',array('score_keeper' => $score_keeper));
// }
//returns scorboard view for all others
return $this->render('ScoreBoardViewerBundle:Viewer:view.html.twig',array('score_keeper' => $score_keeper));
}
You have a typo in your {% include %} statements. You are supposed to use : instead of . to separate blocks in template name.
Try this:
{% if score_keeper=='sk' %}
{% include 'ScoreBoardViewerBundle:Viewer:scoreboard_keeper.html.twig' with {'score_keeper' : score_keeper} %}
{% else %}
{% include 'ScoreBoardViewerBundle:Viewer:scoreboard.html.twig' %}
{% endif %}

Twig filter included template

I wanted to do something like this:
{{ include("tpl.html")|f }}
But that doesn't seem to work, it just printed tpl.html without any filtering, then I tried:
{% filter f %}
{% include "tpl.html" %}
{% endfilter %}
And it worked. I just wonder, why can't I use shorter one? Do I misunderstand something?
Thanks in advance.
Sorry for being that long to come back :-)
The fact is that the include function writes on the template.
If you do :
{% set s = include('FuzHomeBundle:Default:test.html.twig') %}
Which is not supposed to display something, you'll get the content of the file output anyway, and the s variable will be set to null.
If you do instead :
{% filter upper %}
{% include 'FuzHomeBundle:Default:test.html.twig' %}
{% endfilter %}
or
{% filter upper %}
{{ include('FuzHomeBundle:Default:test.html.twig' }}
{% endfilter %}
The filter tag will compile some code that control output buffer.
To apply a filter on a section of code, you have to wrap it with the filter tag:
{% filter f %}
...
{% endfilter %}
What you were trying originally is to filter a variable which in twig is defined by the double parenthesis:
{{ variable name|filter }}
to read more check out the twig documentation on filters here

Dynamic {% use %} statement in twig?

I want to achieve the following thing:
I have a basic template for all of my pages, named "_page_base.twig". It contains the header and footer.
Then I have a Template for different areas of the page: "topic.twig", "section.twig" and "article.twig" - each of them extending the "_page_base.twig", thats working well so far.
Now I want to write my articles. I would love to save them as .twig file as well, since I can edit the complete markup in my editor and just upload it.
Since I can't say that my article files just extend "article.twig" (multiple inheritance is not possible) I could tell the "article.twig" that it should use blocks from my different article-content twigfiles.
The problem is: "use" statements have to be hardcoded!
My current solution is to add {% use "[PLACEHOLDER]" %} into the "article.twig" and then loading the template into a string, replacing the placeholder to the correct article-content.twig and then passing the whole thing to the template engine. What a mess.
Have you guys any idea for a better solution?
You can try with include tag. Since this tag accepts a dynamic name template to include, you just only need to define a variable which contains the name of article twig template.
{# on article.twig#}
{% set articles = ['someArticle.twig', ...] %}
{% for article in articles %}
{% include article %}
{% endfor %}
{# on someArticle.twig #}
... Article text ...
In the case of you need to customize some content inside of someArticle.twig you could stage the next level: embed tag. You should define a block tag inside of someArticle.twig, this block will be the placeholder to the custom values.
{# on article.twig#}
{% set articles=['someArticle.twig', ...] %}
{% for article in articles %}
{% embed article %}
{% block inside_text_article %}
... custom text ...
{% endblock %}
{% endembed %}
{% endfor %}
{# on someArticle.twig #}
... article text ...
{% block inside_text_article %}default values{% endblock %}
... article text ...
https://github.com/fabpot/Twig/issues/17 - no dynamic namespaces
LiipThemeBundle might be a solution:
http://symfony2bundles.org/liip/LiipThemeBundle
Unless I am not understanding the question, displaying a single article only requires that it also extends __page_base.twig.
In the case that you would need to display multiple articles, you could have a template dedicated to showing a list of articles which would also extend __page_base.twig and pass the article list to this template.

Categories