Passing array from one template to another in magento2Passing an array from block file to template fileMagento 2 Get product collection in a custom template blockMagento2 Add to Cart from homepage not workingMagento 2 disabling product by custom attributeMagento 2 - Will x-magento-init tag apply from a html template?Magento 2: How to pass variable to template from another template?Magento2: How to convert JSON to Array of nested objectsHow to remove products linked to another store view?Code to Display data on page coming in form of array?Magento 2 : keep disable product in cart
If it isn't [someone's name]!
Unconventional examples of mathematical modelling
Have made several mistakes during the course of my PhD. Can't help but feel resentment. Can I get some advice about how to move forward?
Why can't I see 1861 / 1871 census entries on Freecen website when I can see them on Ancestry website?
How do I cope with haze for the photos containing sky and trees at a distance?
What happened after the end of the Truman Show?
What is the opposite of "hunger level"?
Heyawacky: Ace of Cups
Compute the square root of a positive integer using binary search
Why do aircraft leave cruising altitude long before landing just to circle?
Regression when x and y each have uncertainties
Is this bar slide trick shown on Cheers real or a visual effect?
Vegetarian dishes on Russian trains (European part)
Do predators tend to have vertical slit pupils versus horizontal for prey animals?
Check disk usage of files returned with spaces
Combinatorial Argument for Exponential and Logarithmic Function Being Inverse
Meaning and structure of headline "Hair it is: A List of ..."
A reccomended structured approach to self studying music theory for songwriting
Are there any OR challenges that are similar to kaggle's competitions?
Can anybody tell me who this Pokemon is?
Did Michelle Obama have a staff of 23; and Melania have a staff of 4?
Eric Andre had a dream
Adding things to bunches of things vs multiplication
Are there any rules on how characters go from 0th to 1st level in a class?
Passing array from one template to another in magento2
Passing an array from block file to template fileMagento 2 Get product collection in a custom template blockMagento2 Add to Cart from homepage not workingMagento 2 disabling product by custom attributeMagento 2 - Will x-magento-init tag apply from a html template?Magento 2: How to pass variable to template from another template?Magento2: How to convert JSON to Array of nested objectsHow to remove products linked to another store view?Code to Display data on page coming in form of array?Magento 2 : keep disable product in cart
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have used two template for homepage(template inside another template)
I am calling product template inside category template.
Now my case scenario is
1. I am declaring one array in category template
2. I want to pass this empty array to product template.
3. Add elements in that array in product template.
4. Pass the array back to category template.
How can i do that.
Let me know if there is any confusion.
magento2 array
add a comment |
I have used two template for homepage(template inside another template)
I am calling product template inside category template.
Now my case scenario is
1. I am declaring one array in category template
2. I want to pass this empty array to product template.
3. Add elements in that array in product template.
4. Pass the array back to category template.
How can i do that.
Let me know if there is any confusion.
magento2 array
1
You can add these templates inside one block and then you can write your logic in a single block class, You can access the function from both the templates.
– Mitro
Aug 6 at 10:23
Can u give me example through code?
– Nafisa
Aug 6 at 10:32
Can you tell me your templates rendering code So that I can have an idea what you actually want to do?
– Mitro
Aug 6 at 10:36
Is there any way to access variable declared in one phtml file into another?
– Nafisa
Aug 6 at 10:58
add a comment |
I have used two template for homepage(template inside another template)
I am calling product template inside category template.
Now my case scenario is
1. I am declaring one array in category template
2. I want to pass this empty array to product template.
3. Add elements in that array in product template.
4. Pass the array back to category template.
How can i do that.
Let me know if there is any confusion.
magento2 array
I have used two template for homepage(template inside another template)
I am calling product template inside category template.
Now my case scenario is
1. I am declaring one array in category template
2. I want to pass this empty array to product template.
3. Add elements in that array in product template.
4. Pass the array back to category template.
How can i do that.
Let me know if there is any confusion.
magento2 array
magento2 array
edited Aug 6 at 10:42
Rk Rathod
3,0384 silver badges26 bronze badges
3,0384 silver badges26 bronze badges
asked Aug 6 at 10:19
NafisaNafisa
927 bronze badges
927 bronze badges
1
You can add these templates inside one block and then you can write your logic in a single block class, You can access the function from both the templates.
– Mitro
Aug 6 at 10:23
Can u give me example through code?
– Nafisa
Aug 6 at 10:32
Can you tell me your templates rendering code So that I can have an idea what you actually want to do?
– Mitro
Aug 6 at 10:36
Is there any way to access variable declared in one phtml file into another?
– Nafisa
Aug 6 at 10:58
add a comment |
1
You can add these templates inside one block and then you can write your logic in a single block class, You can access the function from both the templates.
– Mitro
Aug 6 at 10:23
Can u give me example through code?
– Nafisa
Aug 6 at 10:32
Can you tell me your templates rendering code So that I can have an idea what you actually want to do?
– Mitro
Aug 6 at 10:36
Is there any way to access variable declared in one phtml file into another?
– Nafisa
Aug 6 at 10:58
1
1
You can add these templates inside one block and then you can write your logic in a single block class, You can access the function from both the templates.
– Mitro
Aug 6 at 10:23
You can add these templates inside one block and then you can write your logic in a single block class, You can access the function from both the templates.
– Mitro
Aug 6 at 10:23
Can u give me example through code?
– Nafisa
Aug 6 at 10:32
Can u give me example through code?
– Nafisa
Aug 6 at 10:32
Can you tell me your templates rendering code So that I can have an idea what you actually want to do?
– Mitro
Aug 6 at 10:36
Can you tell me your templates rendering code So that I can have an idea what you actually want to do?
– Mitro
Aug 6 at 10:36
Is there any way to access variable declared in one phtml file into another?
– Nafisa
Aug 6 at 10:58
Is there any way to access variable declared in one phtml file into another?
– Nafisa
Aug 6 at 10:58
add a comment |
2 Answers
2
active
oldest
votes
Try this it's worked for me.
<?php echo $this->getLayout()->createBlock("MagentoFrameworkViewElementTemplate")->setKey('test')->setTemplate("Vendor_Module::test.phtml")->toHtml(); ?>
to get value use
echo $this->getKey();
add a comment |
As per your actual requirement, in first phtml file add below code :
$arr=array(23,4);
echo $this->getLayout()->createBlock("VendorModulenameBlockDisplay")->setData('var1', $arr)->setTemplate("Vendor_Modulename::test2.phtml")->toHtml();
In second phtml i.e. test2.phtml add
$catIds=$block->getData("var1");
$arr=array();
foreach($catIds as $catId)
$_productCollection=$block->setCustomValue($catId);
foreach ($_productCollection as $_product)
$currentId=$_product->getId();
if (!in_array($currentId, $arr))
echo $_product->getData("name");echo "<br/>";
$arr[]=$_product->getId();
In app/code/Vendor/Modulename/Block/Display.php
add below code:
protected $_productCollectionFactory;
protected $_categoryFactory;
public function __construct(
MagentoCatalogModelResourceModelProductCollectionFactory
$productCollectionFactory,
MagentoCatalogModelCategoryFactory $categoryFactory,
MagentoFrameworkViewElementTemplateContext $context
)
$this->_categoryFactory = $categoryFactory;
$this->_productCollectionFactory = $productCollectionFactory;
parent::__construct($context);
public function setCustomValue($value)
$categoryId = $value;
$category = $this->_categoryFactory->create()->load($categoryId);
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addCategoryFilter($category);
$collection->addAttributeToFilter('visibility', MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$collection->addAttributeToFilter('status', MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
return $collection;
what if i want to fetch this value in another phtml?
– Nafisa
Aug 6 at 11:44
To fetch value in phtml, use this code: print_r($block->getData("var1"));
– Mona Mehta
Aug 6 at 11:49
Thanx. This worked. now how can i append more values in this array in other phtml itself. inshort how can i append in this $block->getData("var1")
– Nafisa
Aug 6 at 11:59
Do you mean that you want to pass this value again to another phtml? If so, then you can directly pass the variable to required phtml
– Mona Mehta
Aug 6 at 12:02
i want to append more elements in this array and then again pass this array back to the first phtml file
– Nafisa
Aug 6 at 12:03
|
show 5 more comments
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f284550%2fpassing-array-from-one-template-to-another-in-magento2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this it's worked for me.
<?php echo $this->getLayout()->createBlock("MagentoFrameworkViewElementTemplate")->setKey('test')->setTemplate("Vendor_Module::test.phtml")->toHtml(); ?>
to get value use
echo $this->getKey();
add a comment |
Try this it's worked for me.
<?php echo $this->getLayout()->createBlock("MagentoFrameworkViewElementTemplate")->setKey('test')->setTemplate("Vendor_Module::test.phtml")->toHtml(); ?>
to get value use
echo $this->getKey();
add a comment |
Try this it's worked for me.
<?php echo $this->getLayout()->createBlock("MagentoFrameworkViewElementTemplate")->setKey('test')->setTemplate("Vendor_Module::test.phtml")->toHtml(); ?>
to get value use
echo $this->getKey();
Try this it's worked for me.
<?php echo $this->getLayout()->createBlock("MagentoFrameworkViewElementTemplate")->setKey('test')->setTemplate("Vendor_Module::test.phtml")->toHtml(); ?>
to get value use
echo $this->getKey();
answered Aug 6 at 11:23
MitroMitro
3167 bronze badges
3167 bronze badges
add a comment |
add a comment |
As per your actual requirement, in first phtml file add below code :
$arr=array(23,4);
echo $this->getLayout()->createBlock("VendorModulenameBlockDisplay")->setData('var1', $arr)->setTemplate("Vendor_Modulename::test2.phtml")->toHtml();
In second phtml i.e. test2.phtml add
$catIds=$block->getData("var1");
$arr=array();
foreach($catIds as $catId)
$_productCollection=$block->setCustomValue($catId);
foreach ($_productCollection as $_product)
$currentId=$_product->getId();
if (!in_array($currentId, $arr))
echo $_product->getData("name");echo "<br/>";
$arr[]=$_product->getId();
In app/code/Vendor/Modulename/Block/Display.php
add below code:
protected $_productCollectionFactory;
protected $_categoryFactory;
public function __construct(
MagentoCatalogModelResourceModelProductCollectionFactory
$productCollectionFactory,
MagentoCatalogModelCategoryFactory $categoryFactory,
MagentoFrameworkViewElementTemplateContext $context
)
$this->_categoryFactory = $categoryFactory;
$this->_productCollectionFactory = $productCollectionFactory;
parent::__construct($context);
public function setCustomValue($value)
$categoryId = $value;
$category = $this->_categoryFactory->create()->load($categoryId);
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addCategoryFilter($category);
$collection->addAttributeToFilter('visibility', MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$collection->addAttributeToFilter('status', MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
return $collection;
what if i want to fetch this value in another phtml?
– Nafisa
Aug 6 at 11:44
To fetch value in phtml, use this code: print_r($block->getData("var1"));
– Mona Mehta
Aug 6 at 11:49
Thanx. This worked. now how can i append more values in this array in other phtml itself. inshort how can i append in this $block->getData("var1")
– Nafisa
Aug 6 at 11:59
Do you mean that you want to pass this value again to another phtml? If so, then you can directly pass the variable to required phtml
– Mona Mehta
Aug 6 at 12:02
i want to append more elements in this array and then again pass this array back to the first phtml file
– Nafisa
Aug 6 at 12:03
|
show 5 more comments
As per your actual requirement, in first phtml file add below code :
$arr=array(23,4);
echo $this->getLayout()->createBlock("VendorModulenameBlockDisplay")->setData('var1', $arr)->setTemplate("Vendor_Modulename::test2.phtml")->toHtml();
In second phtml i.e. test2.phtml add
$catIds=$block->getData("var1");
$arr=array();
foreach($catIds as $catId)
$_productCollection=$block->setCustomValue($catId);
foreach ($_productCollection as $_product)
$currentId=$_product->getId();
if (!in_array($currentId, $arr))
echo $_product->getData("name");echo "<br/>";
$arr[]=$_product->getId();
In app/code/Vendor/Modulename/Block/Display.php
add below code:
protected $_productCollectionFactory;
protected $_categoryFactory;
public function __construct(
MagentoCatalogModelResourceModelProductCollectionFactory
$productCollectionFactory,
MagentoCatalogModelCategoryFactory $categoryFactory,
MagentoFrameworkViewElementTemplateContext $context
)
$this->_categoryFactory = $categoryFactory;
$this->_productCollectionFactory = $productCollectionFactory;
parent::__construct($context);
public function setCustomValue($value)
$categoryId = $value;
$category = $this->_categoryFactory->create()->load($categoryId);
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addCategoryFilter($category);
$collection->addAttributeToFilter('visibility', MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$collection->addAttributeToFilter('status', MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
return $collection;
what if i want to fetch this value in another phtml?
– Nafisa
Aug 6 at 11:44
To fetch value in phtml, use this code: print_r($block->getData("var1"));
– Mona Mehta
Aug 6 at 11:49
Thanx. This worked. now how can i append more values in this array in other phtml itself. inshort how can i append in this $block->getData("var1")
– Nafisa
Aug 6 at 11:59
Do you mean that you want to pass this value again to another phtml? If so, then you can directly pass the variable to required phtml
– Mona Mehta
Aug 6 at 12:02
i want to append more elements in this array and then again pass this array back to the first phtml file
– Nafisa
Aug 6 at 12:03
|
show 5 more comments
As per your actual requirement, in first phtml file add below code :
$arr=array(23,4);
echo $this->getLayout()->createBlock("VendorModulenameBlockDisplay")->setData('var1', $arr)->setTemplate("Vendor_Modulename::test2.phtml")->toHtml();
In second phtml i.e. test2.phtml add
$catIds=$block->getData("var1");
$arr=array();
foreach($catIds as $catId)
$_productCollection=$block->setCustomValue($catId);
foreach ($_productCollection as $_product)
$currentId=$_product->getId();
if (!in_array($currentId, $arr))
echo $_product->getData("name");echo "<br/>";
$arr[]=$_product->getId();
In app/code/Vendor/Modulename/Block/Display.php
add below code:
protected $_productCollectionFactory;
protected $_categoryFactory;
public function __construct(
MagentoCatalogModelResourceModelProductCollectionFactory
$productCollectionFactory,
MagentoCatalogModelCategoryFactory $categoryFactory,
MagentoFrameworkViewElementTemplateContext $context
)
$this->_categoryFactory = $categoryFactory;
$this->_productCollectionFactory = $productCollectionFactory;
parent::__construct($context);
public function setCustomValue($value)
$categoryId = $value;
$category = $this->_categoryFactory->create()->load($categoryId);
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addCategoryFilter($category);
$collection->addAttributeToFilter('visibility', MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$collection->addAttributeToFilter('status', MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
return $collection;
As per your actual requirement, in first phtml file add below code :
$arr=array(23,4);
echo $this->getLayout()->createBlock("VendorModulenameBlockDisplay")->setData('var1', $arr)->setTemplate("Vendor_Modulename::test2.phtml")->toHtml();
In second phtml i.e. test2.phtml add
$catIds=$block->getData("var1");
$arr=array();
foreach($catIds as $catId)
$_productCollection=$block->setCustomValue($catId);
foreach ($_productCollection as $_product)
$currentId=$_product->getId();
if (!in_array($currentId, $arr))
echo $_product->getData("name");echo "<br/>";
$arr[]=$_product->getId();
In app/code/Vendor/Modulename/Block/Display.php
add below code:
protected $_productCollectionFactory;
protected $_categoryFactory;
public function __construct(
MagentoCatalogModelResourceModelProductCollectionFactory
$productCollectionFactory,
MagentoCatalogModelCategoryFactory $categoryFactory,
MagentoFrameworkViewElementTemplateContext $context
)
$this->_categoryFactory = $categoryFactory;
$this->_productCollectionFactory = $productCollectionFactory;
parent::__construct($context);
public function setCustomValue($value)
$categoryId = $value;
$category = $this->_categoryFactory->create()->load($categoryId);
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addCategoryFilter($category);
$collection->addAttributeToFilter('visibility', MagentoCatalogModelProductVisibility::VISIBILITY_BOTH);
$collection->addAttributeToFilter('status', MagentoCatalogModelProductAttributeSourceStatus::STATUS_ENABLED);
return $collection;
edited Aug 7 at 6:50
answered Aug 6 at 11:17
Mona MehtaMona Mehta
1057 bronze badges
1057 bronze badges
what if i want to fetch this value in another phtml?
– Nafisa
Aug 6 at 11:44
To fetch value in phtml, use this code: print_r($block->getData("var1"));
– Mona Mehta
Aug 6 at 11:49
Thanx. This worked. now how can i append more values in this array in other phtml itself. inshort how can i append in this $block->getData("var1")
– Nafisa
Aug 6 at 11:59
Do you mean that you want to pass this value again to another phtml? If so, then you can directly pass the variable to required phtml
– Mona Mehta
Aug 6 at 12:02
i want to append more elements in this array and then again pass this array back to the first phtml file
– Nafisa
Aug 6 at 12:03
|
show 5 more comments
what if i want to fetch this value in another phtml?
– Nafisa
Aug 6 at 11:44
To fetch value in phtml, use this code: print_r($block->getData("var1"));
– Mona Mehta
Aug 6 at 11:49
Thanx. This worked. now how can i append more values in this array in other phtml itself. inshort how can i append in this $block->getData("var1")
– Nafisa
Aug 6 at 11:59
Do you mean that you want to pass this value again to another phtml? If so, then you can directly pass the variable to required phtml
– Mona Mehta
Aug 6 at 12:02
i want to append more elements in this array and then again pass this array back to the first phtml file
– Nafisa
Aug 6 at 12:03
what if i want to fetch this value in another phtml?
– Nafisa
Aug 6 at 11:44
what if i want to fetch this value in another phtml?
– Nafisa
Aug 6 at 11:44
To fetch value in phtml, use this code: print_r($block->getData("var1"));
– Mona Mehta
Aug 6 at 11:49
To fetch value in phtml, use this code: print_r($block->getData("var1"));
– Mona Mehta
Aug 6 at 11:49
Thanx. This worked. now how can i append more values in this array in other phtml itself. inshort how can i append in this $block->getData("var1")
– Nafisa
Aug 6 at 11:59
Thanx. This worked. now how can i append more values in this array in other phtml itself. inshort how can i append in this $block->getData("var1")
– Nafisa
Aug 6 at 11:59
Do you mean that you want to pass this value again to another phtml? If so, then you can directly pass the variable to required phtml
– Mona Mehta
Aug 6 at 12:02
Do you mean that you want to pass this value again to another phtml? If so, then you can directly pass the variable to required phtml
– Mona Mehta
Aug 6 at 12:02
i want to append more elements in this array and then again pass this array back to the first phtml file
– Nafisa
Aug 6 at 12:03
i want to append more elements in this array and then again pass this array back to the first phtml file
– Nafisa
Aug 6 at 12:03
|
show 5 more comments
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f284550%2fpassing-array-from-one-template-to-another-in-magento2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You can add these templates inside one block and then you can write your logic in a single block class, You can access the function from both the templates.
– Mitro
Aug 6 at 10:23
Can u give me example through code?
– Nafisa
Aug 6 at 10:32
Can you tell me your templates rendering code So that I can have an idea what you actually want to do?
– Mitro
Aug 6 at 10:36
Is there any way to access variable declared in one phtml file into another?
– Nafisa
Aug 6 at 10:58