Reputation: 1673
Assuming that I have a controller such as the following:
@Controller
@Template(id= HomePageTemplate.ID, title = "Home Page")
public class HomePageTemplate {
public static final String ID = "project:pages/home-page";
@RequestMapping("/home-page")
public String render(Model model, Node node) {
model.addAttribute("meta", new MetaModel(node));
}
}
And I want to be able to use the MetaModel in conjunction with TemplatingFunctions
and other Magnolia items - but I am unsure how to access the content map from inside this model:
public class AbstractModel {
protected Node node;
protected TemplatingFunctions tf;
public AbstractModel(Node node, @Inject TemplatingFunctions tf) {
this.node = node;
this.tf = tf;
}
public function getTitle() {
return tf.get("metaTitle");
}
}
Any thoughts on how I would get templating functions to inject?
Upvotes: 1
Views: 421
Reputation: 333
Instead of doing new MetaModel(node)
, use
info.magnolia.objectfactory.Components.newInstance(MetaModel.class, node)
in order to create a new instance of your model. TemplatingFunctions
will be automatically injected.
Another option would be to expose TemplatingFunctions
as a Spring bean, somewhere in a @Configuration
class:
@Bean
public TemplatingFunctions templatingFunctions() {
return Components.getComponent(TemplatingFunctions.class);
}
and just autowire the bean in your Spring controllers and add a new constructor to MetaModel
class:
@Controller
@Template(id= HomePageTemplate.ID, title = "Home Page")
public class HomePageTemplate {
@Autowired
private TemplatingFunctions cmsfn;
public String render(Model model, Node node) {
model.addAttribute("meta", new MetaModel(node, cmsfn));
}
}
Upvotes: 1