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 %}
Related
I'm creating a real estate website with the template Realia. This theme is based on Twig files and here's my problem. I have a front end submission where we can add a custom post ( a property ). I want to add a custom field to this form. The code which get and display the field is made and work because it is in a php file ;
<?php acf_form_head(); ?>
<?php acf_form( array(
'field_groups' => array(1943),
'form' => false,
) ); ?>
But now I want to save the data of my field and the button "Add my property" is in a Twig file..
Here's the button code
<div class="form-actions">
{% set value = wp.__('Save', 'aviators') %}
<input type="submit" class="btn btn-primary" value="{{ value }}">
</div>
{% endif %}
</form>
According to this documentation , the acf code is acf_form_head() but I dunno how to put it in my code. I try {{ acf_form_head() }}, {{ wp.acf_form_head() }} and some other sentence but nothing works... I tried to to find the "save" function which is on this php file but I don't know to edit it..
Please, could someone help me ?
Thank in advance
Jennifer O.
As far as i know wp-realia theme.
{{ }}
These tags are used for outputting some thing to browser or calling methods.
add wp. before calling any function wp. means that this functions is user defined or core function. if you call function without wp. prefix it means you are calling twig template function.
{% %}
These tag are used for calling core twig functions eg {% if my_var %}.
For your scenario you want to call acf_form_head() function in header to print css/js and necessary files in the header so you can make a block in the realia/templates/helpers/header.twig file in head tag eg:
{% block header_block %}{% endblock %}
then in your custom twig template file reference that block and put your content in it:
{# we tell our custom template to extends from layout.twig #}
{% extends 'layout.twig' %}
{# Add acf_form_head() function in our header block #}
{% block header_block %}
{{ wp.acf_form_head() }}
{% endblock %}
{# add this content to our content block which is define in layout.twig file #}
{% block content %}
{% if wp.have_posts() %}
{% for post in posts %}
{{ wp_query.the_post() }}
My custom field: {{ wp.the_field('my_custom_field') }}
{{ wp.acf_form() }}
{% endfor %}
{% endif %}
{% endblock %}
Hope it will help you.
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 %}
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 %}
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
First, let me start with the code I'm attempting to use:
{% if modal == true %}
{% block header %}{% endblock %}
{% block footer %}{% endblock %}
{% endif %}
What I'm trying to accomplish is to not show my header and footer blocks ONLY if the variable called modal is true. I also have this below the if statement:
{% block content %}
{{ dump(modal) }}
{% endblock %}
What happens here is that my override for emptying the header and footer blocks always runs regardless of if the value of modal is true or otherwise. So, I run this with modal passed in as false and the result is that the header and footer still don't show. The output of the dump command accurately shows true or false, but the condition always seems to evaluate to true in the if statement.
Can blocks not be wrapped in a conditional statement, or is there something additional I need to do to make this work?
Thanks for any help you can offer.
Define
{% block footer %}Some standard content{% endblock %}
in parent twig template.
Then in template where you want to decide if display content of footer you can do:
{% block footer %}
{% if not modal == true %}
{{ parent() }}
{% endif %}
{% endblock %}
If the modal is true - footer will be empty, if not - in footer will be printed "Some standard content"
Blocks don't care about any logic around it, as said in the documentation:
A block provides a way to change how a certain part of a template is rendered but it does not interfere in any way with the logic around it.
You should put that logic inside the block, not on the outerside, as you can see on the last example in that article.