Magento - Include TPL files in CMS - php

I'm performing a quick job for a Magento-based site and can't recall how to pull in TPL files within the CMS. I've tried using the following code in my CMS page...
{{block type="cms/block" block_id="page_heading" template="cms/content_heading2.phtml"}}
The TPL file is already in the correct folder... app/design/frontend/default/wfs/cms
I'm just not sure how to include this PHTML file correctly. Is it possible to provide the correct syntax?
Thanks!

When you say
`type="cms/block"`
you're telling Magento to create a 'cms/blocktemplate object, which translates to aMage_Cms_Block_Block` class. If you take a look at this block's source
#File: app/code/core/Mage/Cms/Block/Block.php
protected function _toHtml()
{
$blockId = $this->getBlockId();
$html = '';
if ($blockId) {
$block = Mage::getModel('cms/block')
->setStoreId(Mage::app()->getStore()->getId())
->load($blockId);
if ($block->getIsActive()) {
/* #var $helper Mage_Cms_Helper_Data */
$helper = Mage::helper('cms');
$processor = $helper->getBlockTemplateProcessor();
$html = $processor->filter($block->getContent());
}
}
return $html;
}
you can see this doesn't render templates, but instead renders Magento's static block objects.
Try
`type="core/template"`
instead, and make sure your block ID is unique.

Related

Get instances to arbitrary plugins in php files

The name is quite bad, but I really don't know what else to call it.
I'm trying to make a extendable and modular plugin system for my website. I need to be able to access plugin php files that exist in a plugin directory and get access to their classes to call functions such as getting the html content that the plugin should show and more.
Below is a semi-pseudo code example of what I am trying to achieve, but how to actually arbitrarily load the plugins is where I am stuck (PluginLoader.php).
-Max
//BasePlugin.php
abstract class BasePlugin
{
public function displayContent()
{
print "<p>Base Plugin</p>";
}
};
//ExamplePlugin.php -> In specific plugin directory.
require('../BasePlugin.php');
class ExamplePlugin extends BasePlugin
{
public static function Instance()
{
static $inst = null;
if ($inst === null) {
$inst = new ExamplePlugin();
}
return $inst;
}
public function displayContent()
{
print "<p>Example Plugin</p>";
}
}
//PluginLoader.php
foreach($pluginFile : PluginFilesInDirectory) { // Iterate over plugin php files in plugin directory
$plugin = GetPlugin($pluginFile); // Somehow get instance of plugin.
echo plugin->displayContent();
}
I'm guessing here, but it seems to me that you need to:
get a list of the plugins in the desired directory.
include or require the plugin's class file.
create an instance of the class.
call the plugin's displayContent() method.
So, you probably want to do something like
$pluginDir = 'your/plugin/directory/' ;
$plugins = glob($pluginDir . '*.php') ;
foreach($plugins as $plugin) {
// include the plugin file
include_once($plugin) ;
// grab the class name from the plugin's file name
// this finds the last occurrence of a '/' and gets the file name without the .php
$className = substr($plugin,strrpos($plugin,'/') + 1, -4) ;
// create the instance and display your test
$aPlugin = $className::Instance() ;
$aPlugin->displayContent() ;
}
There's probably a cleaner way to do it, but that will ready your directory, get the plugins' code, and instantiate each one. How you manage/reference them afterwards depends on how your plugins register with your application.

front controller won't load css and js from setMedia on a prestashop 1.7 module

I'm writing a prestashop module for prestashop 1.7.2.1.
I created a front controller for my module with the following code:
<?php
require_once (__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'php'.
DIRECTORY_SEPARATOR.'TuxInDb.php');
class TuxInModCarTypeCarTypeProductsModuleFrontController extends ModuleFrontController {
private $tuxDb;
public function initContent(){
parent::initContent();
$productIds = [];
$this->tuxDb = TuxInDb::getInstance();
$companyName = Tools::getValue('company_name');
$modelName = Tools::getValue('model_name');
$year = Tools::getValue('year');
$month = Tools::getValue('month');
$carType = Tools::getValue('car_type');
$carListCarTypeIds=$this->tuxDb->getCarListCarTypeIds($companyName,$modelName,$carType,$year,$month);
$productIds = $this->tuxDb->getProductIdsByCarListCarTypeIds($carListCarTypeIds);
$this->context->smarty->assign('product_ids',$productIds);
$this->setTemplate('module:tuxinmodcartype/views/templates/front/cartypeproducts.tpl');
}
public function setMedia() {
parent::setMedia();
$this->registerStylesheet('module-tuxinmodcartype-cartypeproducts-style','modules/'.$this->module->name.'/css/cartypeproducts.css');
$this->registerJavascript('module-tuxinmodcartype-cartypeproducts-js','modules/'.$this->module->name.'/js/cartypeproducts.js');
}
}
as you can see in setMedia() function I load a css and js files.
I even debugged it in xdebug and I noticed that those lines of code actually get executed, but when I try to browse my front controller with the following url:
http://prestashop.dev:8080/index.php?company_name=BMW&model_name=SERIA+1&year=2011&month=1&car_type=5+%D7%93%D7%9C%D7%AA%D7%95%D7%AA+%28%D7%94%D7%90%D7%A6%D7%B3%D7%91%D7%A7%29&fc=module&module=tuxinmodcartype&controller=cartypeproducts&id_lang=1
and I check the network tab of my google chrome browser I noticed that the js and css file I required do not get loaded.
any ideas?
I see no javascript errors or php errors (I also have DEV enabled in prestashop).
If an asset path is wrong then Prestashop won't even append it to the browser's <head> (or bottom depending on CCC settings) and won't throw out any errors.
Probably your path is incorrect, to get proper path use this:
$this->registerStylesheet('module-tuxinmodcartype-cartypeproducts-style', $this->module->getPathUri() . 'css/cartypeproducts.css');
$this->registerJavascript('module-tuxinmodcartype-cartypeproducts-js', $this->module->getPathUri() . 'js/cartypeproducts.js');
This works well with PrestaShop 1.7.x
Add this inside your ModuleFrontController:
public function setMedia()
{
parent::setMedia();
$this->addCSS($this->module->getPathUri().'views/css/style.css');
}
I hope this helps!

How to load data in view file in magento?

I just started to learn magento.I have a list of data in controller. I want to show that list in my view file. How can i do that ?
Here is my controller action- category. Where i getting the array of data.
<?php
class Company_Web_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
public function addcategoryAction()
{
if ($this->getRequest()->isPost())
{
$data = $this->getRequest()->getParams();
$catName = $data['catName'];
$status = $data['status'];
$data = array('name'=>$catName,'status'=>$status);
$model = Mage::getModel('web/web')->setData($data);
try {
$insertId = $model->save()->getId();
$this->_redirect('web/index/category');
} catch (Exception $e){
echo $e->getMessage();
}
}
$this->loadLayout();
$this->renderLayout();
}
public function categoryAction()
{
$collection = Mage::getModel('web/web')->getCollection()->getData();
$this->loadLayout();
$this->renderLayout();
}
}
?>
Magento works with block type i.e where you have mentioned your phtml file to render the content for category action i.e
<web_index_category>
<reference name="content">
<block type="core/template" name="category.block" template="customfile.phtml" />
</reference>
</web_index_category>
for the block type you can drfine your custome type
i.e. type="web/category"
and create one Block
Company_Web_Block_Category extends Mage_Core_Block_Template
inside this create a function and return your collection
i.e.
public function getCollection()
{
return Mage::getModel('web/web')->getCollection()->getData();
}
In your phtnl access this function using,
$this->getCollection()
Check here for more elaboration
http://www.gravitywell.co.uk/blog/post/how-to-creating-your-own-custom-block-in-magento
http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-4-magento-layouts-blocks-and-templates
You could do this from the controller, but it's much simpler and much more in line with Magento best practices to pass data to templates via Blocks.
To figure out which template and block is being rendered, go to System > Configuration > Advanced > Developer > Debug and enable template path hints (with block names). Now when you load the frontend, you'll see red borders around parts of the page that illustrate which block and template combination is loading various parts of the webpage.
Properties and methods of a Block object are automatically available to the template being rendered by that Block.
To make that data available in your template, just add a method to the Block that's rendering the template, then call that method from within your template.
As a shortcut, you could also just call $collection_data = Mage::getModel('web/web')->getCollection()->getData(); directly inside your template.
For more information, see Magento for Developers: Part 4 - Magento Layouts, Blocks and Templates.

Better way to overide content page in drupal

I am creating custom content pages using this function in my template file
function myTheme_preprocess_page(&$var, $hook) {
if (isset($var['node']->type)) {
$var['theme_hook_suggestions'][] = 'page__' . $var['node']->type;
}
}
Then I am creating a custom page--"content_name".tpl.php file for my content. However this also overrides the edit, moderate, track pages for that content as well. I only want it to override the main content page. Is there an easy way to do this.
Change your code to be something like:
function myTheme_preprocess_page(&$var, $hook) {
if (isset($var['node']->type) && is_null(arg(2))) { // the edited line
$var['theme_hook_suggestions'][] = 'page__' . $var['node']->type;
}
}

Apply styles using php file in Zend?

NOTE: UPDATED MY QUESTION
I am using zend.I have the file "stylesettings.php" under css folder. Having the following line to convert php file to css.
header("Content-type: text/css");
stylesetting.php is under application/css/stylesettings.php
Now i want to get the color code from my DB in stylesettings.php.Here i can write basic DB connection code to get values from DB. I guess there might be another way to get all DB values by using zend. How can we connect DB like "Zend_Db_Table_Abstract" in stylesettings file ?
Is it possible to use zend component in that file ? Kindly advice on this.
I hope you understand.
You are breaking the basic separated model assumed in a MVC framework. For such color code, if it's only used in one place, I would suggest that you output the color in the "style="color: <color>"" in-line style in order to keep the dynamic part in HTML and your CSS file static.
If you really want to do this, then you should consider output your dynamic stylesheet in a URL path other than css, and use the controller/views etc to generate the stylesheet.
i am using the following way to apply color settings in layout.phtml
Use below code in bootstrap file
<?php
require_once 'plugins/StyleController.php';
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initRouter() {
$front = Zend_Controller_Front::getInstance ();
$front->setControllerDirectory ( dirname ( __FILE__ ) . '/controllers' );
$router = $front->getRouter ();
$front->registerPlugin ( new StyleController ( $router ) );
}
}
Created folder plugins under application and create a new file called stylecontroller.php
<?php
class StyleController extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$layout = Zend_Layout::getMvcInstance();
$view = $layout->getView();
/* code for getting color settings */
$this->settings = new Admin_Model_DbTable_Settings();
$view->colorsettings = $this->settings->getStyleSettings();
//print_obj($view->colorsettings);
}
}
?>
Also to get color code,
<?php
class Admin_Model_DbTable_Settings extends Zend_Db_Table_Abstract
{
public function getStyleSettings()
{
$select = $this->_db->select()
->from('style_settings');
$result = $this->getAdapter()->fetchAll($select);
return $result['0'];
}
}
?>
By using above code i can able to use $this->colorsettings in layout page.
This one fix my dynamic color in layout but not in other template files.

Categories