Probably :). But you would need the "href" to be set to a Twig variable. What exactly are you trying to accomplish? Do you want to loop over some variable and create multiple "Tabs-Content" elements... but one of them is slightly different? Let me know - your example helps... but I still can't quite see what you want to do.
Hi and thanks for your time, I would like to show a mix of items tweet and linkedin if href is mixed, only item tweet if href is tweet and only item linkedin if href is linkedin. Something like this: ` <div class="Grid-inner">
<div class="Grid-wrap">
{ if href === '#mixed'}
{% for item in mixed %}
... do something ... {%endfor%}
{% elseif href === '#tweet' %}
{% for tweet in tweets%}
... do something
... {% endfor %}
{%else href === '#linkedin'%} {% for post in linkedin%} ... do something ... {% endfor%} {% end if %}
</div>
</div> ` The html code inside loop for is the same, just the variables (mixed, tweets, linkedin) sent by controller will change to show the good content. An user use a button to toggle between them.
Hi, finally I found a solution. I changed the html and used the right css. But I would like to know also a different solution by setting a Twig variable. This way I could improve the duplicated code. Thanks again
Ah, I think I understand! You literally want to be able to read the "#anchor" off of the URL and use that in Twig to do different things based on this part of the URL, right? If so, that part of the URL (e.g. https://yourdomain.com/page#anchor) is not available in PHP at all - it's not actually sent to your server (your server just sees https://yourdomain.com/page). So, this isn't possible to access in PHP or Twig.
However, I think what you are really after is a way to have these 3 sections - mixed, tweets & linkedin - in your Twig template without duplicating the HTML code around them (because all 3 sections are basically identical). I would probably do this:
A) Extract the HTML into a macro - something like this
{% macro tab_content(html) %}
<div class="Grid-inner">
... other markup
.. finally print the HTML passed to your
{{ html|raw }}
</div>
{% endmacro %}
B) In your template, use the special "set" syntax to set each content - for mixed, tweets & linkedin - to a variable. Then pass that variable to the macro:
{% set mixedHtml %}
{% for item in mixed %}
...
{% endfor %}
{% endset %}
{% set tweetsHtml %}
{% for tweet in tweets %}
...
{% endfor %}
{% endset %}
{{ _self.tab_content(mixedHtml) }}
...
{{ _self.tab_content(tweetsHtml) }}
I'm using the _self. because I'm assuming you will put the macro inside the same template - you can also put it in a different template.
Hi, this is a good idea. Just to be a little bit more clear (sorry for my bad explanation of use case), I don't use the "#anchor" of the URL. It's just a button: https://drive.google.com/op... What you see is a part of the home page. In the first message I wrote the code to manage this button. There is also the js to add and remove classes to move it (up and down when you click). The '#anchor' is to call the right div with the class 'Tabs-content'. Thanks again for your help P.S. I hope is more clear :)
Ah! It's making more sense now :). Ok, suppose the page loads to "Bla Bla Bla" originally. Now I click on "Tweet". This adds the #tweet to the URL and maybe you even have some JavaScript to change the "tab" to the Tweet tab. When the URL changes to have #tweet on it, there is no full page refresh (sorry, this may already be obvious to you - just saying it in case) and so Symfony/Twig doesn't have any opportunity to render the Tweet content. What you need to do is render all 3 of the tab contents on initial page load (then just switch the tabs with JavaScript). The tricky part (and I think this was maybe your real question) is how to render all 3 tab content areas without duplicating all the markup 3 times. For that, I'd try the macro thing :).
Ok, I will try to improve my code with Macro. It will be a good challenge but it won't be easy to have the same result. really Thanks for your help and your time.
What URL do you have when you see that NotFoundHttpException? It should be something like "http://localhost:8000/news/some-slug" where "some-slug" might be any string actually.
Btw, could you open this http://localhost:8000/news/some-slug URL, do you still see the error? If so, try to clear the cache with "bin/console cache:clear --env=dev" or instead of "dev" put the environment you actually use and try again to hit that http://localhost:8000/news/some-slug .
Still the same error? Then could you debug the routes registered in your system with "bin/console debug:router", do you see that "/news/{slug}" path in the output?
After you did what that error occurred? If it happend after trying to render a Twig's template, probably you forgot to extend from Symfony's Abstract controller. Add this import to your class Symfony\Bundle\FrameworkBundle\Controller\AbstractController
Ah, yes! Make sure you make your ArticleController extend AbstractController - that’s where this method comes from :). It’s the first thing we do in this chapter.
my question is why symfony decided that you have to install twig when in symfony 3 it was done automatically. This indicates to me that we are able to use different templates? (blade) for example?
Great question :). This reflects a philosophical difference in Symfony 3 versus 4. Basically:
Symfony 3: Give the user a bunch of features out-of-the box. But, some features you won't need, and you will still need to install other features
Symfony 4: Start very micro so that the user has a small and super fast app. We don't know anything about their app (API, web app?), so instead of giving them things they don't need, allow the user to install what they need. BUT, use Symfony Flex to make installation much easier :).
So, there still *are* many packages that we recommend: like Twig *if* you need a templating engine or Doctrine *if* you need an ORM. To make those packages easier to find and install, we use the "alias" system in Flex.
As far as *other* templating systems, for example, these are just as easy to install (if a bundle exists in the community) in Symfony 3 or Symfony 4 - not much has changed there.
Probably worth noting that you can run the following (instead of the command used in the 1st tutorial) to install basic website plugins, including twig:
Yea, indeed, that's a good tip. I don't *want* people to do that for this tutorial (because I want you to follow along exactly), but actually, yea, the website-skeleton is what we use on the official docs, since it gives you a bit more out of the box :).
Cheers!
Please, log in to vote for this comment
1|
Share Comment
"Houston: no signs of life" Start the conversation!
17 Comments
Hello guys,
Is it possible using the href attribute in the if statement of twig? With an example is more clear:
Thanks for your help
Hi Gaetano S.!
Probably :). But you would need the "href" to be set to a Twig variable. What exactly are you trying to accomplish? Do you want to loop over some variable and create multiple "Tabs-Content" elements... but one of them is slightly different? Let me know - your example helps... but I still can't quite see what you want to do.
Cheers!
Hi and thanks for your time,
I would like to show a mix of items tweet and linkedin if href is mixed, only item tweet if href is tweet and only item linkedin if href is linkedin.
Something like this:
`
<div class="Grid-inner">
{ if href === '#mixed'}
...
do something
...
{%endfor%}
{% for tweet in tweets%}
...
do something
...
{% endfor %}
{%else href === '#linkedin'%}
{% for post in linkedin%}
...
do something
...
{% endfor%}
{% end if %}
</div>
`
The html code inside loop for is the same, just the variables (mixed, tweets, linkedin) sent by controller will change to show the good content. An user use a button to toggle between them.
Hi, finally I found a solution. I changed the html and used the right css. But I would like to know also a different solution by setting a Twig variable. This way I could improve the duplicated code. Thanks again
Hey Gaetano S.!
Ah, I think I understand! You literally want to be able to read the "#anchor" off of the URL and use that in Twig to do different things based on this part of the URL, right? If so, that part of the URL (e.g. https://yourdomain.com/page#anchor) is not available in PHP at all - it's not actually sent to your server (your server just sees https://yourdomain.com/page). So, this isn't possible to access in PHP or Twig.
However, I think what you are really after is a way to have these 3 sections - mixed, tweets & linkedin - in your Twig template without duplicating the HTML code around them (because all 3 sections are basically identical). I would probably do this:
A) Extract the HTML into a macro - something like this
B) In your template, use the special "set" syntax to set each content - for mixed, tweets & linkedin - to a variable. Then pass that variable to the macro:
I'm using the
_self.because I'm assuming you will put the macro inside the same template - you can also put it in a different template.References: https://twig.symfony.com/doc/3.x/tags/macro.html and https://twig.symfony.com/doc/3.x/tags/set.html
Let me know if that helps!
Cheers!
Hi, this is a good idea. Just to be a little bit more clear (sorry for my bad explanation of use case), I don't use the "#anchor" of the URL. It's just a button:
https://drive.google.com/op...
What you see is a part of the home page. In the first message I wrote the code to manage this button. There is also the js to add and remove classes to move it (up and down when you click). The '#anchor' is to call the right div with the class 'Tabs-content'.
Thanks again for your help
P.S. I hope is more clear :)
Hey Gaetano S.!
Ah! It's making more sense now :). Ok, suppose the page loads to "Bla Bla Bla" originally. Now I click on "Tweet". This adds the #tweet to the URL and maybe you even have some JavaScript to change the "tab" to the Tweet tab. When the URL changes to have #tweet on it, there is no full page refresh (sorry, this may already be obvious to you - just saying it in case) and so Symfony/Twig doesn't have any opportunity to render the Tweet content. What you need to do is render all 3 of the tab contents on initial page load (then just switch the tabs with JavaScript). The tricky part (and I think this was maybe your real question) is how to render all 3 tab content areas without duplicating all the markup 3 times. For that, I'd try the macro thing :).
Let me know if that helps!
Cheers!
Ok, I will try to improve my code with Macro. It will be a good challenge but it won't be easy to have the same result. really Thanks for your help and your time.
hey iv followed the vidios step by step twice not but i keep getting
these two Exceptions
NotFoundHttpException
ResourceNotFoundException
im not sure what i have done wrong
Hey Andrew,
What URL do you have when you see that NotFoundHttpException? It should be something like "http://localhost:8000/news/some-slug" where "some-slug" might be any string actually.
Btw, could you open this http://localhost:8000/news/some-slug URL, do you still see the error? If so, try to clear the cache with "bin/console cache:clear --env=dev" or instead of "dev" put the environment you actually use and try again to hit that http://localhost:8000/news/some-slug .
Still the same error? Then could you debug the routes registered in your system with "bin/console debug:router", do you see that "/news/{slug}" path in the output?
Cheers!
Hey there
After you did what that error occurred? If it happend after trying to render a Twig's template, probably you forgot to extend from Symfony's Abstract controller. Add this import to your class
Symfony\Bundle\FrameworkBundle\Controller\AbstractControllerCheers!
Hi,
I am geetting this error
Attempted to call an undefined method named "render" of class "App\Controller\ArticleController".
Hey @Hunter Baba!
Ah, yes! Make sure you make your ArticleController extend AbstractController - that’s where this method comes from :). It’s the first thing we do in this chapter.
Cheers!
Hi Ryan,
my question is why symfony decided that you have to install twig when in symfony 3 it was done automatically. This indicates to me that we are able to use different templates? (blade) for example?
Hey Peter!
Great question :). This reflects a philosophical difference in Symfony 3 versus 4. Basically:
Symfony 3: Give the user a bunch of features out-of-the box. But, some features you won't need, and you will still need to install other features
Symfony 4: Start very micro so that the user has a small and super fast app. We don't know anything about their app (API, web app?), so instead of giving them things they don't need, allow the user to install what they need. BUT, use Symfony Flex to make installation much easier :).
So, there still *are* many packages that we recommend: like Twig *if* you need a templating engine or Doctrine *if* you need an ORM. To make those packages easier to find and install, we use the "alias" system in Flex.
As far as *other* templating systems, for example, these are just as easy to install (if a bundle exists in the community) in Symfony 3 or Symfony 4 - not much has changed there.
I hope this clarifies!
Cheers!
Probably worth noting that you can run the following (instead of the
command used in the 1st tutorial) to install basic website plugins,
including twig:
$ composer create-project symfony/website-skeleton my-project
Hey GDIBass!
Yea, indeed, that's a good tip. I don't *want* people to do that for this tutorial (because I want you to follow along exactly), but actually, yea, the website-skeleton is what we use on the official docs, since it gives you a bit more out of the box :).
Cheers!
"Houston: no signs of life"
Start the conversation!