Как отобразить "HelloWorld" с помощью файла *.phtml, но helloworld не должен напрямую записываться в файл.phtml
Я хочу отобразить "Hello world" на странице категории через файл .phtml, но условие состоит в том, что hello world не должен напрямую записываться в файл.phtml. Мы можем написать любой код в файле .phtml и вызвать этот файл phtml, и он отобразит hello world.
Итак, как это возможно?
Я предполагаю, что если я создам файл .php и создам одну функцию, и эту функцию я вызову в файле .phtml, возможно ли это? а затем .phtml вызывает .xml.
Спасибо.
3 answers
Создать YourNameSpace_Test.xml в приложении/etc/модули
<?xml version="1.0"?>
<config>
<modules>
<YourNameSpace_Test> <!-- Name of Module -->
<active>true</active> <!-- This says if the module is active or not -->
<codePool>local</codePool> <!-- This says the location of the module i.e inside the local folder. It can also be community folder. -->
</YourNameSpace_Test>
</modules>
</config>
В app/code/local/YourNameSpace/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<YourNameSpace_Test>
<version>0.1.0</version> <!-- Version of module -->
</YourNameSpace_Test>
</modules>
<frontend>
<routers>
<test>
<use>standard</use>
<args>
<module>YourNameSpace_Test</module>
<frontName>test</frontName> <!-- This is the URL
of the module. i.e www.yourmagento.com/index.php/test will be the url of your module. -->
</args>
</test>
</routers>
<layout>
<updates>
<unique_identifier module="YourNameSpace_Test">
<file>yournamespace_test.xml</file>
</unique_identifier>
</updates>
</layout>
</frontend>
<global>
<blocks>
<test>
<class>YourNameSpace_Test_Block</class> <!-- Path of the
Block Folder, where all php files are located related to view -->
</test>
</blocks>
</global>
</config>
В app/code/local/YourNameSpace/controllers/IndexController.php
class YourNameSpace_Test_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout(); //This function read all layout files and loads them in memory
$this->renderLayout(); //This function processes and displays all layout phtml and php files.
}
}
В app/design/frontend/default/default/layout/yournamespace_test.xml
<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
<catalog_category_view>
<reference name="content"> <!-- block name inside which you need to display hello world -->
<action method="setTemplate">
<template>yournamespace_test/catalog/yournamespace_test.phtml</template>
</action>
</reference>
</catalog_category_view>
</layout>
В app/design/frontend/default/default/template/yournamespace_test/catalog/yournamespace_test.phtml
<div>
<?php
echo $this->getContent();
?>
</div>
В app/code/local/YourNameSpace/Block/Test.php
<?php
class YourNameSpace_Test_Block_Test extends Mage_Core_Block_Template
{
public function getContent()
{
return "Hello World";
}
}
Это лучший способ сделать это. в противном случае просто создайте
public function yourFunction(){ return "Hello World"; }
в app\code\core\Mage\Catalog\Block\Category\View.php и вызовите эту функцию как<?php echo $this->yourFunction(); ?>
в приложении/коде/дизайне/интерфейсе/по умолчанию/по умолчанию/каталог/категория/просмотр.phtml Но всегда помните, что редактирование ядра - очень плохая идея!
Я не уверен, что понимаю ваш вопрос, но я могу попробовать. Вы можете поместить это в один из ваших файлов xml-макета:
<catalog_category_view><!--in the category view page-->
<reference name="content"><!--in the content area-->
<block type="core/template" name="helloworld" as="helloworld" template="path/to/template.phtml"><!--create a block-->
<action method="setMessage" translate="message"><!--set a message and translate it-->
<message>Hello world</message><!-- the text-->
</action>
</block>
</reference>
</catalog_category_view>
И ваш path/to/template.phtml
должны быть расположены в app/design/frontend/{interface}/{theme}/template
, и это должно выглядеть так:
<?php echo $this->getMessage();?>
Это должно дать вам желаемый результат (если я понял, каков ваш желаемый результат).
Это?
Привет.Файл phtml:
<?php
include_once 'simple.php';
echo get_saying();
?>
Simple.php файл:
<?php
function get_saying() {
return "Hello World!";
}