<?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/"
	>

<channel>
	<title>NemoPS &#187; page</title>
	<atom:link href="http://nemops.com/tag/page/feed/" rel="self" type="application/rss+xml" />
	<link>http://nemops.com</link>
	<description>Prestashop Tutorials, Modules and More!</description>
	<lastBuildDate>Wed, 05 Dec 2018 13:25:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.35</generator>
	<item>
		<title>Target specific pages from a Prestashop Module&#8217;s Hook</title>
		<link>http://nemops.com/page-specific-prestashop-module/</link>
		<comments>http://nemops.com/page-specific-prestashop-module/#comments</comments>
		<pubDate>Mon, 17 Jun 2013 09:49:30 +0000</pubDate>
		<dc:creator><![CDATA[Nemo]]></dc:creator>
				<category><![CDATA[Tips n Tricks]]></category>
		<category><![CDATA[hook]]></category>
		<category><![CDATA[module]]></category>
		<category><![CDATA[page]]></category>

		<guid isPermaLink="false">http://nemops.com/?p=1361</guid>
		<description><![CDATA[<p>You can easily restrict a Prestashop module&#8217;s display by adding exceptions in the back office positions. However, if you automate the process, there is a small trick that can be used when creating a module&#8217;s hook, which will also open a whole set of new possibilities to display page-specific content. The php_self context variable Each [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://nemops.com/page-specific-prestashop-module/">Target specific pages from a Prestashop Module&#8217;s Hook</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>You can easily restrict a Prestashop module&#8217;s display by adding exceptions in the back office positions. However, if you automate the process, there is a small trick that can be used when creating a module&#8217;s hook, which will also open a whole set of new possibilities to display page-specific content.</p>
<p><span id="more-1361"></span></p>
<h2>The php_self context variable</h2>
<p>Each Prestashop page has its own name, setup in the appropriate controller. It&#8217;s the body id assigned to a page as the $page_name variable in smarty. However, if you tried to get the page name from within a module, you might have noticed it&#8217;s only assigned to smarty. So is there a way to target each specific page from within a module? Yes, indeed. Here is the code:</p>
<pre class="brush: php; title: ; notranslate">
$this-&gt;context-&gt;controller-&gt;php_self
</pre>
<p>It can be really useful if you only want to <strong>target specific pages within a module</strong>. Let&#8217;s see a practical example.</p>
<h2>Target specific pages from a module&#8217;s hook</h2>
<p>Say, for the only sake of this example, that we only want to display a top of pages module if the current page is the homepage. Let&#8217;s use blocksearch.</p>
<p>In this case, We would only need to edit blocksearch.php, locate the <strong>hookTop</strong> method:</p>
<pre class="brush: php; title: ; notranslate">

	public function hookTop($params)
	{
		if (!$this-&gt;isCached('blocksearch-top.tpl', $this-&gt;getCacheId('blocksearch-top')))
		{
			$this-&gt;calculHookCommon($params);
			$this-&gt;smarty-&gt;assign('blocksearch_type', 'top');
		}

		return $this-&gt;display(__FILE__, 'blocksearch-top.tpl', $this-&gt;getCacheId('blocksearch-top'));
	}
</pre>
<p>And add this at the beginning of the function:</p>
<pre class="brush: php; title: ; notranslate">
if('index' != $this-&gt;context-&gt;controller-&gt;php_self)
	return false;
</pre>
<p>We a re simply telling it: if the current page is not the homepage, kill the module so it doesn&#8217;t display anything.</p>
<p>Just think at the possibilities at this point: <strong>you can display different content, and do diffrent things from the same hook, depending on the current page.</strong></p>
<p>How to? First, decide the pages that need specific behaviors. Use <strong>var_dump($this->context->controller->php_self)</strong> to check each page&#8217;s exact name. Then simply add a switch or if operator, that checks the current page name and acts properly each time, like:</p>
<pre class="brush: php; title: ; notranslate">
if('index' == $this-&gt;context-&gt;controller-&gt;php_self)
	// Do something and display a template for the homepage
else if('category' == $this-&gt;context-&gt;controller-&gt;php_self)
	// Do something and display a template if we are in the category page
else // not in any of the 2 pages above
	return false: // don't display anything
</pre>
<p>The post <a rel="nofollow" href="http://nemops.com/page-specific-prestashop-module/">Target specific pages from a Prestashop Module&#8217;s Hook</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nemops.com/page-specific-prestashop-module/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
