The website I'm working on has a navigation menu item called categories which is a drop-down containing some entries from the db (the users can adjust the categories from the admin panel.) The navigation is located in a base.html.twig file which is extended by all other twig files.
Question: What's the best way to get those entries? The only way that comes to my mind is use a call to {{ render(controller(...)) }} which would create a new request, what's a little overkill in my opinion and will slow the page in general down. Is there a better way to do it? Maybe an event which is called on every request and is able to transfer data to the view file?
You have two good options to achieve this :
Use render() in your twig template to call a specific method from a controller (as you said in your message)
Create a twig extension to render your menu, it's very simple : http://symfony.com/doc/current/cookbook/templating/twig_extension.html
In my opinion you should use the first option (a controller), since you only need to render your menu 1 time. Twig extension are better designed to be reused in several templates.
About your performance concern, don't worry, all you need is to cache your menu since it won't change often, and invalid the cache when the menu is updated in your backoffice.
Regards
Related
I'm starting with EasyAdminBundle for Symfony.
For default usage it is perfect, but what if I wanna create custom view and use for it custom controller?
Let's say I have a task to load json file, display all his data and user then can edit data and save them.
Ok, I have my own custom controller, no big deal, data are loaded, I have custom view, data are there. I can click on link in left menu and see them. Ok. But I can't put together this view with EasyAdminBundle. To show view in a this nice theme. What I have to extend?
'#EasyAdmin/default/layout.html.twig'?
Yes, if you want to extend the default layout you can extend EasyAdminBundle's layout.html.twig like this:
{% extends '#EasyAdmin/default/layout.html.twig' %}
You can also check out the default templates e.g. for lists, but unfortunately the are a bit hard to read due to the high level of abstraction.
Let me first explain what I am trying to do.
In joomla 3.0 I have created a Menu_Item_Text_Separator override for my template http://docs.joomla.org/Help30:Menus_Menu_Item_Text_Separator. It seems as though joomla will only recognize one default Text separator per template which is ok if you just want the one. but I ideally would like to have the choice of selecting the custom one in my template folder as well as the default one that joomla recognizes. To inform you this is what I have done to make it happen.
in my template I have a folder named html which had a folder called mod_menu inside it.
In the mod_menu folder I have the .php files called:
default_separator.php
custom_separator.php
I then go into menu manager and edit the menu item for which I want to display a text separator for.
I then go to 'template style' http://docs.joomla.org/Help30:Menus_Menu_Item_Text_Separator and look for my custom style. but I only can choose the default one.
So i wonder if this is way that joomla works that you can only have one default per template. but is it possible to have more than one?
any advice most welcome.
regards
w9914420
Sorry this got too long for a comment.
Okay let's start from the beginning. Templates have a set of parameters defined in the templateDetails.xml file. A template style is simple a record containing the information about a template and the array of parameter options you have selected. You can make as many template styles as you want for a given template Each one has its own name. In the menu you can select any of the styles and assign it to a menu item. ....
What you are talking about has nothing to do with template styles. You are talking about using a layout override for mod_menu. Because you are using a file with the same name as a core layout file you should get a 1:1 replacement.
From what I understand of what you want to do, you should instead make a new named replacemen both for default_separator and for default.php. THat's because the alternative layout field is going to look for a replacement for default.php say yourname.php and then in that replacement when you load a template called separator it is automatically going to look for yourname_separator rather than default_separator because it assumes you are appending the _separator to the base name. If you do this it has some advantages such as you will be able to make a more complex layout and it will allow you to load different sub layouts conditionally for example.
I think your are confusing template styles with the style for menu module. Unless you have a parameter in your template that specifies the choice of a mod_menu layout a template style is not going to help you.Go to he module manager and pick the module you want to apply the style to. Use the field to select the layout you want. Or if it is a css style use the style option. It could also be that what you really want is to apply module chrome. THat do can be done by editing the module. If you want to have the same menu with different layouts or styles you'll probably want to make additional modules for that menu.
I had a rethink of what i was trying to do. What I was trying todo could not be achieved through the method I hoped - creating a template for that one text separator would not be practical although I have now discovered how to feed template parameter values into layout overrides.
Thank you Elin for your time
regards w9914420
We have a controller that manages sales.
We need to display a snipped of those sales on a dashboard. No more logic is involved.
What should that snipped be ?
A clip ? A partial ? Or a Widget ?
You're likely looking for a widget.
Clips are (I think, I've not really used them much) specialised widgets for replacing common content on a page with a more specific version on the page.
I'm not sure what you mean by partial but I'd guess a renderPartial? This could work, but it means you have to provide that partial view with the data it needs every time you use it.
Widgets can pull their own data from your sources, take parameters to change behaviour and be themed. They're completely reusable and you can place them in your layout, or in your case, the dashboard view.
There can be many small widgets on the page which can be responsible for their own data, or you can have a parent->child structure, so you call the parent widget with some parameters, this gathers up the required data and splits it among the "child" widgets for different methods of display (graphs, tables, comparisons etc.).
I think you should prefer widget for this requirement. We had some discussion over widget & renderPartial here
What should I prefer to use widget or renderPartial in Yii's view?
I hope you will get your answer.
Could someone tell what is good logic for creating custom helper for Views?
What I would like to accomplish is...
load header
load main navigation (static for all pages)
load some widgets etc. (static for all pages)
load content/main pages (dynamic)
and in the end, load footer of course
Could someone point me to the solution?
Thanks a lot, in advance!
This question gives some explanation to the method dqhendricks describes. It includes code examples showing how to use $this->load->view() to return the view to a variable instead of outputting it to the browser, how to include sub-views, and using a specific view as your main template. It's the best way I've found of handling view logic. Getting the hang of CodeIgniter - Templating / loading views
Some people will create a master template, then pass the sub content's view name in the data object. The master template will call the header view, footer view, etc, and use the data array to dynamically display the correct sub content view.
For a Template library that is more actively developed than Collin's (linked by predrag.music) try out mine:
https://github.com/philsturgeon/codeigniter-template
His is good but this supports modules, themes, mobile version variations of themes and plenty more.
you could use inheritance chaining (with a custom controller class or with an included extra class that extends controller that you extend from your controllers instead of the controller class) to make a common controller hub for all pages that has a method for the top stuff, and a method for the bottom stuff.
If you need something for the reference this one is good:
http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html
I want to have a view displayed as a menu either in primarylinks or secondarylinks.
I searched and looked up all modules with no luck.
should I build this functionality manually ==> build a module?
thanks in advance
Primary and secondary links (in fact, all menus) are rendered as unordered lists of links using a theme function like theme_links(). You can get pretty close to the same functionality by using the HTML List style within a view.
You can't duplicate it exactly because Drupal's menu system requires static menu items: it doesn't handle wildcards like the results of a query.
That is, Drupal's menu system is cached, and is only rebuilt upon request (e.g. by adding a menu item manually, clearing the cache, etc.) A view, on the other hand, is a wrapper to a query: every time you access the view, unless it too is cached, it runs a query to get the latest results.
So, if you were to inject a view into a menu, it'd only be the results at the time of first request, and any subsequent changes would require rebuilding the menu.
The solution I suggested will let you keep the functionality of the view, theme it to look like a menu, and avoid the caveats of the menu system.