Something that has been bothering me since I started using drupal is how exactly does the php engine know which $node/$classes/$attributes you are referring to in template files. these variables are never declared as globals, so how does the php engine figure out what "$node" you are referring to??
See http://drupal.org/node/223430 which states
The main role of the preprocessor is to set up variables to be placed
within the template (.tpl.php) files. From Drupal 7 they apply to
templates and functions, whereas in Drupal 6 preprocess functions only
apply to theming hooks implemented as templates. Plain theme functions
do not interact with preprocessors.
Look at the template_preprocess* and the template_process referred to in the API. These functions show you the code that set up the various variables that you can access in your node template.
In a similar manner, you can also add your own preprocess code in a theme function to add to the variables available in your tpl.php file like so:
/**
* Implements hook_preprocess_HOOK().
*/
function MY_MODULE_preprocess_node(&$variables) {
$variables['hello'] = 'Look at me now!';
}
After this, you can reference $hello in your node.tpl.php file.
These variables are set by Drupal's templating engine.
Related
What is the file (or files) that allow me to access all the variables in Smarty templates written in other PHP files?
I can't find a function to do that.
there's not a file where you find all the smarty variables. To get one (or more) variables instantiated from other files you have to call a smarty method.
For example, if you have developed a module you have to call:
$myvar = $this->context->smarty->GetTemplateVars('myvar');
I decided to rewrite an older website that I made years ago and use templating system. I decided to use Latte, as its generating PHP files, which makes it really fast compared to systems that parse tpl every time. But I was not able to figure out, how to call a function with latte and get its result.
I am used to our custom company TPL system which can call any function and even pass parameters to it just by calling {function_name.param} or use function constants with {function::param}.
Is something like this possible purely in Latte (I am not using Nette or any other framework)? I do not want to call every single function in PHP and add it to array of parameters that TPL has to its disposal. That just makes it slower (yep I know I could use ifs in there and then ifs in TPL, but that's also an useless code duplication).
I want it to be able to call a function within class that's rendering the TPL (or its parent classes OFC) and return its output when I need it (if I even do need it), so I can avoid unnecessary calls to functions when initializing parameters for TPL parsing.
I tried to google quite a lot, but I didn't find anything useful.
I should also mention, that I am not going to use any framework at all, except Latte with Tracy and Tester for automatic testing. I do not want to use Nette or Symfony 2 etc. as the site is not that big and using whole framework would just make it even more complicated than it needs to be.
Thanks.
.
Ps.: Could somebody create tag for Latte?
You can simply call any php function this way:
{? echo 'hello'}
or in newer versions of Latte:
{php echo 'hello'}
Also, you can pass instances of Nette\Utils\Html (small lib separated from framework, full of great tools even for small apps) which will be directly rendered.
Or if you want to use own class rendering output directly, simply implement __toString() function using IHtmlString interface:
class MyOwnClassRenderableByLatte implements \Latte\Runtime\IHtmlString
{
function __toString()
{
return 'Hi';
}
}
Template sample including your later questions:
{php
// You can instantiate needed classes in one synoptical block
// in the head of template file or reather instantiate them
// outside of template and pass them as template variables
$a = new ClassA();
$b = new ClassB();
}
<div>{$a->someFunction()}</div>
<div>
{* Or you can instantiate class inplace this way,
but I wouldn't recommend it. BTW: This is Latte comment.
*}
{php (new ClassC())->otherFunction()}
</div>
Try to use something like this, its same as with javascript
{some code} //is for latte expression
{ some other code} //with space after first bracket its no more latte expression
Not sure if your TPL will handle it, but you will see
If it will work, you can use more imagination and use something like
{
some fluffy code
}
I'm trying to add customization to a Wordpress theme. Instead of replacing the original code, I'm using a "child theme" technique where I can replace files individually.
Now, that original code invokes the get_footer(), and I like to intercept that call so that I can insert my own html output before the actual get_footer function does its work.
I've read about the PHP 5's __call method but that only applies to classes whereas the WP code is not using classes but global functions. So I cannot use that technique, right?
get_footer() function just find and load proper template for the footer. There is a hook in WP get_footer that rans just before template being loaded.
Codex says that is not good to echo-es from this function, but you can use it to load your own template just before template that get_footer() loads.
You can do something like this in your functions.php:
add_action( 'get_footer', 'my_hook_fn' );
function my_hook_fn() {
load_template('my_template_file.php');
}
Codex
I use that thick in several sites, they do not use child themes, but I think this will do the trick.
I need to inject a HTML file from another website into my base.html.twig file.
One solution would be to use the php function 'file_get_contents' within my controller and make it available to the template like so:
$globalFooter = file_get_contents('http://mysite.co.uk/footer/footer.html.twig');
return $this->render('ImagineGdmBundle:Default:product.html.twig', array('globalFooter' => $globalFooter));
But that would mean having to implement the code above to every controller function that uses the footer. Is there a better way?
Im sure there must be a way to create a global variable and make them available to my base.html.twig template...but I am new to Symfony and am unsure how to do it. Any suggestions??
Question well explained in documentation about twig global variables. Your situation requires create twig extension. It would be a simlpe function that you can call anywhere in your templates.
I have tried making a function which I can run in the .tpl files.
I included a class Templates:
<?php
class Templates {
function getTemplate($template, $gameid) {
echo "test";
}
}
?>
Then I put this into the PHP page.
//Create a template object
$templates = new Templates();
$tpl->assign("template", $templates);
And then I try calling the function in the tpl file:
{$template->get('header',1)}
But I just get a blank page when I try this.
Any ideas how to actually do this?
You should probably look through the documentation on Smarty Plugins. These allow you to register your own functions which can be used just like the built-in tags and modifiers, or even custom sources of template data for use with the {include} function.
Your example is too stripped-down to guess what you actually want to achieve, so feel free to post a follow-up question if you can't see a way of achieving your specific goal.