Magento

eCommerce Software for Online Growth

Magento Forum

From setting up your store to managing your products, pages and promotions to generating detailed reports, the Magento User Guide empowers the user to utilize the platform for all of its vast capacity.
Available in eBook and Print formats – Download Now!!!
   
How do I use PHP in CMS pages
 
DetailAddict
Jr. Member
 
Total Posts:  16
Joined:  2008-10-16
 

so I am able to call my php code without a problem but I need to pass in a parameter.  However, the parameter will not be hard coded.  how can I make value 1 and value 2 a dynamic variable?

{{block type="mayerwin_custom/test" my_param1="value 1" another_param="value 2"}}

 
Magento Community Magento Community
Magento Community
Magento Community
 
DetailAddict
Jr. Member
 
Total Posts:  16
Joined:  2008-10-16
 

alright, so I got the php thing to work very nicely and pretty much all my old code works.  So instead of returning $html, I just echo straight from my module code with echo.

however, in my php code, i generate bunch of links dynamically… it searches a particular folder, find image files, sort them, and display them in thumbnail with a link to a bigger resolution.  let say my link is like this:

http://www.detailaddict.com/default/portfolio?folder=AD/20080628/

how do I pass “AD/20080628” in my_param1 in the block code?

{{block type="mayerwin_custom/test" my_param1="value 1" another_param="value 2"}}

 
Magento Community Magento Community
Magento Community
Magento Community
 
Bhaskar Duvvuri
Jr. Member
 
Total Posts:  4
Joined:  2008-08-20
 

Try this:

$post $this->getRequest()->getParam('my_param1');
$html "Hello ".$post;

It will work.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Stress Head
Member
 
Total Posts:  32
Joined:  2008-08-28
 

Can anyone help me resolve this problem I have with this code please? After much messing around, I have a nice “Hello” message at the bottom of my homepage.

However, the idea was to place there the latest blog post from my Wordpress installation at the bottom of the page.

The problem I have is, if I use the code below in, it shows the latest post correctly, but ABOVE everything else on the page, not at the bottom.

<?php

class Mayerwin_Custom_Block_Test extends Mage_Core_Block_Abstract
{
  
protected function _toHtml()
  
{

 $lastposts 
get_posts('numberposts=1');
 foreach(
$lastposts as $post) :
    
setup_postdata($post);
 
?>
 
<h2><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></h2>
 
<?php the_content(); ?>
 <?php 
endforeach; ?>

<?php


     
// put here your custom PHP code with output in $html;
     // use arguments like $this->getMyParam1() , $this->getAnotherParam()

     
return $html;
  
}

?>
Having messed around, I realise it is the return $html; that is the problem, but I don’t know how to resolve it.

Any help would be great, thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
davidgrun
Sr. Member
 
Total Posts:  213
Joined:  2008-07-10
 

Hi. I got “hello world” working… how would I go about implementing this simple script on app/code/local/Mayerwin/Custom/Block/Test.php :

<?php

class Mayerwin_Custom_Block_Test extends Mage_Core_Block_Abstract
{
  
protected function _toHtml()
  
{
     
// put here your custom PHP code with output in $html;
     // use arguments like $this->getMyParam1() , $this->getAnotherParam()
    

///*

// START of "Frontpage Slideshow" settings
    
$nameOfSlideshowToDisplay "mydemoslideshow";                     // Enter the name of your slideshow. Slideshows are in folders inside /fpss/slideshows/.
    
$URLofyoursite "http://kehot.hhhost.net";                         // Enter your site's URL.
    
$AbsoluteServerPathofyoursite "/home/kehothhh/public_html/";        // Enter the root path of your site on the server.
    
    // do not edit below this line
    
include_once($AbsoluteServerPathofyoursite."/fpss/mod_fpslideshow.php");
// END of "Frontpage Slideshow" settings
//*/

    //    $body = $response->getRawBody();
    //    return $body;
    
    
$html "<h1>Hello world</h1>";
  return 
$html;
  
}
}

It gives me the following error (I’m not sure how I implement the code to return with $html:

NoticeUndefined variabletarget  in /home/kehothhh/public_html/fpss/mod_fpslideshow.php on line 72
Trace
:
#0 /home/kehothhh/public_html/fpss/mod_fpslideshow.php(72): mageCoreErrorHandler(8, 'Undefined varia...', '/home/kehothhh/...', 72, Array)
#1 /home/kehothhh/public_html/app/code/local/Mayerwin/Custom/Block/Test.php(19): include_once('/home/kehothhh/...')
#2 /home/kehothhh/public_html/app/code/core/Mage/Core/Block/Abstract.php(643): Mayerwin_Custom_Block_Test->_toHtml()
#3 /home/kehothhh/public_html/app/code/core/Mage/Core/Model/Email/Template/Filter.php(74): Mage_Core_Block_Abstract->toHtml()
#4 /home/kehothhh/public_html/lib/Varien/Filter/Template.php(128): Mage_Core_Model_Email_Template_Filter->blockDirective(Array)
#5 /home/kehothhh/public_html/app/code/core/Mage/Cms/Block/Page.php(80): Varien_Filter_Template->filter(Array, Array)
#6 /home/kehothhh/public_html/app/code/core/Mage/Core/Block/Abstract.php(643): Mage_Cms_Block_Page->_toHtml('

Con 25 a??os...') #7 /home/kehothhh/public_html/app/code/core/Mage/Core/Block/Text/List.php(43): Mage_Core_Block_Abstract->toHtml() #8 /home/kehothhh/public_html/app/code/core/Mage/Core/Block/Abstract.php(643): Mage_Core_Block_Text_List->_toHtml() #9 /home/kehothhh/public_html/app/code/core/Mage/Core/Block/Abstract.php(503): Mage_Core_Block_Abstract->toHtml() #10 /home/kehothhh/public_html/app/code/core/Mage/Core/Block/Abstract.php(454): Mage_Core_Block_Abstract->_getChildHtml() #11 /home/kehothhh/public_html/app/design/frontend/default/default/template/page/2columns-right.phtml(56): Mage_Core_Block_Abstract->getChildHtml('content', true) #12 /home/kehothhh/public_html/app/code/core/Mage/Core/Block/Template.php(144): include('/home/kehothhh/...') #13 /home/kehothhh/public_html/app/code/core/Mage/Core/Block/Template.php(176): Mage_Core_Block_Template->fetchView('content') #14 /home/kehothhh/public_html/app/code/core/Mage/Core/Block/Template.php(193): Mage_Core_Block_Template->renderView('frontend/defaul...') #15 /home/kehothhh/public_html/app/code/core/Mage/Core/Block/Abstract.php(643): Mage_Core_Block_Template->_toHtml() #16 /home/kehothhh/public_html/app/code/core/Mage/Core/Model/Layout.php(525): Mage_Core_Block_Abstract->toHtml() #17 /home/kehothhh/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(325): Mage_Core_Model_Layout->getOutput() #18 /home/kehothhh/public_html/app/code/core/Mage/Cms/Helper/Page.php(92): Mage_Core_Controller_Varien_Action->renderLayout() #19 /home/kehothhh/public_html/app/code/core/Mage/Cms/controllers/IndexController.php(32): Mage_Cms_Helper_Page->renderPage() #20 /home/kehothhh/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(348): Mage_Cms_IndexController->indexAction(Object(Mage_Cms_IndexController), 'home') #21 /home/kehothhh/public_html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(163): Mage_Core_Controller_Varien_Action->dispatch() #22 /home/kehothhh/public_html/app/code/core/Mage/Core/Controller/Varien/Front.php(174): Mage_Core_Controller_Varien_Router_Standard->match('index#23 /home/kehothhh/public_html/app/Mage.php(450): Mage_Core_Controller_Varien_Front->dispatch(Object(Mage_Core_Controller_Request_Http)) #24 /home/kehothhh/public_html/index.php(52): Mage::run() #25 {main}

THANKS!!

 Signature 

Magento Specialist - http://www.futureye-designs.com

 
Magento Community Magento Community
Magento Community
Magento Community
 
vgration
Member
 
Total Posts:  53
Joined:  2008-07-09
USA Arkansas
 

I build a nice extension based on this thread and now its stopped working. Has something changed in magento?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ravikumar N B
Sr. Member
 
Avatar
Total Posts:  213
Joined:  2009-01-12
 
nikefido - 24 July 2008 01:09 PM

Moshe - 15 March 2008 04:26 PM
Say, you want to run custom PHP code that can accept arguments.

1. declare your module: app/etc/modules/Mayerwin.xml
//code
3. Create your custom PHP code: app/code/local/Mayerwin/Custom/Block/Test.php
//code
4. Use your custom PHP logic in CMS page/block:
//code

Thanks, this is VERY useful - however, I can’t get it to work!
rather than doing this within the CMS content, using something like:

{{block type="myCompany_Events/Events" category_id="5" template="catalog/product/eventview.phtml" }}

I am using the “Layout update XML” so I have this entry:

<reference name="right">
<
block type="myCompany_Leftbar/Leftbar" category="3" template="leftbar/left.phtml" />
</
reference>

Can I not pass a variable using this method??

Hi have same issue.

give some idea to fix this!!!!

 Signature 

Magento 1.2.0.3
Php 5.2.6

 
Magento Community Magento Community
Magento Community
Magento Community
 
bmartus
Sr. Member
 
Total Posts:  124
Joined:  2008-05-22
Greenville, TX
 

Same here .. need to pass variables via the Custom Layout XML, if possible.

 Signature 

Brandon Martus

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ravikumar N B
Sr. Member
 
Avatar
Total Posts:  213
Joined:  2009-01-12
 

!!****!!BUMP!!****!!

 Signature 

Magento 1.2.0.3
Php 5.2.6

 
Magento Community Magento Community
Magento Community
Magento Community
 
davidgrun
Sr. Member
 
Total Posts:  213
Joined:  2008-07-10
 

Is Moshe’s solution still working in Magento 1.3.2.1????

 Signature 

Magento Specialist - http://www.futureye-designs.com

 
Magento Community Magento Community
Magento Community
Magento Community
 
DetailAddict
Jr. Member
 
Total Posts:  16
Joined:  2008-10-16
 

just upgraded to the latest and my code is still working! smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
davidgrun
Sr. Member
 
Total Posts:  213
Joined:  2008-07-10
 

can I put JS in this function too?

 Signature 

Magento Specialist - http://www.futureye-designs.com

 
Magento Community Magento Community
Magento Community
Magento Community
 
davidgrun
Sr. Member
 
Total Posts:  213
Joined:  2008-07-10
 

Hello.
I have a small PHP and JS code that checks and sets some coockies as soon as a new visitor comes to our website. These coockies determine which website to direct the visitor to
http://www.domain.com
or
http://www.domain.com/international
and than it will remember to always direct him to the same ‘domain’

So based on this tutorial I thought I need to do this as a module and place the block code on the app/design/........./layout/page.xml

but, ofcourse since I’m not sure of what I’m doing I get the following error:
Fatal error: Call to a member function toHtml() on a non-object in C:\AppServ\www\app\code\core\Mage\Core\Block\Abstract.php on line 503

Here is my code:

app/etc/modules/Residente.xml

<?xml version="1.0"?>
<config>
  <
modules>
    <
Residente_Custom>
      <
active>true</active>
      <
codePool>local</codePool>
    </
Residente_Custom>
  </
modules>
</
config>

app/code/local/Residente/Custom/etc/config.xml

<?xml version="1.0"?>
<config>
  <global>
    <
blocks>
      <
residente_custom>
        <class>
Residente_Custom_Block</class>
      </
residente_custom>
    </
blocks>
  </global>
</
config>

app/code/local/residente/custom/block/Coockies.php

file is too largelook in attached file

app/design/........./layout/page.xml

<default>
      <
block>
...
...
            <!-- 
LIGHTBOX Coockies Module #Custom -->
                
<block type="residente_custom/coockies"/>
            <!-- 
// LIGHTBOX Coockies Module -->
            
</block>
...
</default>

I’d really appreciate your help!! And perhaps someone might benefir (although something tells me I’m reinventing the wheel)

THANKS!!!
David

File Attachments
coockies-MagentoForum.zip  (File Size: 7KB - Downloads: 28)
 Signature 

Magento Specialist - http://www.futureye-designs.com

 
Magento Community Magento Community
Magento Community
Magento Community
 
Olof
Sr. Member
 
Total Posts:  99
Joined:  2008-10-17
 

Guys, I’m certain I’m being a wally…

I have:

{{block type='catalog/product_list' category_id='81' template='catalog/product/richard_random.phtml' my_param1='value 1'}}

I now need to pull out the my_param1 variable as the heading for the block…

I have this:

div class="cat-name">
           <
h3><?php echo $this->getMyParam1() ?></h3>
</
div>

But it isnt working…

Someone put me out of my misery..  Where should I connect “getMyParam1” with “my_param1”?!?!

Thanks in advance!

Rich

 
Magento Community Magento Community
Magento Community
Magento Community
 
Olof
Sr. Member
 
Total Posts:  99
Joined:  2008-10-17
 

Anyone....? :(

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
 
© Copyright 2010 Magento Inc.
Privacy Policy|Terms of Service
Magento Community Count
195896 users|804 users currently online|366290 forum posts