How to tell custom developed magento block to use a template? - php

This is the code in contollers
public function transactionsAction()
{
$ModelLayout = new Pteb_System_Model_Layout_Backend();
$ModelLayout->LoadLayout();
$Block = $ModelLayout->SetContentBlock( 'transactions-block', 'Pteb_System_Block_Cms' );
$Block->setTemplate('user/transactions/results.phtml');
$ModelLayout->ShowLayout();
}
/* this is the content to display on the page,
currently it displays without any template*/
$Block->setTemplate('user/transactions/results.phtml');
/* the template for the content above resides in the same folder as it is, with the name form.phtml. I replaced results.phtml with form.phtml in the above code but it displays the design only not the content.
Basically the form.phtml contains table structure with table heading while the results.phtml contains rows of contents.*/
How do I tell this results.phtml to use form.phtml as a template?

Can you try to go to this block:
Pteb_System_Block_Cms
and add the following setTemplate script:
public function __construct()
{
parent::__construct();
$this->setTemplate('abc/abc.phtml');
}

Related

opencart how to addEvent Journal3?

work fine in default opencart checkout
example..
$this->model_setting_event->addEvent('custom_name', 'catalog/view/theme/default/template/checkout/payment_method/after', 'event_function');
$this->model_setting_event->addEvent('custom_name', 'catalog/controller/checkout/payment_method/save/after',
'event_function');
$this->model_setting_event->addEvent('custom_name', 'catalog/model/checkout/order/addOrder/before', 'event_function');
how can i make it work with journal3 one checkout page?
opencart 3.0.2.0
There are 2 approaches, you can choose either one to implement based on which approach is more applicable to your situation:
Replace the view file before rendering the output
Modify the html through the output
I assuming you already know how to register an event, following
example just demonstrate how to implement the approach. Leave me a
comment to let me know if you not sure how to register an event.
1. Replace the view file before rendering the output
Create an event script: catalog/controller/event/my_event.php
/* before */
public function beforeLoadCheckoutView(&$route, &$args) {
/*
if the view route is 'journal3/checkout/checkout',
aka: 'catalog/view/theme/journal3/template/journal3/checkout/checkout.twig'
*/
if ($route == 'journal3/checkout/checkout') {
/*
replace the view file 'journal3/checkout/checkout' with your own view file
aka: 'catalog/view/theme/journal3/template/journal3/checkout/my_checkout_page.twig'
*/
$route = str_replace('journal3/checkout/checkout', 'journal3/checkout/my_checkout_page', $route);
}
}
Create your own checkout page view: catalog/view/theme/journal3/template/journal3/checkout/my_checkout_page.twig
<div>
I have replace the original view with this!
</div>
Result:
2. Modify the html through the output
Create an event script: catalog/controller/event/my_event.php
/* after */
public function modifyCheckoutPageHtml($route, &$args, &$output) {
/*
if the view route is 'journal3/checkout/checkout',
aka: 'catalog/view/theme/journal3/template/journal3/checkout/checkout.twig'
*/
if ($route == 'journal3/checkout/checkout') {
/* replace the title with your own title */
$output = str_replace('<h1 class="title page-title">Quick Checkout</h1>', '<h1 class="title page-title">I have changed this title!</h1>', $output);
}
}
Result:
now I am suffering from new problem
I can do some code when user click this button(繼續===continue)in default 6step checkout through
$this->model_setting_event->addEvent('name','catalog/controller/checkout/payment_method/save/after', 'function');
but in journal3 one step checkout do not have that button.

edit_form.php in custom block of totara/moodle

I have created a custom block "blockpre" in totara which will show the results of assignments.In edit_form.php of my block blockpre i added some extra elements in mform for my block.
in edit_form.php i have created a class "block_blockpre_edit_form" which extends "block_edit_form" (standard class). I have added standard elements in mform and also 1 extra element.
The problem is that i'm unable to access the value of standard as well as extra element added.
i have tried using $mform->get_data on view page of my block but its not working on any of the block pages.
Is there some standard way to access the value of mform element ?
edit_form.php
<?php
class block_blockpre_edit_form extends block_edit_form {
protected function specific_definition($mform) {
// Section header title according to language file.
$mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));
// A sample string variable with a default value.
$mform->addElement('text', 'config_text', get_string('blockstring', 'block_blockpre'));
$mform->setDefault('config_text', 'default value');
$mform->setType('config_text', PARAM_MULTILANG);
$mform->addElement('header', 'configheader', get_string('blocksettings1', 'block_blockpre'));
global $CFG, $OUTPUT,$DB,$USER;
$courseid = $_REQUEST['id'];
$sel="select id,name from {assign} where course=".$courseid;
$sel1= $DB->get_records_sql($sel);
foreach($sel1 as $s)
{mform element
$dis=$s->id;
$namem=$dis;
$mform->addElement('advcheckbox','assignid',$s->name,null, array('name' => $namem),$namem); //extra element
}
}
}
?>
Thanks for help in advance.
You can display form data with $mform->render();
Or use the data internally with $mform->set_data();

Custom layouts for catalog_category_view on Magento

I need to be able to use different layouts for different categories, selected on the Custom Design tab on the category and Page Layout field.
I'm using Magento 1.9.1.0.
In my config.xml I have:
<global>
<page>
<layouts>
<module_home module="page" translate="label">
<label>Module Home</label>
<template>page/base.phtml</template>
<layout_handle>module_home</layout_handle>
</module_home>
<module_categories module="page" translate="label">
<label>Module Categories</label>
<template>page/base.phtml</template>
<layout_handle>module_categories</layout_handle>
</module_categories>
</layouts>
</page>
...
And in my layouts.xml file I have:
<default>
<reference name="root">...</reference>
</default>
<module_home translate="label">...</module_home>
<module_categories translate="label">...</module_categories>
When I select Module Categories layout from the admin of the category I don't get the changes for module_categories handler, only the ones set on <default>.
If I force it like this:
<catalog_category_view>
<update handle="module_categories" />
</catalog_category_view>
I do get the changes, but I want multiple handlers, selected as Layouts on the admin.
Maybe I'm doing something wrong? Could not find an example for how to do this, maybe you can point somewhere? Thanks!
You've got the right idea with the <update /> directive. Just put it in your category's Custom Layout Update field in the admin, and that category should apply that layout handle. You can still set the page template with the Page Layout field.
The reason you need to explicitly specify the layout handle with the <update /> directive is because Magento's category controller does not use the layout_handle node, whereas other parts of Magento, like Magento's CMS page controller, do use it.
For example, let's look at Mage_Cms_PageController, which is responsible for rendering a CMS page:
public function viewAction()
{
$pageId = $this->getRequest()
->getParam('page_id', $this->getRequest()->getParam('id', false));
if (!Mage::helper('cms/page')->renderPage($this, $pageId)) {
$this->_forward('noRoute');
}
}
Let's dig deeper and look at Mage_Cms_Helper_Page::renderPage(), which calls Mage_Cms_Helper_Page::_renderPage():
protected function _renderPage(Mage_Core_Controller_Varien_Action $action, $pageId = null, $renderLayout = true)
{
$page = Mage::getSingleton('cms/page');
/* ... */
if ($page->getRootTemplate()) {
$handle = ($page->getCustomRootTemplate()
&& $page->getCustomRootTemplate() != 'empty'
&& $inRange) ? $page->getCustomRootTemplate() : $page->getRootTemplate();
$action->getLayout()->helper('page/layout')->applyHandle($handle);
}
/* ... */
if ($page->getRootTemplate()) {
$action->getLayout()->helper('page/layout')
->applyTemplate($page->getRootTemplate());
}
/* ... */
}
We see two important pieces of logic here.
First, _renderPage() calls $action->getLayout()->helper('page/layout')->applyHandle($handle). If you dig even deeper, you'll see that Mage_Page_Helper_Layout::applyHandle() is responsible for applying the appropriate layout_handle as defined by the configuration XML:
public function applyHandle($pageLayout)
{
$pageLayout = $this->_getConfig()->getPageLayout($pageLayout);
if (!$pageLayout) {
return $this;
}
$this->getLayout()
->getUpdate()
->addHandle($pageLayout->getLayoutHandle());
return $this;
}
Second, _renderPage() calls $action->getLayout()->helper('page/layout')->applyTemplate($page->getRootTemplate()). Similar to applyHandle(), applyTemplate() applies the actual page template.
So, this explains why you can depend on the layout_handle as defined in the configuration XML when it comes to CMS pages. Now, let's find out why it's not dependable for categories.
Let's look at Mage_Catalog_CategoryController::viewAction(), which is responsible for displaying a category page:
public function viewAction()
{
if ($category = $this->_initCatagory()) {
$design = Mage::getSingleton('catalog/design');
$settings = $design->getDesignSettings($category);
/* ... */
// apply custom layout update once layout is loaded
if ($layoutUpdates = $settings->getLayoutUpdates()) {
if (is_array($layoutUpdates)) {
foreach($layoutUpdates as $layoutUpdate) {
$update->addUpdate($layoutUpdate);
}
}
}
/* ... */
// apply custom layout (page) template once the blocks are generated
if ($settings->getPageLayout()) {
$this->getLayout()->helper('page/layout')->applyTemplate($settings->getPageLayout());
}
/* ... */
}
elseif (!$this->getResponse()->isRedirect()) {
$this->_forward('noRoute');
}
}
Stripping out all of the default layout logic, we're left with two pieces:
// apply custom layout update once layout is loaded
if ($layoutUpdates = $settings->getLayoutUpdates()) {
if (is_array($layoutUpdates)) {
foreach($layoutUpdates as $layoutUpdate) {
$update->addUpdate($layoutUpdate);
}
}
}
This goes through the category's layout updates (as defined in the Custom Layout Update field in the admin) and applies them. This is why using <update handle="some_handle" /> works.
And...
// apply custom layout (page) template once the blocks are generated
if ($settings->getPageLayout()) {
$this->getLayout()->helper('page/layout')->applyTemplate($settings->getPageLayout());
}
This applies the custom page template, similar to how the CMS page logic did it, using Mage_Page_Helper_Layout::applyTemplate().
Now, notice something missing?
Yup, the category controller does not call Mage_Page_Helper_Layout::applyHandle() to apply the layout_handle as defined in the configuration XML. This means that you can use the Page Layout field to give the category a particular page template, but your layout_update that accompanies the template won't get applied!
Hope this clears up why your layout_update node isn't getting used on the category page the way you would expect it to. Magento is full of odd behavior and inconsistencies like this :)

cannot get drupal 7 to recognize a page template for a content type

I have a drupal 7 site that I want to make secondary front pages on. the problem with this is the "page--front.tpl.php" is a two column layout and the "page.tpl.php" is a one column layout. if i use a node template it shoves it in the body of the one column.
the theme name is "egress" the machine name for the content type is "landing" but when i try to hook the page--landing the same way i do the node--landing nothing happens. nothing.
i am clearing the cache and hard refreshing the page with every change of the template files.
one code i have tried in the of the "page.tpl.php"
function egress_preprocess_page(&$vars) {
global $node;
if ($node->type == 'landing') {
$vars['theme_hook_suggestions'] = array('page__landing');
}
}
another
function egress_preprocess_page(&$vars) {
if ($vars['node']->type == "landing_page") {
$vars['template_files'][] = 'page--landing';
}
}
anyone ideas?
The following should go to your theme's template.php rather than page.tpl.php
function egress_preprocess_page(&$vars) {
// For page--(node-type).tpl.php
if (isset($vars['node'])) {
$vars['theme_hook_suggestions'][] = 'page__'. $vars['node']->type;
}

View in MVC, what is a layout and how to create one

I dont understand what the layout is, in the view. I asked a question previously on the subject of templating in PHP, but I still dont quite understand. I assume that you create a general layout for the site and then include each specific view within that layout.... I would like to know how to go about doing this. Also, are should the templates be made using just html, because I also looked at these things called helpers.... I'm just confused on the View part of the MVC, and the actual templates and how they're made. I learn best with examples, If you guys have any.
Also, a more important question, lets say I had a form that a user saw only if he was logged in, would I control that in the view, or in the controller?
So Would i do
in the controller
include 'header';
if(isset($_SESSION['userID'])){
include 'form';
}
include 'footer';
or
in the template
<html>
<?php if(isset($_SESSION['user_id'])): ?>
<form>....</form>
<?php endif;?>
</html>
EDIT
So, is there an include statement from within the layout to see the specific view template? how so?
a layout is whatever you have around your main content area. Usually on a normal website it would be any sidebar,header,footer. Most of MVC framework provide the layout to avoid to repeat those parts in all views.
You can imagine if like you have two view cascaded
you actual view is rendered, this content is saved
the layout view (all the items around the content) are rendered and your content is included in that output
for your login question actually your would have to do both
on the controller and the view
$this->view->isLogged = isset($_SESSION['userID']);
in the view
<?php if($isLogged): ?>
<form>....</form>
<?php endif;?>
I hesitate to answer this question only because of the religious fervor that surrounds it.
To get a really good understanding of the issues behind the general concepts see This Wiki Discussion Page This is the discussion page around the wiki MVC article.
Here is the rule of thumb I like to follow (BTW I use CodeIgniter and it kind of sounds like you are too):
The "view" should have virtually no logic. It should only be HTML (in the web world) with peppered PHP that simply echos variables. In your example you would break out the form into its own view and the controller would determine if was loaded or not.
I like to look at it this way: The view should have no concept of where the data comes from or where it is going. The model should be view agnostic. The controller meshes data from the model and provides it to the view - and it takes input from the view and filters it to the model.
Here is a quick and dirty (untested - but it should get the point across) example:
Theapp.php (The App controller)
class Theapp extends Controller
{
var $_authenticated;
var $_user;
var $_menu; // array of menus
function __construct()
{
session_start();
if (isset($_SESSION['authenticated']) && $_SESSION['authenticated'])
{
$this->_authenticated = $_SESSION['authenticated']; // or some such thing
$this->_user = $_SESSION['user'];
}
$this->_menu = array("Logout", "Help", "More");
parent::__construct();
$this->loadView("welcome"); // loads primary welcome view - but not necessarily a complete "html" page
}
function index()
{
if (!$this->_authenticated)
$this->loadView("loginform");
else
{
$viewData['menu'] = $this->_menu;
$viewData['user'] = $this->_user;
$this->loadView("menu", $viewData);
}
}
function login()
{
/* code to authenticate user */
}
function Logout() { /* code to process Logout menu selection */ }
function Help() { /* code to process Help menu selection */ }
function More() { /* code to process More menu selection */ }
}
welcome.php
<h1> Welcome to this quick and dirty app!</h1>
All sorts of good HTML, javascript, etc would be put in here!
loginform.php
<form action"/Theapp/login" method="post">
User: <input id='user' name='user'>
Pass: <input id='pass' name='pass' type='password'>
<input type='submit'>
</form>
menu.php
Hi <?= $user ?>!<br>
Here's your menu<br>
<? foreach ($menu as $option) { ?>
<div class='menuOption'><a href='/Theapp/<?=$option?>'><?=$option?></a></div>
<? } ?>
Hope this helps.
If your not using a framework, then a simple way to have a layout and a view can be like so:
<?php
function layout($layout_params) {
extract($layout_params);
# Remember: $layout_content must be echoed for the view to be seen.
ob_start();
include "layout_page.php";
$html = ob_get_contents();
ob_end_clean();
return $html;
}
function view($view_params) {
extract($view_params);
ob_start();
include "home_page.php";
$html = ob_get_contents();
ob_end_clean();
return $html;
}
#
# The variable $parameters is extracted and $params becomes a variable in the view as an array,
# $logged_in is also now avaiable in the view
#
$parameters = array("params" => array("name" => "joe"), "logged_in" => false);
$view_content = view($parameters); # => Returns the HTML content for home_page.php
# Now we need the layout content to include the view:
# The layout file will expect a variable called $layout_content to be the html view.
# So we need to set $layout_content to be $view_content
$parameters["layout_content"] = $view_content;
# We no longer need $view_content so we can unset the variable
unset($view_content);
# When $paramters is extracted it will have $layout_content as a variable:
$layout_content = layout_view($paramters); # => Returns the HTML content for the layout_page.php
# Now send the results to the browser
echo $layout_content;
?>

Categories