Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Using a set variable in an include for an if / else

Started by Muut Archive 9 years ago · 1 replies · 708 views
9 years ago

Hi, struggling to work out how to do the following
template which calls an include for a menu, because the menu code is outputted twice (once for mobile, then again for the full screen menu) there is a little difference if the html code before hand for the right containers.

Can i set a variable to test in the include

{% set menunavigation = "responsive" %}
{% include 'partials/navigation.html.twig' %}
{% endblock %}

then later on
{% set menunavigation = "fullscreen" %}
{% include 'partials/navigation.html.twig' %}
{% endblock %}

then in the standard navigation.html.twig

{% if menunavigation="responsive" %}
<div class="nav-toggle">
<div class="icon"/></div>
<div class="overlay-nav">
<nav id="menu">
<ul class="navigation">
{ else% }
{% if menunavigation="fullscreen" %}
<nav id="menu">
<ul class="navigation">
{% endif %}

then the normal navigation code
not sure if the scope would be seen or it needs to be passed as variable or a function

could some one clarify and tell me where I am going wrong :-/
Thank you

9 years ago

Had a nights sleep and came away from it for a while and ended up resolving it myself after reading a post so wanted to post my findings of a novice and maybe it will help someone else out there.

To send a variable to an include is the easiest way of calling a way of testing something.

Use something like this

in my base.html.twig
{% include 'partials/navigation.html.twig' with {'menunavigation' : 'mobile'} %}
where menunavigation is the variable and mobile is the data

later on in my code I call the same partial navigation with this command
{% include 'partials/navigation.html.twig' with {'menunavigation' : 'responsive'} %}

then in my navigation.html.twig

{% if menunavigation == "mobile" %}
<div class="nav-toggle">
<div class="icon"/></div>
<div class="overlay-nav">
<nav id="menu">
<ul class="navigation">
{% endif %}

{% if menunavigation == "responsive" %}
<header>
<div class="container">
<div class="row">
<a id="logo" href="https://getgrav.org"></a>
<nav id="menu">
<ul class="navigation">
{% endif %}

Then it loops through the standard navigation.html.twig code

So why did I struggle and create this post ... SYNTAX !!!
Something I didn't realise was the sensitivity of grav and a couple of crafty space ended up in my "if" statement, so me thinking I cant work out the logic operators of an "IF" or that the variable is not being seen, whilst all along it was a cheeky little space which had ended up in my of my {% endif %} --- mine originally was {% endif % } <basically a space between the % and the bracket, foolish error but stumped me as you just kept looking over it.

Woo hoo it works, this is very handy instead of writing a separate file include file for each of the instances.

Still not sure how the variable is set (global or based on the calling page, or if for good practice I should destroy the variable myself after completing the code.

Hope this helps someone who is learning :-) but happy for input if anyone has a better way or better technique of doing this .

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1323 9 years ago
Archive · by Muut Archive, 9 years ago
2 919 9 years ago
Archive · by Muut Archive, 9 years ago
2 4048 9 years ago
Archive · by Muut Archive, 9 years ago
1 2923 9 years ago
Archive · by Muut Archive, 9 years ago
3 1106 9 years ago