With blade, it is possible to show a default value if there is no data to #yield, like so:
#yield('section', 'Default Content');
But I would really like to show a view instead of a string text. The reason is that I want to #yield a navigation menu into a layout. Most of the times the navigation menu will be exactly the same, so a default option of some sort should be optimal here, but I would like a way to override the menu.
Is it possible to achieve this?
To check if the section exists you could use:
array_key_exists('fooSection', View::getSections())
and to check if the section is empty you could use:
empty(View::getSections()['fooSection'])
For example:
#section('fooSection')
Foo
#stop
#section('barSection')
Bar
#stop
<h1>If Section Exists</h1>
#if (array_key_exists('fooSection', View::getSections()))
#yield('fooSection')
#else
#yield('barSection')
#endif
<h1>If Section isn't Empty</h1>
#if (!empty(View::getSections()['fooSection']))
#yield('fooSection')
#else
#yield('barSection')
#endif
Hope this helps.
Related
Hi all i will be needing an assistance. I have a navbar that all link on my app use but i will like to seclude a search form from some of the link in the app but am having difficulty doing that. This what i have tried on my nabvar so far example
Tried using route but no success
#if( (route('portfolio') &&(route('portfolio.details)
content here
#else
content
#endif
then i tried targeting the URI
#if( Request::is('/portfolio') &&(route('portfolio/details)
content here
#else
content
#endif
None seems to working.
I don't think you should be using the route helper method in your if statement. I think if you just do something like this, it should work.
#if (request()->is('portfolio'))
content
#else
content
#endif
I am new to symfony.SO please find me a solution.On my controller
$table="<table><tr><th>Test</th></tr><tr><td>Show Test</td></tr></table>"
And I have Passed it to the twig from the controller like
return $this->render('lottery/transactions/trnissuenote/show.html.twig', array(
'table'=>$table
));
And on my twig
<div >
{{table}}
</div>
But i didnt get the table structure .Just echoing it.How will i get the table structure.Help me find a solution.
You need to display it using raw filter. Since by default {{}} escapes html content of a variable.
<div>
{{ table|raw }}
</div>
Read more: https://twig.symfony.com/doc/2.x/filters/raw.html
I am using laravel 5.3.
I know that how can paginate large data set and how to work it.
Pagination links shown Even if just one page is require to show result. but I want to hide pagination links in this case?
I use render() method to show pagination links like this :
<nav id="pagination">
{!! $posts->render() !!}
</nav>
Any idea?
The best way to handle this is to hide the links to the pagination in your front-end if total <= per_page.
If you're using laravel's links() method you can do the following:
#if($results->total() > $results->perPage())
{{ $results->links }}
#endif
Here is better approach by undocumented method, for Laravel 5.0 and later.
#if ($results->hasPages())
{{ $results->links() }}
#endif
Link to method:
https://laravel.com/api/5.8/Illuminate/Contracts/Pagination/Paginator.html#method_hasPages
Because I want to apply this behavior to all paginations on the whole laravel project ,I used #Vuldo suggested approach to default.blade.php view in the resources/views/vendor/pagination directory like this :
#if($paginator->total() > $paginator->perPage())
<ul class="pagination">
...
</ul>
#endif
I have a language JSON file containing the text of each page on my website. So to fetch the title of the page, I'll do something like
{{ translation.page1.title }}
In order to fetch the title for page 1.
I already have a page variable that tells me the name of the page, I was wondering if it was possible to do something to avoid having a giant if statement for each page such as:
{{ translation.{{ page }}.title }}
I've looked through the twig doc and I have no idea.
Try to put your variable between brackets
{{ translation[page].title }}
That should be work
I have a question: how can 1 view/page use 2 controllers to display different data?
For example:
1) Page displays DVD's information using DvdController.
2) On that same page I want to use a Template/Partial view, that displays list of actors, and that list should come from the second ActorsController.
I can add a template view inside another page but the second controller doesn't work:
MainPage on URL: website/dvd
{% block body %}
<div>
<h1>{{ dvd.title }} </h1>
</div>
<div>
{{ include('DVDBundle:Actors:actors.html.twig') }}
</div>
{% endblock %}
Above I use the partial view actors.html.twig to show the actors, strangely the html code/div's and so on are actually displayed and working on the page, however the controllers(ActorsController) method that meant to return this partial view is not executed for some reason.?
But if I go to the view directly via link: website/actors then its working.
The Symfony documentation on embedded controllers explains this nicely.
In short, you will need to do something like this in your template file:
<div>
{{ render(controller(
'DVDBundle:Actors:actors',
{ ... parameters to the controller action here ... }
)) }}
</div>
The important thing is calling the render function, which allows a controller to be rendered, as opposed to simply includeing a template. (This assumes that your have an ActorsController with an actorsAction method; change the names as appropriate to your controller.)