How block and template work in Magento - php

i've been working with magento for a while and there is some detail that i'm trying to understand how it works, block and templates, for example, the part i don't understand how works is that you can do this in a template( a .phtml file)
$this->getFunctionName();
this means that there is a function with that name in the block that was assigned that template.
I'm trying to write a simple example just to see how it works but i can't figure it out, untill now, i just have a headeach.
How is posible that you can use $this within a .phtml file to call the block functions?? Seems to be that the .phtml is part of the object, right?
thanks

Check out the fetchView method of Mage_Core_Block_Template, template files are included within that method and have access to the class. Output buffering is used to collect the template output, rather than displaying the template as it's included.

When a file is include/require'd in PHP, you can for most purposes suppose that the code inside of it gets inlined into the calling file. Hence, all scope (including $this gets inherited by the template file.

Related

Include external php inside Wordpress Header

I am trying to do something simple but it won't work.
I have a custom Wordpress header. I need to include external php classes and other stuff. The problem is that if I do that the page breaks, it doesn't work anymore.
This doesn't occur when I try to "require" external php scripts inside page templates. The problem occurs when I include it in the header.
To make it shorter, I have:
header-home.php (standard wordpress theme header file, which has to include the following file)
snippets.php (a php class)
header.home.php is located at /wp-content/themes/twentyseventeen-child/templates...
snippets.php is located at /resources/scripts/snippets.php
Perhaps the header loads something that is not compatible with custom inclusions?
I was able to include an external php file which contains pure html elements. If I try to load custom classes, the page simply breaks. The include filepath is correct, so that's not the problem.
All the files that you are going to require, you must do in the file "functions.php" that is in "/wp-content/themes/twentyseventeen-child/functions.php", then in the file "header.php" you can instantiate your objects or call functions. For example:
Functions.php
<?php
require("./YourClass.php");
function getYourClass() {
$yourClass = new YourClass();
var_dump($yourClass); //Verify if it returns data and then delete this line
return $yourClass;
}
Header.php
getYourClass();//Here it brings the object and shows the output.

WordPress include file at header not working

I tried to include some php files in header.php. I use the script below :-
get_template_part( 'custom/tos_functions');
However get_template_part doesn't work for me, so I use include which works fine.
include (TEMPLATEPATH . "custom/tos_functions.php");
The issue is, if I put this line in header.php, I want to use some of the functions in custom template , say profile.php. In profile.php the file is as though not being called at all. I can't get the data I need in profile.php.
Then I tried to take out the include script from header, and place it in profile.php and the data I need can be called perfectly fine.
This will be an issue as there will be many custom php pages I need to create, thus every page will call the include script.
Question, why is it that the file cannot be called from header.php ? The data can only be retrieved within header.php and any custom page that calls the header, couldn't get the data from the include file.
Anyway I can fix this so that I can place the include file in the header.php?
Thank you!
Rather than including files in the theme template files (such as header.php), you should include them in the theme's functions.php file.
Including them in the template files is almost always too late in the WP run for it to be useful, and it may cause unexpected results and unexpected output in your templates.
Instead, in your functions.php file, do something like so:
require_once 'custom/tos_functions.php';
Then, your API code is available where you need it, and it is in the "right" place and consistent with the "WordPress way".

Error in simple function call in Wordpress page

I am trying to insert a secondary header beyond the main.
the main thing is header.php and call with get_header();
To try to make another header I added in functions.php:
function secondary_header() {
echo '<p>This is a test</p>';
}
add_action('get_customheader', 'secondary_header');
and in the page.php
get_customheader();
I have the error:
Fatal error: Call to undefined function get_customheader()
As the Wordpress Codex say "Custom header is an image that is chosen as the representative image in the theme top header section."
So, this function is expecting an array of default arguments to render the image, like "width", "height", "default-image" (image location/path).
This means your problem is one of the followings:
You are using an unnecessary function for what you want to accomplish.
You want to use the Custom Hearder function, but don't understand how it works.
To make it work you just need to delete this line:
add_action('get_customheader', 'secondary_header');
And then call your function on your template file like any other normal function:
<?php secondary_header(); ?>
If what you want is to insert additional code/html in multiple pages dynamically or just to keep your documents neat and clean, I'll recommend you to learn about the built-in function of Wordpress to insert template files, which is get_template_part().
For further understanding of how get_template_part() works I'll recommend you to learn about include() and require() which are native PHP functions and will work even outside of WP.
I think it would be easier for you to create a .php file with your custom header, and then just pull it in your template file where you call get_header() with something like
get_template_part('additional/custom_header');
This will search the custom_header.php file in the 'additional' folder of your theme.
Your error comes from the fact that you have added an action to a non existing function get_customheader(). What you wanted probably is do_action('get_customheader');
Read more:
do_action()
add_action()

Broken SilverStripe Template

For some reason, after doing a ?flush=all, a certain page type is not able to locate it's default template. I figure out that it's not loading it's template file after appending showtemplate=1 to the URL. The dumped raw template shows nothing for the default template.
Pastebin: http://pastebin.com/uMLefAsP
I wish someone could point me to the right direction here, for I have no idea where to start debugging.
Thanks, Jan.
Firstly, and I have to ask: Is there actually a PageType called "CommunityExtensionPage"? PageTypes need to be named the same as the required template, for the template to be picked-up automatically.
You seem to have two template files "CommunityExtensionPage.ss". One at "templates/CommunityExtensionPage.ss" and one at "templates/Layout/CommunityExtensionPage.ss"
It would be useful to see the contents of both files. SilverStripe will look for "CommunityExtensionPage.ss" in the top-level of the "templates" dir before looking in "templates/Layout".
If "templates/CommunityExtensionPage.ss" is found, it will also try and look for "templates/Layout/CommunityExtensionPage.ss" and render this into the $Layout template variable. Otherwise, it'll use the default "templates/Page.ss" and request "templates/Layout/CommunityExtensionPage.ss"
Does your "templates/CommunityExtensionPage.ss" template contain a reference to $Layout? If not, then the contents of "templates/Layout/CommunityExtensionPage.ss" will not be rendered.

php/wordpress include a file

This question is regarding wordpress.
I have two variables $random_n and $adcount declared in each of index.php , archieve.php, search.php and single.php files at the very first line.
The variables are declared as follows:
<?php $random_n = randomNumber();?>
<?php $adcount = 1; ?>
randomNumber() is a function inside function.php.
If I move those variables to header.php and remove them from all these files, it stops working. :/ The variables can't be found in index.php, archieve.php, search.php, single.php if they are placed in header.php
I've also tried putting this code inside a new file and including that file to index.php, archieve.php, search.php, and single.php, but it hadn't helped as well.
If the variable is somehow related to your blog configuration, you should use the wordpress option API to build this (see here , search for option).
If your variable is random (your function name hints that) a way to work around this would be using session variables to store those values.
A good tutorial on how to use session variables in wordpress can be found here:
http://www.myguysolutions.com/2010/04/14/how-to-enable-the-use-of-sessions-on-your-wordpress-blog/
Type in var_dump($random_n) into the beginning of index.php and post the output, please.
One possible reason is that your index, search and so on are included inside the function that does not share the scope with header.php, so your vars are just outside of the scope. It might help if you declare them global both in header.php and your working files. But that's not a good idea.
Better solution would be to introduce some Registry object (Registry pattern) and use it.

Categories