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.
Related
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.
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.
I am passing a variable to a view script from a controller with Zend Framework. I want to know if there is anyway I can pre-filter the view so that I can change how the variable is retrieved in the view script.
For example. In the controller I have:
$this->view->name = 'Bob';
And in the view I have:
echo $this->name;
Which works fine no question! What I want is to occationally have the ability to change it so I can just use:
echo $name;
So basically, removing the $this statement. Is this possible? I am making a template and have other designers using the template system and want to make it easier for them then to type $this->array->name all the time.
I know in the view script I can simply add:
$name = $this->name;
But I would like to do that in the controller somewhere.
Thanks for your advice!
I'll say right up front that I think this is not a good idea to monkey around with the scope of your view's output variables. This works against the framework's MVC pattern by exposing the view's internals all over the global scope. But if you really insist on doing this, in the controller, you can probably assign them globally like this:
$GLOBALS['name'] = $this->view->name;
And access them later as $name or $GLOBALS['name'].
Note: depending on when the view's variables get populated versus when you assign them in the controller, you might need to assign references.
$GLOBALS['name'] = &$this->view->name;
Your idea is reasonable and seems quite easy to achieve. Just add this line to the beginning of your template:
<?php extract(get_object_vars($this)) ?>
Passing state to view by the means of global variables might not be considered a good idea by some, but this way is safe, as extract creates local variables, not global ones.
If you would like to reuse this method and prefer not to copy&paste, you could easily inherit from Zend_View, introduce this line to method (if I recall correctly) _run, and then in your bootstrap:
create an instance of the controller,
get the ViewRenderer action helper (using static method of Zend_Controller_Action_HelperBroker)
inject your view instance into ViewRenderer
This way all your views will present their public properties as variables.
I am learning how to use the Zend Framework. I come from a codeigniter background.
What I want to do is define a function somewhere that performs a very simple yet useful function. I am predominantly going to use the function within view scripts. I don;t really want to make a whole class for such a simple thing, so my question is, is there anywhere were can I put a file containg all of my general functions and how do I go about using it?
Thanks
John
What you are looking for are view helpers.
A view helper however is a function in a helper class. Therefore only one view helper can be put in a single class.
If you are using the project setup as used in the quick start tutorial or as generated by Zend_Tool, your view helpers should be put in the application/views/helpers directory.
Declaring a view helper is pretty simple, and is explained in great detail on this page of the zend framework documentation (i must say it's a bit hidden in the docs):
http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.custom
Some background information on view helpers as well as some standard included ones can be found on this page: http://framework.zend.com/manual/en/zend.view.helpers.html
Hope this helped you in the right direction.
If you realy whant to use a function you can make a library class with a static method , make a folder like this Application/Library/MyLib , then at bootstrap register MyLib namespace like this
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('MyLib'); , then inside MyLib folder you can make a filename MyClass , with a class name MyLib_MyClass , then inside you're view you can call MyLib_MyClass::staticMethod().
Tough i suggest you make a view helper for this . You don't realy use functions in ZF like you where used to in CI ( i was in you're exact situation a few months ago ) , ZF is all about OOP .
Can someone please tell me what the best way to pass values from a controller into a view would be? If anyone has played with codeignitor, they will know what I mean. I have looked at CIs code but can't find the file that handles this. I'd LOVE to know how this is done.
Thanks!
There's not necessarily a "best" way as far as I know, but there is a common method that I've seen used many times, and have used myself. It generally involves an associative array, and either the extract() function or variable variables.
Basically, all you do is set up your data into an associative array, using keys that will become your template variables.
//inside the controller
$data['name'] = 'my name';
$data['zip'] = '90210';
The $data array gets passed to the view somehow, either directly or indirectly, and extracted via extract() or using a loop of variable variables (same thing, really). The template can then be included, and the variables are in local scope.
//inside the view rendering process
extract($data);
//$name and $zip now exist
Code Igniter follows this exact procedure. Inside system\libraries\Loader.php in the most recent version (1.7.1) there's a function called view(), which is what you call in your CI controller to load a view/template (same thing really in CI). You pass a data array as the second parameter.
view() calls an internal function called _ci_load() in the same file, which extracts the data you passed it (and does some other wacky caching stuff). Your variables are ready to go after that in the local function scope, and can be manipulated inside the template after the subsequent include(), since everything happening in the included file exists in the local _ci_load() function scope as well.
I've used the exact same design in a quick-and-dirty homebrew MVC set up before. It's quite effective.
You might want to try CakePHP's 15-min blog sample. I haven't tried Code Igniter.
In Zend Framework, it's as simple as
class IndexController {
public function IndexAction {
$this->view->name='Name';
}
}
with the $this->view->xxxx setting the variable in the view.