<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>product-view &#8211; MaxVergelli.com</title>
	<atom:link href="https://www.maxvergelli.com/tag/product-view/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.maxvergelli.com</link>
	<description>sourcecode repository</description>
	<lastBuildDate>Sat, 11 Apr 2020 16:38:27 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://i2.wp.com/www.maxvergelli.com/wp-content/uploads/2020/04/wp-1586823685141.png?fit=32%2C32&#038;ssl=1</url>
	<title>product-view &#8211; MaxVergelli.com</title>
	<link>https://www.maxvergelli.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">34603511</site>	<item>
		<title>Access a variable declared in a phtml from another phtml in Magento2</title>
		<link>https://www.maxvergelli.com/access-a-variable-declared-in-a-phtml-from-another-phtml-in-magento2/</link>
					<comments>https://www.maxvergelli.com/access-a-variable-declared-in-a-phtml-from-another-phtml-in-magento2/#respond</comments>
		
		<dc:creator><![CDATA[Max]]></dc:creator>
		<pubDate>Tue, 14 Apr 2020 16:25:00 +0000</pubDate>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[block]]></category>
		<category><![CDATA[blocks]]></category>
		<category><![CDATA[catalog-product-view]]></category>
		<category><![CDATA[custom-block]]></category>
		<category><![CDATA[custom-module]]></category>
		<category><![CDATA[global-variable]]></category>
		<category><![CDATA[magento]]></category>
		<category><![CDATA[magento2]]></category>
		<category><![CDATA[module]]></category>
		<category><![CDATA[phtml]]></category>
		<category><![CDATA[product-view]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[template-system]]></category>
		<category><![CDATA[variable]]></category>
		<guid isPermaLink="false">https://www.maxvergelli.com/?p=237</guid>

					<description><![CDATA[Having two blocks in the catalog_product_view.xml file like this: To access a variable declared in the first block from the second block, you can set the variable with register from the first phtml, then you get the value in the &#8230; <a class="kt-excerpt-readmore" href="https://www.maxvergelli.com/access-a-variable-declared-in-a-phtml-from-another-phtml-in-magento2/" aria-label="Access a variable declared in a phtml from another phtml in Magento2">Read More</a>]]></description>
										<content:encoded><![CDATA[
<p>Having two blocks in the <code>catalog_product_view.xml</code> file like this:</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;xml&quot;,&quot;mime&quot;:&quot;application/xml&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;XML&quot;,&quot;modeName&quot;:&quot;xml&quot;}">&lt;block class=&quot;Magento\Catalog\Block\Product\View&quot; name=&quot;product.info.custom-product-attributes&quot; template=&quot;product/view/custom-product-attributes.phtml&quot; after=&quot;product.info.overview&quot;/&gt;
...
&lt;block class=&quot;Magento\Catalog\Block\Product\View&quot; name=&quot;product.info.custom-estimated-delivery&quot; template=&quot;product/view/custom-estimated-delivery.phtml&quot; after=&quot;product.price.final&quot;/&gt;</pre></div>



<p>To access a variable declared in the first block from the second block, you can set the variable with <code>register</code> from the first phtml, then you get the value in the 2nd phtml with <code>registry</code></p>



<p>To do things well without injecting direclty the objectManager in phtml, you have to create some function your product block, so in your block : <code>app/code/Vendor/Modulename/Block/Custom.php</code>assuming that your variable name is : <code>$tech</code> add the following code:</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}">&lt;?php
namespace Vendor\Modulename\Block;

class Custom extends \Magento\Framework\View\Element\Template
{
    protected $_coreRegistry;

    public function __construct(
        ...
        \Magento\Framework\Registry $coreRegistry
    ) {
        ...
        $this-&gt;_coreRegistry = $coreRegistry;
    }

    /**
     * Set var data
     */
    public function setTechData($data)
    {
        $this-&gt;_coreRegistry-&gt;register('tech', $data);
    }

    /**
     * Get var data
     */
    public function getTechData()
    {
        return $this-&gt;_coreRegistry-&gt;registry('tech');
    }
}</pre></div>



<p>Then in your First phtml :</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}">&lt;?php $tech = 'Your Data here'; ?&gt;
&lt;?php $block-&gt;setTechData($tech); ?&gt;</pre></div>



<p>Then, in the Second phtml :</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}">&lt;?php $block-&gt;getTechData(); ?&gt; //you'll get your tech value here</pre></div>



<p>Besides, you can also do that in phtml to <strong>test</strong> without the block solution, </p>



<p>First phtml :</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}">&lt;?php 
$tech = 'Your Data here';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objectManager-&gt;create(&quot;Magento\Framework\Registry&quot;)-&gt;register('tech', $tech);
?&gt;</pre></div>



<p>Second phtml :</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}">&lt;?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$techData = $objectManager-&gt;create(&quot;Magento\Framework\Registry&quot;)-&gt;registry('tech');
echo $techData;
?&gt;</pre></div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.maxvergelli.com/access-a-variable-declared-in-a-phtml-from-another-phtml-in-magento2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">237</post-id>	</item>
	</channel>
</rss>
