In wagtail, is there a good way to add the same fields to every page model? I am thinking about things like SEO data fields and search meta information that virtually every page would like to implement. I cannot seem to find a good way to do this using Wagtail.
You can use abstract classes (you then inherit from this base class instead of Wagtail's Page) or mixins (you then inherit from both the mixin and Wagtail's Page) for that.
# Example with Abstract class
class BasePage(wagtail.wagtailcore.models.Page):
seo_image = models.ForeignKey(...)
class Meta:
abstract = True
class MyPage(BasePage):
pass
# Example with mixins
class SEOMixin(django.db.models.Model):
seo_image = models.ForeignKey(...)
class Meta:
abstract = True
class MyPage(SEOMixin, wagtail.wagtailcore.models.Page):
pass
Using abstract classes is probably the simplest, but all your pages will always inherit all the fields defined on the base class.
Using mixins on the other hand is more flexible as you can have multiple mixins (SEOMixin, ThumbnailMixin, etc) and combine them depending on your need.
Related
I'm working on a Wagtail app which has a number of different page types RawHTMLLandingPage, MarketingLandingPage, etc. which are children of a base page, LandingPageBase.
If I understand correctly, a child page type knows what type it's parent is. But is it possible for a parent to programmatically know about it's children?
When we're requesting a landing page, we don't already know what type it is (other than a 'landing page' until it hits the API and we query the database).
class LandingPageBase(Page):
pass
def identify_children(self):
# Can I find out about the two children from this method?
pass
class RawHTMLLandingPage(LandingPageBase):
pass
class MarketingLandingPage(LandingPageBase):
pass
There is a get_page_models function in wagtail.core.models which returns all classes that descend from Page, so it would be possible to filter that list to just the ones that are subclasses of a given parent:
from wagtail.core.models import get_page_models
class LandingPageBase(Page):
#classmethod
def identify_children(cls):
return [model for model in get_page_models() if issubclass(model, cls)]
I'm very new to React and I'm trying to make a simple application that renders html controls.
I want to have an API that return array of json objects contains properties that determine the type and the value of each control like id, type, value ... etc.
Let's say that I have some controls like Input and Button.
What I'm thinking of is Creating a base class named HtmlControl that extends React.Component and other html controls should extend from the HtmlControl and finally a class for rendering these controls.
How can I be able to render the controls that extends only HtmlControl class ?
That would not be a good choice as react says you should not focus inheritance rather focus on containment or composition.
React has a powerful composition model, and we recommend using composition instead of inheritance to reuse code between components.
and
At Facebook, we use React in thousands of components, and we haven't found any use cases where we would recommend creating component inheritance hierarchies.
Props and composition give you all the flexibility you need to customize a component's look and behavior in an explicit and safe way. Remember that components may accept arbitrary props, including primitive values, React elements, or functions.
Please Visit this link to learn more.
You can create separate classes for each of the components or elements and combine them in one class like this:
import Button from './Button'
import Radio from './Radio'
import TextInput from './TextInput'
class HtmlComponent{
// note: we don't need to extend it from React.Component
// we will some function to render them.
renderTextInput() { <TextInput /> }
renderRadio() { <Radio /> }
}
// you can now use below code:
const hC = new HtmlComponent();
hC.renderTextInput();
Note Again this is not a nice idea to do but you can achieve your goal.
I am using cakephp v2.6 to develop a web app.
Is it possible to call a controller within another controller in CAKEPHP.
Is it correct way for doing the same
In SecondController.php
App::uses('FirstController','Controller');
class SecondController extends AppController
{
$firstcontrollerobject=$this->FirstController;
}
Thanqs
Short answer is Yes, but you shouldn't.
You should use either a component or a model. Or put your action in AppController if you want it to be used by other controllers.
App::uses('FirstController','Controller');
class SecondController extends AppController
{
public function test() {
$FirstController = new FirstController();
$Firstcontroller->action();
}
}
Put PHP code in the Component, if you want to share between several controllers
Components are packages of logic that are shared between controllers. CakePHP comes with a fantastic set of core components you can use to aid in various common tasks. You can also create your own components. If you find yourself wanting to copy and paste things between controllers, you should consider creating your own component to contain the functionality. Creating components keeps controller code clean and allows you to reuse code between projects.
http://book.cakephp.org/2.0/en/controllers/components.html
I have a custom structure in my CakePHP app which goes like this:
class AppController extends Controller // default
class ExtendedAppController extends AppController
class ChildController extends ExtendedAppController
The components I declare in ExtendedAppController get erased when I declare components in a ChildController class. I guess I will have this same problem with helpers also. How do I merge the arrays to avoid this?
Cake merges the current controller's variables with only ONE parent class which by default is set in the variable $_mergeParent = 'AppController'; in the core Controller class.
You can override this variable in your ChildController by defining:
class ChildController extends ExtendedAppController {
protected $_mergeParent = 'ExtendedAppController';
}
However, this will ignore all the helpers and components defined in AppController, so copy the components and helpers from your AppController to your ExtendedAppController. This should answer your question I guess as you will be able to use ExtendedAppController's components from your ChildController and other controllers extending AppController will use AppController's components.
It is the way the Controller::_mergeControllerVars() method is written in the core. This is precisely why the book says:
The HtmlHelper, FormHelper, and SessionHelper are available by
default, as is the SessionComponent. But if you choose to define your
own $helpers array in AppController, make sure to include HtmlHelper
and FormHelper if you want them still available by default in your
Controllers.
Using CakePHP, I am finding that I'm duplicating some code between controller actions. I have a dozen or so actions (belonging to various controllers) that all need to run the same query and set() the same 10 variables for the use in a particular layout. They also need to handle any errors in the same way and render an error page.
I know that components are intended to centralize logic used among controllers, but in my case, this logic needs access to the set() and render() methods of the controller. What is the suggested approach to this situation?
Thanks, Brian
Put the logic in your AppController class which your controller should extend from.
Check out the docs: http://book.cakephp.org/view/957/The-App-Controller
Ended up rolling my own sort of business logic layer on this one. Example below. Thoughts/comments welcome.
class MyController extends AppController {
public function my_action() {
// The BLL class is specific for this action and gets the entire
// controller so has access to the set() method as well as components.
$this->Bll = new MyActionLogic($this);
$this->Bll->do_whatever();
}
}