0
votes

I could add extra functionality to a dataobject if I extend it with a dataextension. For example I've got an Item which gets extended from a module with stock keeping functionality. Let's say the Item also gets's extended form a few other modules.

After the extension with the stock keeping functionality, I'd like to display the availability of the item in the frontend, for example with a green/red dot. How can I get this dot's markup inside my template for the detail page(ItemPage.ss) and the include (Item.ss) for the overview page of items without overwriting the whole template. Just adding this one part, like the way I extend a function on my base class?

2

2 Answers

1
votes

That could be a way to add extra markup to the original template.

Inside dataobject or global dataobject extension

  public function ExtraTemplateHTML($position) {
    $html = null;

    foreach($this->owner->extend('updateExtraTemplateHTML') as $positionBlocks) {
      if(isset($positionBlocks[$position])) {
        foreach($positionBlocks[$position] as $htmlBlock) {
          $html .= $htmlBlock->getValue();
        }
      }
    }

    return $html;
  }

Inside the specific dataobject extension

  public function updateExtraTemplateHTML($htmlBlocks) {
    $viewer = new SSViewer(__CLASS__);
    $html = $viewer->process($this->owner);
    $htmlBlocks['bottom'][] = $html;

    $topHtml = HTMLText::create();
    $topHtml->setValue(123);
    $htmlBlocks['top'][] = $topHtml;

    return $htmlBlocks;
  }

Original Template

$ExtraTemplateHTML(top)
...
...
$ExtraTemplateHTML(bottom)

Extension Template

Than just write a new template for your extension with the content you would like to add.

0
votes

You cannot partially change a template, you can only substitute the template with another. So, keeping this in mind you should keep modular architecture with many logical includes.

Another possible way to extend existing content is using DOM and javascript. But you should think about side effects. For example if you add extra textual content then it won't be visible by crawlers and will effect your SEO. But for decorative enhancements, like adding extra coloured dot, this approach will work.