简体   繁体   中英

Magento: load template file from admin module

I've been following this tutorial on creating modules in the management section. I'm just expermenting with magento modules and the twitter API. Here is the tutorial http://t.wits.sg/2009/03/31/howto-repackageable-custom-extension-development-in-magento/

I got as far as page two where I have a module menu in management that calls on my AdminController's indexAction like so:

<?php
class Optimise_Twits_AdminController extends Mage_Adminhtml_Controller_Action
{
  public function indexAction()
  {
    $this->loadLayout();
    $this->getLayout()->getBlock('content')->append($this->getLayout()->
      createBlock('twits/helloWorld'));
    $this->renderLayout();
  }
}

So this displays the block/HelloWorld.php block:

<?php
class Optimise_Twits_Block_HelloWorld extends Mage_Core_Block_Template
{
  protected function _toHtml()
  {
    return 'Hello world';
  }
}
?>

THis all works fine, I can navigate to my management menu click on the menu item and I see 'HelloWorld'. What I want to do is load a template file here instead of displaying text through a class.

I have a form that I want to display that looks up all the products and displays them. I can then choose a product and write in tags and when I click the submit button on the form it formats the data and punts it over to twitter as a status update.

Am I going about this wrong? (should I not be using templates here?) if not can someone help me out with where to put my templates and how to call them.

This is what my phtml file will hopefully look like:

  <div class="simple_contact">
  <h1 class="cms">'Tweet up' Your Products</h1>
  <form id="twitter-feed" name="twitter-feed" action="[action_here]" 
     method="post">
  <table><tr>
  <?php
  $model = Mage::getModel("optimise_twits/products");
  $products = $model->getProducts();
  $i = 0;
  foreach ($products as $product)
  {
   // var_dump($product);
    echo '<tr>';
    echo '<td>';
    echo '<label for="'. $product .'">' . $product . '</label>';
    echo '<input type="hidden" name="tweet['.$i.'][product]" value="'. 
      $product .'">';
    echo '<br />';
    echo '<input type="text" class="hashtag" name="'.
      'tweet['.$i.'][tags]" id="tags" value="#enter, #product, #hastag"';
    echo '</td>';
    echo '<td>';
    echo '<input type="checkbox" name="tweet['.$i.'][chk]" id="'. 
       $product .'"></td>';
    echo '</tr>';
    $i++;
  }
?>
<tr>
   <td colspan="2"><input type="submit" name="submit" value="tweet"></td>
</tr>
</table>
</form>
</div>

Thanks a lot!

Jonesy

You can set the template on a block by calling it's setTemplate() method. But don't.

If you use a descended Mage_Adminhtml_Block_Widget_Grid it'll make the table for you, you only need to supply a collection class and describe some columns. But don't do that either.

Instead use the module creator . It will make the necessary grid widget (and container) for you and save hours/days of work. At least until you're experienced enough to do it yourself properly.

Yes, you're doing this right. While Magento has a class hierarchy for automatically creating form widgets, they suffer from the same "problem" that all tightly bound OO UI components do: Great if you want to do exactly what's done elsewhere in the app. time consuming if you want/need to heavily customize the functionality. It's worth learning how they work, but there's no reason you can't use Magento like you would a simpler PHP MVC system.

It's worth learning how every system in Magento works, but jumping into the grid whole-hog may just leave you more confused than when you started.

Moving to your specific question, every Block that inherits from Mage_Core_Block_Template has a " setTemplate " method. This method if used to tell Magento which phtml template you want a block to use. So, in your Block's constructor, something like

class Optimise_Twits_Block_HelloWorld extends Mage_Core_Block_Template
{
    protected function __construct()
    {
        $this->setTemplate('path/to/my/template.phtml');
    }   
}

You'll also notice we've removed the _toHtml method. The _toHtml in the base class ( Mage_Core_Block_Template ), is what loads your template. So if you override it, your block will no longer load and render a template.

The template path starts including from your theme's base template folder. So, if you were using the modern theme

/app/design/frontend/default/modern/template

You'd put your template at

/app/design/frontend/default/modern/template/path/to/my/template.phtml

(this is slightly simplified, but again, getting too tangled up into how the theming paths work might only get in the way at this point. Start tracing at Mage_Core_Block_Abstract::renderView for "the truth")

That should be enough to get you started. If you run into further problems just ask another, specific, question here and someone should be able to help. Working your way through the Knowledge Base is a great way to learn more about how all this works, as well as articles that go beyond the knowledge base (disclaimer: I'm the author of both)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM