Main Content

Magnolia Community Forums: Get help with Magnolia: Create components with template script


  • af-redouane
    af-redouane
    Full name: Redouane Afif
    Posts: 2
    Last post: Jun 1, 2017 11:33:59 AM
    Registered on: May 31, 2017
    Create components with template script
    #1 by af-redouane on May 31, 2017 10:14:51 AM

    Hello,

    I'm new to magnolia and I need to create components (nodes) using template script, is this possible?
    if not, is there any other way to do that?

    Thank you,

  • ahietala
    ahietala
    Full name: Antti Hietala
    Posts: 343
    Last post: Nov 21, 2017 1:46:37 PM
    Re: Create components with template script
    #2 by ahietala on May 31, 2017 11:45:35 AM

    Hi Redouane,

    Can you describe a use case to help us understand what you want to do? Who is interacting with the page? An editor or a visitor?

    It's a little unusual for a template script to _create_ nodes. Templating functions in Magnolia typically only consume content from the repository, then render it to visitors.

    See cmsfn templating functions
    https://documentation.magnolia-cms.com/display/DOCS/cmsfn

    Have you looked at component autogeneration? It allows you to create components inside an area automatically without editor involvement.
    https://documentation.magnolia-cms.com/display/DOCS/Component+autogeneration

    --Antti

  • af-redouane
    af-redouane
    Full name: Redouane Afif
    Posts: 2
    Last post: Jun 1, 2017 11:33:59 AM
    Registered on: May 31, 2017
    Re: Create components with template script
    #3 by af-redouane on Jun 1, 2017 11:33:59 AM

    Hello,

    I found a solution, I create component using Java model class, I can't use autogenration because created component values should be dynamic (related to page names).

    this the function I use:
    static Node newNode = null;
    static public void createComponent(Node parent, String name, String template, Map<String,Object> content) throws AccessDeniedException, PathNotFoundException, RepositoryException{
    newNode = NodeUtil.createPath(parent, name, "mgnl:component");
    if (template != null) {
    NodeTypes.Renderable.set(newNode, template);
    }
    log.debug("creating component"+ newNode.getPath());

    for (Entry<String, Object> property : content.entrySet()) {
    PropertyUtil.setProperty(newNode, property.getKey(), property.getValue());
    }
    newNode.getSession().save();
    }


    Thanks,
    Redouane

You don't have the permission to post on this thread

Sign in

To login on this forum, you can use your Magnolia Forge, Support or Partner account, or, below, your Google, Yahoo! or OpenID account. If you have trouble logging in, or any other sort of issue, please let us know in the Meta forum, on the user-list, or simply by email at forum-admin at magnolia-cms dot com.

* Required

... or sign in with:

  • icon http://{your-openid-url}
  • icon
  • icon https://me.yahoo.com/