Jump to content

Difference between add and append


pogidude
 Share

Recommended Posts

I'm working with inputfields and saw in core/InputfieldWrapper.php file the add(), append() and other related methods.

I tested both and came out the same:

      $modules = $this->modules;

      //$this->setFuel('processHeadline', 'Content Flow Here');
      $form = $modules->get('InputfieldForm');

      $field = $modules->get('InputfieldMarkup');
      $field->label = $this->_('My Markup');
      $field->attr('value', '<h4>Not my markup content</h4>');

      $field2 = $modules->get('InputfieldMarkup');
      $field2->label = $this->_('Second Markup');
      $field2->attr('value', '<h4>Not my markup content</h4>');

      $wrapper = new InputfieldWrapper();
      $wrapper->attr('title', 'Wrap Title');
      $wrapper->attr('description', 'Wrapp');
      $wrapper->add($field);
      $wrapper->append($field2);

      $form->append($wrapper);
      //$form->append($field2);

      return $form->render();

so, my question is, what's the difference between the two? and when should I use one over the other?

Link to comment
Share on other sites

They're both doing the same.

There are also other methods available:

  • prepend(Inputfield $item) : Adds your Inputfield on the first position
  • insertBefore(Inputfield $item, Inputfield $existingItem) : Add your Inputfield before the Inputfield $existingItem
  • insertAfter(Inputfield $item, Inputfield $existingItem) : Add your Inputfield after the Inputfield $existingItem
  • Like 1
Link to comment
Share on other sites

We have append() for consistency with jQuery. I also like the team of prepend/append naming (it sure beat's PHP's unshift/push terminology). But I also think the term "add" is ultimately more friendly, especially when talking about adding an item to a group of items. As a result, I tend to use add() when adding items and append() when moving things around. Ultimately they do the same thing, but one may be more readable than the other depending on the context. Use whatever you prefer. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...