Get skin path in Magento?

后端 未结 6 1321
面向向阳花
面向向阳花 2020-12-14 06:01

I have a few custom PHP functions for my Magento store that I stored in myfunc.php and I need to require it from in a few different .phtml files. How do I do that?

I

相关标签:
6条回答
  • 2020-12-14 06:12

    The way that Magento themes handle actual url's is as such (in view partials - phtml files):

    echo $this->getSkinUrl('images/logo.png');

    If you need the actual base path on disk to the image directory use:

    echo Mage::getBaseDir('skin');

    Some more base directory types are available in this great blog post:

    http://alanstorm.com/magento_base_directories

    0 讨论(0)
  • 2020-12-14 06:26

    To get current skin URL use this Mage::getDesign()->getSkinUrl()

    0 讨论(0)
  • 2020-12-14 06:30

    First note that

    Mage::getBaseDir('skin')
    

    returns only path to skin directory of your Magento install (/your/magento/dir/skin).

    You can access absolute path to currently used skin directory using:

    Mage::getDesign()->getSkinBaseDir()
    

    This method accepts an associative array as optional parameter to modify result.

    Following keys are recognized:

    • _area frontend (default) or adminhtml
    • _package your package
    • _theme your theme
    • _relative when this is set (as an key) path relative to Mage::getBaseDir('skin') is returned.

    So in your case correct answer would be:

    require(Mage::getDesign()->getSkinBaseDir().DS.'myfunc.php');
    
    0 讨论(0)
  • 2020-12-14 06:32

    To get that file use the below code.

    include(Mage::getBaseDir('skin').'myfunc.php');
    

    But it is not a correct way. To add your custom functions you can use the below file.

    app/code/core/Mage/core/functions.php
    

    Kindly avoid to use the PHP function under skin dir.

    0 讨论(0)
  • 2020-12-14 06:34

    To use it in phtml apply :

    echo $this->getSkinUrl('your_image_folder_under_skin/image_name.png');
    

    To use skin path in cms page :

    <img style="width: 715px; height: 266px;" src="{{skin url=images/banner1.jpg}}" alt="title" />
    

    This part====> {{skin url=images/banner1.jpg}}

    I hope this will help you.

    0 讨论(0)
  • 2020-12-14 06:36

    First of all it is not recommended to have php files with functions in design folder. You should create a new module or extend (copy from core to local a helper and add function onto that class) and do not change files from app/code/core.

    To answer to your question you can use:

    require(Mage::getBaseDir('design').'/frontend/default/mytheme/myfunc.php');
    

    Best practice (as a start) will be to create in /app/code/local/Mage/Core/Helper/Extra.php a php file:

    <?php
    class Mage_Core_Helper_Extra extends Mage_Core_Helper_Abstract
    {
    
        public function getSomething()
        {
            return 'Someting';
        }
    
    }
    

    And to use it in phtml files use:

    $this->helper('core/extra')->getSomething();
    

    Or in all the places:

    Mage::helper('core/extra')->getSomething();
    
    0 讨论(0)
提交回复
热议问题