Block with HTML elements

Block with HTML elements

Helson C -
回帖数:7

Hello, i created a block, but i dont know how i can add html elements within.

I need a some help...

回复Helson C

Re: Block with HTML elements

Darko Miletić -
Core developers的头像 Plugin developers的头像
回复Darko Miletić

Re: Block with HTML elements

Helson C -

Hello Darko, thanks for you reply!

I need create a form element within, i'm trying use html_witter::tag('form', ''); but its wont work.


回复Helson C

Re: Block with HTML elements

Darko Miletić -
Core developers的头像 Plugin developers的头像
Show us your current code in get_content method.
回复Darko Miletić

Re: Block with HTML elements

Helson C -

 public function get_content(){

        if ($this->content !== null){

            return $this->content;

        }

        

        $this->content = new stdClass();

        $this->content->items = array();

        $this->content->items[] = html_writer::tag('form','');

        $this->content->items[] = html_writer::tag('input', '');

        $this->content->items[] = html_writer::tag('button', 'Enviar');    

        return $this->content;

    }



Nothing happens...

I'm trying to use html_writter direct in $this->content->text = html_writter::tag('form','');

But it's won't work for FORM.


Sorry for my bad english.

回复Helson C

Re: Block with HTML elements

Davo Smith -
Core developers的头像 Particularly helpful Moodlers的头像 Peer reviewers的头像 Plugin developers的头像

Have a look at the HTML output by the block in your browser.

I suspect you will find something that looks like this:

<ul>
<li><form></form></li>
<li><input></input></li>
<li><button>Enviar</button></li>
</ul>

That is probably not what you are looking for - if you want to create a form, then you should consider using 'block_base', not 'block_list' and setting $this->content->text, instead of $this->content->items.

block_list is for (bulleted) lists of items

block_base is more sensible for a block that has a chunk of HTML inside it, rather than a list of items.

You should also make sure Debugging is on, as I suspect you might be have some warning messages (but I haven't checked).

回复Davo Smith

Re: Block with HTML elements

Helson C -
Davo, thank you!

I'm was missing to extend block_list...


My solution to this was:

class block_htmlsimples extends block_base{

    public function get_content(){

        if ($this->content !== null){

            return $this->content;

        }

        

        $this->content = new stdClass();

        $this->content->text = '<form method="post" action="">';

        $this->content->text .= "Nome: <input type='text'/>";

        $this->content->text .= html_writer::empty_tag('input', array('type' => 'submit', 'value' => 'Enviar'));

        $this->content->text .= "</form>";        

        return $this->content;

}

}

What do you think about this?



回复Helson C

Re: Block with HTML elements

Helson C -

Hello, i found a solution...


require_once ('myblock_form.php');


public function get_content(){

       $this->get_content->text = $mform->render();  //instead $mform->display();

       $this->content = new stdClass();

       $mform = new myblock_form('mymoodle.com/myblock/another_view.php');        

       return $this->content;

}


Thus, you can have two views and two form definitons, one for exhibition within block and another page to receiver parameters: post or get


Thanks!