How to use loops with Mustache.php? - mustache.php

I need to loop as foreach() with my array,
$input = array (
1 => array ( 'year' => '1534', 'name' => 'test1', ),
2 => array ( 'year' => '1644', 'day' => 'test2' )
3 => array ( 'year' => '2015', 'day' => 'test3', ),
// ...
);
$m->render( $template, $input );
but can't ref without a "rooot key"... This was the first problem... Then I sulve using $input = array('list'=>$input); and ok, now list key exist to
{#list} test {/list}
but it not loops (!), it shows "test" once...

I think the problem is inside the $input array. You shouldn't use the numeric keys. So try changing the array from
$input = array (
1 => array ( 'year' => '1534', 'name' => 'test1', ),
2 => array ( 'year' => '1644', 'day' => 'test2' )
3 => array ( 'year' => '2015', 'day' => 'test3', ),
);
to
$input = array (
array ( 'year' => '1534', 'name' => 'test1', ),
array ( 'year' => '1644', 'day' => 'test2' )
array ( 'year' => '2015', 'day' => 'test3', ),
);
This is a my example, a little bit different from your code:
Mustache_Autoloader::register();
$oMustache = new Mustache_Engine( array(
'loader' => new Mustache_Loader_FilesystemLoader( 'templates' ),
));
$aVariables = array(
'list' => array(
array( 'value' => 'one' ),
array( 'value' => 'two' ),
array( 'value' => 'three' ),
)
);
$template = $oMustache->loadTemplate( 'my_template_name' );
return $template->render( $aVariables );
And this is the mustache template:
{{#list}}
test {{value}} <br/>
{{/list}}

Related

How to select multiple check box in edit grid section of magento

http://magento.localhost.com/index.php/arithmetic/adminhtml_arithmetic/edit/id/5/key/c03c12d4c338a2e4cdbb93c3d9e511a93401d19b21a13ea77cffda20cac94577/
This is what my link looks like. I am getting all values by the ID, in the edit grid page
there is a section for multiple check boxes. How can I select all the check boxes according to the result array
$fieldset-> addField('st_user_interest', 'checkboxes', array(
'label' => Mage::helper('arithmetic')->__('Interest'),
'required' => true,
'name' => 'st_user_interest[]',
'values' => array(
array(
'label' => Mage::helper('arithmetic')->__('Education'),
'value' => 'education',
'class' => 'required-one',
),
array(
'label' => Mage::helper('arithmetic')->__('Business'),
'value' => 'business',
'class' => 'required-one',
),
array(
'label' => Mage::helper('arithmetic')->__('Marketing'),
'value' => 'marketing',
'class' => 'required-one',
),
array(
'value' => 'investment',
'label' => Mage::helper('arithmetic')->__('Investment'),
'class' => 'required-one',
)
),
));
Thanks
Hi at the time of storing array field value we are storing as string after converting array to string,
So at the time of setValues() Magento looking for that same input field value as array to check the check boxes
Trick is that convert that stored string value into array and assign to that column field that will work
Package_Arithmetic_Block_Adminhtml_Arithmetic_Edit_Tab_Form
protected function _prepareForm()
{
$id = $this->htmlEscape(Mage::registry('arithmetic_data')->getIn_user_id());
$model = Mage::getModel("arithmeti/newuser")->load($id);
/* here is the stuff witch converting the stored string to array and set in st_user_interest */
$interest = $model->getSt_user_interest();
$model->setSt_user_interest(explode(',',$interest));
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('arithmetic_form', array('legend'=>Mage::helper('arithmetic')->__('User information')));
$fieldset-> addField('st_user_interest', 'checkboxes', array(
'label' => Mage::helper('arithmetic')->__('Interest'),
'required' => true,
'name' => 'st_user_interest[]',
'values' => array(
array(
'label' => Mage::helper('arithmetic')->__('Education'),
'value' => 'education',
),
array(
'label' => Mage::helper('arithmetic')->__('Business'),
'value' => 'business',
),
array(
'label' => Mage::helper('arithmetic')->__('Marketing'),
'value' => 'marketing',
),
array(
'value' => 'investment',
'label' => Mage::helper('arithmetic')->__('Investment'),
)
),
));
if ( Mage::getSingleton('adminhtml/session')->getArithmeticData() )
{
$form->setValues(Mage::getSingleton('adminhtml/session')->getArithmeticData());
Mage::getSingleton('adminhtml/session')->setArithmeticData(null);
} elseif ( $model->getData() ) {
//Mage::registry('arithmetic_data')->getData(); /* removing this line and adding $model->getData() inslde the $form->setValues() */
$form->setValues($model->getData());
}
return parent::_prepareForm();
}

ZF2 Doctrine2 Pagination Cache

Does anybody of you know how to configure cache for zf2 pagination using doctrine2?
The way i dot it:
$cache = \Zend\Cache\StorageFactory::factory (
array (
'adapter' => array (
'name' => 'Filesystem',
'options' => array (
'ttl' => 10,
'cache_dir' =>__DIR__.'/../../../data/cache/',
),
'plugins' => array (
'serializer' => array(
'serializer' => 'igbinary'
)
)
)
) );
return $cache;
But i get Exceptions like the following.
\Zend\Cache\Storage\Adapter\Filesystem.php:1549
Message:
fwrite() expects parameter 2 to be string, object given
\Zend\Cache\Storage\Adapter\Filesystem.php:1553
Message:
Error writing file '...\data\cache\zfcache-c0\zfcache-Zend_Paginator_2_722b98872575050f0443b2b626605650.dat'
\Zend\Paginator\Paginator.php:637
Message:
Error producing an iterator
The nesting was incorrect:
$cache = \Zend\Cache\StorageFactory::factory( array(
'adapter' => array(
'name' => 'Filesystem',
'options' => array (
'ttl' => 10,
'cache_dir' =>__DIR__.'/../../../data/cache/',
),
),
'plugins' => array (
'serializer' => array(
'serializer' => 'igbinary'
)
)
) );
return $cache;

PHPUnit mocks - assert method called

i'm new to phpunit and have read the documentation on mock objects but it isn't very clear.
I am trying to write a simple test that asserts a method within a class is called. With the following code, i am testing that when the Client::exchangeArray is called, a call is made to Client::getInputFilter.
class Client implements InputFilterAwareInterface
{
public function getInputFilter() {
if(!$this->_inputFilter){
$inputFactory = new InputFactory();
$inputFilter = new InputFilter();
$inputFilter->add($inputFactory->createInput(array(
'name' => 'id',
'required' => true,
'filters' => array(
array(
'name' => 'Int'
)
)
)));
$inputFilter->add($inputFactory->createInput(array(
'name' => 'name',
'required' => true,
'filters' => array(
array(
'name' => 'StripTags'
),
array(
'name' => 'StringTrim'
),
array(
'name' => 'StripNewLines'
),
array(
'name' => 'Alpha'
)
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 100
)
)
)
)));
$inputFilter->add($inputFactory->createInput(array(
'name' => 'surname',
'required' => true,
'filters' => array(
array(
'name' => 'StripTags'
),
array(
'name' => 'StringTrim'
)
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 100
)
)
)
)));
$inputFilter->add($inputFactory->createInput(array(
'name' => 'email',
'required' => false,
'filters' => array(
array(
'name' => 'StripTags'
),
array(
'name' => 'StringTrim'
)
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 150
)
),
array(
'name' => 'EmailAddress'
)
)
)));
$this->_inputFilter = $inputFilter;
}
return $this->_inputFilter;
}
public function exchangeArray($data){
$inputFilter = $this->getInputFilter();
$inputFilter->setData($data);
if(!$inputFilter->isValid()){
throw new \Exception('Invalid client data');
}
$cleanValues = $inputFilter->getValues();
$this->_id = (isset($cleanValues['id']) ? $cleanValues['id'] : null);
$this->_name = (isset($cleanValues['name']) ? $cleanValues['name'] : null);
$this->_surname = (isset($cleanValues['surname']) ? $cleanValues['surname'] : null);
$this->_email = (isset($cleanValues['email']) ? $cleanValues['email'] : null);
}
}
Here is my test case:
public function testExchangeArrayCallsInputFilter(){
$data = array('id' => 54,
'name' => 'john',
'surname' => 'doe',
'email' => 'john.doe#domain.com'
);
$mock = $this->getMock('Client', array('exchangeArray'));
$mock->expects($this->once())
->method('getInputFilter');
$mock->exchangeArray($data);
}
...and i'm getting the following error:
Expectation failed for method name is equal to
when invoked 1 time(s). Method was expected to be called 1 times,
actually called 0 times.
Where am i going wrong?
It all depends on what you want test and what you want mock. Basing on the name of your test I assume that you want test exchangeArray method.
The getMock method takes as second argument names of methods that you want mock. It means that they will never be called.
So, if you want to test exchangeArray method and mock getInputFilter you should pass "getInputFilter" in second argument, like below:
$mock = $this->getMock('Client', array('getInputFilter'));
$mock->expects($this->once())
->method('getInputFilter');
$mock->exchangeArray($data);
But be careful. You didn't tell your mock to return anything, so it will return null value. That means that you'll get a fatal error on the second line of exchangeArray method (trying to call a method on a non-object). You should prepare some faked filter object to deal with that, eg:
// $preparedFilterObject = ...
$mock = $this->getMock('Client', array('getInputFilter'));
$mock->expects($this->once())
->method('getInputFilter')
->will($this->returnValue($preparedFilterObject);
$mock->exchangeArray($data);
And if you want to invoke the "real" getInputFilter method - then you just can't mock this method.

Cakephp using the returned array values

I guess this should be smple, but I have been stuck at this for a while so I need help. I have a cakephp application and I am getting the following array:
chratSortedMean= array(
'totalAucFourHour' => array(
'Screening Visit-1 (scr)' => array(
'MmttFourthHour' => array(
'totalAucs' => array(
(int) 0 => (float) 0,
(int) 1 => (float) 220
)
)
),
'Month12' => array(
'MmttFourthHour' => array(
'totalAucs' => array(
(int) 0 => (float) 263.25,
(int) 1 => (float) 16.25
)
)
)
),
'totalAucTwoHour' => array(
'Day0' => array(
'MmttSecondHour' => array(
'totalAucs' => array(
(int) 0 => (float) 0,
(int) 1 => (float) 112.125
)
)
),
'DayD2' => array(
'MmttSecondHour' => array(
'totalAucs' => array(
(int) 0 => (float) 97.375,
(int) 1 => (float) 4.5
)
)
),
'Month3' => array(
'MmttSecondHour' => array(
'totalAucs' => array(
(int) 0 => (float) 8.53125,
(int) 1 => (float) 8.75
)
)
)
)
)
I want to calculate the mean of the totalAucs values for each of the timepoints(Screening Visit-1 (scr) , Month12, Day0 etc.) separately for the totalAucFourHour and totalAucTwoHour individually.
So the result i want is somewhat in the following form:
'totalAucFourHour' => array(
'Screening Visit-1 (scr)' => array(
'MmttFourthHour' => array(
'totalAucMean' => (float) 110
)
)
),
'Month12' => array(
'MmttFourthHour' => array(
'totalAucMean' => (float) 139.75
)
)
)
),
and so on. I do not want to use multiple for loops as i expect the data to grow in the future.
CakePHP has Core Utility Classes one of which is named Set. Set is the array manipulation
Class. You can call it from anywhere in your app with:
Set::methodNane();
What you need are the extract(); or classicExtract(); methods. Usage is fairly simple:
$Day0path = '/chratSortedMean/totalAucTwoHour/Day0/MmttSecondHour'
$arrayOfDay0s = Set::extract($Day0path, $data);
Check the other possibilities at the Cake Book. Also since CakePHP 2.2 there is a new array manipulation class Hash and it is faster. Since that version Set is actually deprecated but it will not be removed up until Cake 3.0.

Magento, populating checkboxes fields on an admin edit form

I have a form page where I am using
$form->addField('name', 'checkboxes', array('label' => 'check', 'name' => 'name[]',
'values' => array(
array('value'=>'1', 'label'=>'1'),
array('value'=>'2', 'label'=>'2'),
array('value'=>'3', 'label'=>'3'),
array('value'=>'4', 'label'=>'4'),
array('value'=>'5', 'label'=>'5'),
)
));
to create a list of checkboxes.
The problem is I can't figure out how to get them to populate when editing. Can anyone tell me how to do this?
I am using the checkboxes type so they display as a list instead of in separate rows in the form. If there is a way to create them as separate fields but all in one row I would love to know how.
$form->addField('name', 'checkboxes', array('label' => 'check', 'name' => 'name[]',
'values' => array(
array('value'=>'1', 'label'=>'1'),
array('value'=>'2', 'label'=>'2'),
array('value'=>'3', 'label'=>'3'),
array('value'=>'4', 'label'=>'4'),
array('value'=>'5', 'label'=>'5'),
),
'value' => array('1', '5'),
// or
// 'checked' => array('1', '5')
));
Then checkboxes with values "1" and "5" will be checked. For more details you can check lib/Varien/Data/Form/Element/Checkboxes.php
I also attached code or you can follow below link for more help
http://pastebin.com/hKMmryE9
Magento, populating checkboxes fields on an admin edit form
$form->addField('name', 'checkboxes', array('label' => 'check', 'name' => 'name[]',
'values' => array(
array('value'=>'1', 'label'=>'1'),
array('value'=>'2', 'label'=>'2'),
array('value'=>'3', 'label'=>'3'),
array('value'=>'4', 'label'=>'4'),
array('value'=>'5', 'label'=>'5'),
)
));
$form->addField('name', 'checkboxes', array('label' => 'check', 'name' => 'name[]',
'values' => array(
array('value'=>'1', 'label'=>'1'),
array('value'=>'2', 'label'=>'2'),
array('value'=>'3', 'label'=>'3'),
array('value'=>'4', 'label'=>'4'),
array('value'=>'5', 'label'=>'5'),
),
'value' => array('1', '5'),
// or
// 'checked' => array('1', '5')
));
Little improved and verified:
$fieldset->addField('payment_methods', 'checkboxes', array('label' => 'Payment Methods', 'name' => 'payment_methods[]',
'values' => array(
array('value'=>'1', 'label'=>'Cash'),
array('value'=>'2', 'label'=>'Paypal'),
array('value'=>'3', 'label'=>'Authorize.Net'),
array('value'=>'4', 'label'=>'Square'),
),
'required' => true,
'checked' => array('1','4'),
'disabled' => array('1'), ////if you want
));
Create $array like below
Array
(
[0] => Array
(
[value] => 1
[label] => Value 1
)
[1] => Array
(
[value] => 2
[label] => Value 2
)
[2] => Array
(
[value] => 3
[label] => Value 3
)
[3] => Array
(
[value] => 4
[label] => Value 4
)
[4] => Array
(
[value] => 5
[label] => Value 5
)
)
$fieldset->addField('checkboxes', 'checkboxes', array(
'label' => 'Select Value',
'name' => 'checkboxes[]',
'values' => $array,
'onclick' => "",
'onchange' => "",
'disabled' => false,
'after_element_html' => '',
'tabindex' => 1
));

Resources