On Drupal 8 I'm trying to override the template of a block created using views. So far I've a file name as block--views-block--xxx.html.twig which gives me access to the view fields.
I've to do something like content.view_build['#rows'].0['#rows'] to get the rows which is pretty awful but I couldn't find any other way. Also I've to set "Show content" on the views settings.
I can not get the pager to work at all. Reading the documentation from Drupal I can not find any clues.
Turn on twig debugger (he's showing, in inspector tools, all templates what used now).
In inspector tools you see something like this:
- html--internalviewpath.html.twig
- html--node--id.html.twig
- html.html.twig
Then just create template with this name add you custom architecture. For example: html--node--id.html.twig. Check you can hear to, you see this:
- html--internalviewpath.html.twig
+ html--node--id.html.twig (now twig use this template)
- html.html.twig
I think you should rather override the view template rather than the view-block template.
View templates Naming conventions can be found here and the variables you can work with are listed here. you can now put the pager, header, rows and other view elements in different sections in template.
If you need to override the contents of the view rows then you will need to take action according to how you are displaying the content in the view.
If you use a teaser view of content you can override the content teaser template like "node--content_type--teaser.html.twig", or if you use fields in view then you will have to override the fields templates of each field.
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
I have just started YII one week back. I have made a theme in that is
\themes\yog
it is very small project, just 4-5 forms, those are mostly fro the admin. so what i want now it suppose i am using the
site/login
route, I want to render it via YII's default views . i.e
\protected\views
I want to run the default views for only some actions, like login, or adding news, event and other admin related tasks. But other actions should render the views from my new theme.
Thanks.
In that SiteController set Yii::app()->theme to a non existing theme, and it should fall back to use the views from the protected/layouts file.
If you want to specify different layout files read this: Yii: Render action using different layout than controller's layout
I have installed the HerzultFourmBundle into my Symfony2 application, added the calls in the app kernel and auto load and extended the base classes. The problem i am having is i have a site layout that is applied to every page in the application and i want to render the forum within this.
I am very new to Symfony and PHP, but i have tried a few ways to get this working. I have a controller method set up that will render the forum page correctly which is set through the normal routing configuration, i can also set it up so that is routes to the index method in the Forum controller in the Herzult forum bundle. However this renders the default styles for the forum index page and is not contained by the layout i require.
Can someone provide me with an example of how to implement the forum and integrate it into a site? A simple example would point me in the right direction.
If you look at bundle's main layout file you'll notice that it extends '::base.html.twig' template:
{% extends '::base.html.twig' %}
This is an application level template located in apps/Resources/viewsbase.html.twig file.
You should be able to define your custom stuff as long as you're using this template.
This is a convention called "Three level template inheritance". Read more about it in the documentation: http://symfony.com/doc/2.0/book/templating.html#three-level-inheritance
Note that HerzultForumBundle is currently under intensive development. Some features might be missing.
You have the URL:
http:///www.site.com/controller/method/values/1/2/3
Do I have to always have the controller being called or can I have the view being called and instantiate the controller inside the view or in a bootstrap file referring to this view?
What I don't get it is if I need more than 1 controller on the view, how to archive that?
For example:
On my index page I want run a simple CMS, where the admin can change the text blocks and images of the site. That would be on the content management controller.
On my index page I also got the latest added products vitrine, what would be controlled by the controller products.
If I define www.site.com/contentmanagement or www.site.com to run the contentmanagement controller, how the product controller would be called?
Also, another example. On my menu I got a link to a page called aboutus, that would be a simple page and the only feature needed would be the content management controller to manage the texts blocks.
If I follow the pattern Im reading all over the place I will end with a link like:
http://www.site.com/contentmanagement/method/aboutus
?
Kinda lost here cause surely this URL will look weird. Would be much easier to have the URL calling the view http://www.site.com/aboutus and a boot file where I can tell the controller that should be loaded when the surfer is there ...
bootstrap would look like:
switch($view)
case: index
controller load contentmanagement
controller load product
case: aboutus
controller load contentmanagement
I appreciate any help or a light here, thanks.
by the way, Im coding in PHP.
Hm, if you want to have text blocks and images of the site (one controller), and products vitrine (second controller), then call the methods you need in one controller..
I would do it this way: when you request index page with all the elements you mentioned above, actually one controller is called, and it decides which view to show.. so in that controller, you call the all methods that you need, get the data, and pass it to the view.. if the methods are called from other controllers, just call that methods and get the data.. i often make some static methods in controllers, which I can call anywhere, so I don't have to instantiate whole object..
E.g. you call www.site.com/contentmanagement, controller is called, which will display index view, and in that controller, you call all methods you need, prepare the data, and pass that data to the final view which will be displayed..
Do I have to always have the controller being called or can I have (..blah blah..)?
It kinda depends on what you understand by "call".
Controller needs an ability to change state of both View and Model(s).
And each View need ability to request data (in Model2 MVC) from Model(s).
Thought Controller should not cause the rendering of View (which is common in all the RoR sycophants) much like View has not business in executing actions on the Controller.
What I don't get it is if I need more than 1 controller on the view, how to archive that?
Views and Controllers should have 1:1 relationship. If your view need more then one controller, you might want to look into HMVC architecture.
For example: On my index page I want run a simple CMS, (.. more blah .. ) how the product controller would be called?
CMS should be a separate application ( or at least module ) with multiple controllers.
If I follow the pattern Im reading all over the place I will end with a link like: http://www.site.com/contentmanagement/method/aboutus ?
Why not just http://site.com/cms/content/2/edit (where 2 is the ID for "about us" page). There is no law which states that URL structure for site administration must mirror the publicly available page .. hell .. it can actually cause increased vulnerability.
I am going to try to walk
What I don't get it is if I need more than 1 controller on the view,
how to archive that?
For example: On my index page I want run a simple CMS, where the admin
can change the text blocks and images of the site. That would be on
the content management controller. On my index page I also got the
latest added products vitrine, what would be controlled by the
controller products. If I define www.site.com/contentmanagement or
www.site.com to run the contentmanagement controller, how the product
controller would be called?
Having the URL to contains the controller's name is definitely nice, but it is not the requirement for MVC. So you don't have to necessary map your controller to the URL itself. A classic MVC does not tie itself to a particular naming convention, so you could call your product controller via different URL which then process the product and show the view for the product.The important for MVC is to have one controller that deals with sets of model and resulting in one view as the presentation layer. In your particular example, your entry point seems to be a single controller that interacts with both ContentManagement and Product Class/Module and the resulting interaction produce a single view.
bootstrap would look like:
switch($view)
case: index
controller load contentmanagement
controller load product
case: aboutus
controller load contentmanagement
Thus your original interaction above is not completely off, except that you are not really calling two controllers, but rather doing the following upon hitting index:
Load a controller, let's call this one IndexController
Load ContentManagement module to get the related content, you might want to put the info as part of your Model for the Index page
Load Product module to get related products, again put this into your Model
Pass the Model containing the info for rendering the page into the View
In your View, you render the Model that contains both Content from ContentManagement module and Product list from the Product module thereby producing a single view
If I follow the pattern Im reading all over the place I will end with
a link like: http://www.site.com/contentmanagement/method/aboutus ?
This is again not a requirement, you should follow what makes sense and easier for the users. Also, if you are required to follow the conventions, you could always make your URL pretty by using URL mapping.
What you are looking for is called HMVC and there are a couple of frameworks for that out there, like Kohana.
Also see this question:
What is the HMVC pattern?