Skip to content

Commit

Permalink
Creación de un bloque
Browse files Browse the repository at this point in the history
Los bloques contienen la parte lógica que se emplea en las plantillas

Haremos uso de la vista previamente creada en el modulo de Pfay_Contacts
Mas información --> 45789fd

1.- Creamos nuestro bloque:
	app/code/Pfay/Contacts/Block/Contactslist.php
2.- Enlazamos nuestro bloque con nuestro template, para eso modificamos nuestro LAYOUT
3.- Modificamos nuestro template para hacer uso de los métodos de nuestro bloque.

4.- recompilamos y asignamos permisos
	php bin/magento setup:upgrade
	php bin/magento setup:di:compile
	chmod -R 777 var/ pub/

5.- verificamos resultados en el navegador WEB:
	http://magento240.local/contacts/test/index
  • Loading branch information
PerezContrerasLuis committed Jan 19, 2022
1 parent 45789fd commit c221740
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
23 changes: 23 additions & 0 deletions code/Pfay/Contacts/Block/Contactslist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace Pfay\Contacts\Block;
use Magento\Framework\View\Element\Template;

class Contactslist extends \Magento\Framework\View\Element\Template
{
public function __construct(Template\Context $context, array $data = array())
{
parent::__construct($context, $data);
$this->setData('contacts',array());
}

public function addContacts($count)
{
$_contacts = $this->getData('contacts');
$actualNumber = count($_contacts);
$names = array();
for($i=$actualNumber;$i<($actualNumber+$count);$i++) {
$_contacts[] = 'nom '.($i+1);
}
$this->setData('contacts',$_contacts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="contactForm" template="Pfay_Contacts::test_index.phtml"></block>
<block class="Pfay\Contacts\Block\Contactslist" name="contactForm" template="Pfay_Contacts::test_index.phtml"></block>
</referenceContainer>
</body>
</page>
45 changes: 44 additions & 1 deletion code/Pfay/Contacts/view/frontend/templates/test_index.phtml
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
<div id="contactslist">
my contact list
<h2>empty</h2>
<table>
<tr>
<th>Name</th>
</tr>
<?php $contacts = $this->getData('contacts'); ?>
<?php foreach ($contacts as $contact): ?>
<tr>
<td><?php echo $contact ?></td>
</tr>
<?php endforeach; ?>
<tr>
<td>there is no contact in this list</td>
</tr>
</table>


<h2>10 éléments</h2>
<table>
<tr>
<th>Name</th>
</tr>
<?php $this->addContacts(10); ?>
<?php $contacts = $this->getData('contacts'); ?>
<?php foreach ($contacts as $contact): ?>
<tr>
<td><?php echo $contact ?></td>
</tr>
<?php endforeach; ?>
</table>

<h2>20 éléments</h2>
<table>
<tr>
<th>Name</th>
</tr>
<?php $this->addContacts(10); ?>
<?php $contacts = $this->getData('contacts'); ?>
<?php foreach ($contacts as $contact): ?>
<tr>
<td><?php echo $contact ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>

0 comments on commit c221740

Please sign in to comment.